Created
October 15, 2014 08:14
-
-
Save BenWard/1f38be8e28df87fced81 to your computer and use it in GitHub Desktop.
Quick script to generate end-times from track lengths for tracks played on Creek.FM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function endTimesFromTrackLength () { | |
function padNumber (i) { | |
i = "" + i; | |
if (i.length == 1) return "0" + i; | |
return i; | |
} | |
function timestampToSecs (dateString) { | |
return Date.parse(dateString.replace(" ", "T") + "-07:00") / 1000; | |
} | |
function durationToSecs (duration) { | |
var parts = duration.split(":"); | |
if (parts.length == 1) { | |
parts.unshift(0); | |
} | |
return (parseInt(parts[0], 10) * 60) + parseInt(parts[1], 10) ; | |
} | |
function formatCreekTimestamp (ms) { | |
var date = new Date(ms * 1000) | |
, year = date.getFullYear() | |
, month = padNumber(date.getMonth() + 1) | |
, day = padNumber(date.getDate()) | |
, hour = padNumber(date.getHours()) | |
, minutes = padNumber(date.getMinutes()) | |
, seconds = padNumber(date.getSeconds()); | |
return [ | |
[year, month, day].join("-") | |
, [hour, minutes, seconds].join(":") | |
].join(" "); | |
} | |
// Expand all tracks | |
var collapsedTracks = document.querySelectorAll("#Tracks tbody tr td:first-child"); | |
Array.prototype.forEach.call(collapsedTracks, function (row) { row.click(); }); | |
var waitingForExando = window.setInterval(function () { | |
var expandedTracks = document.querySelectorAll("#Tracks tbody tr.row_edit"); | |
console.log("Checking for Expanded Content", expandedTracks.length, collapsedTracks.length); | |
// Wait for fetch to happen | |
if (expandedTracks.length != collapsedTracks.length) return; | |
// Stop polling | |
window.clearInterval(waitingForExando); | |
// Extract start + length, turn into end time. | |
Array.prototype.forEach.call(expandedTracks, function (item) { | |
var start = timestampToSecs(item.querySelector('input[name="data[Track][played]"]').value) | |
, duration = durationToSecs(item.querySelector('input[name="data[Track][length]"]').value); | |
if (!start || !duration) return; | |
console.log(start, duration, (start + duration), formatCreekTimestamp(start + duration)); | |
item.querySelector('input[name="data[Track][ended]"]').value = formatCreekTimestamp(start + duration); | |
}); | |
}, 300); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment