Skip to content

Instantly share code, notes, and snippets.

@BenWard
Last active August 29, 2015 14:06
Show Gist options
  • Save BenWard/4189f5cf1452adac41fc to your computer and use it in GitHub Desktop.
Save BenWard/4189f5cf1452adac41fc to your computer and use it in GitHub Desktop.
Quick and dirty function to extract tracklistings from a BFF.FM show playlist and convert it into a Mixcloud compatible blob of text. Recognises gaps in songs and creates chapter breaks automatically.
(function mixcloudify (startHour) {
var TALK_BREAK_MINIMUM = 4, // minimum length to insert a talk break
showDate;
function showTime () {
var start = startHour.split(":").map(function (item) {
return padNumber(item);
});
return Date.parse([
showDate,
"T",
start[0] || "00",
":",
start[1] || "00",
":",
start[2] || "00",
"-07:00"].join("")
) / 1000;
}
function padNumber (i) {
i = "" + i;
if (i.length == 1) return "0" + i;
return i;
}
function formatOffset (startTime) {
var durationSecs = startTime - showTime();
return [durationSecs, 0, 0].map(function (val, i, arr) {
var nextVal,
next = i + 1;
if (next == arr.length) return val;
nextVal = Math.floor(val / 60);
arr[next] = nextVal;
return val - (nextVal * 60);
}).map(padNumber).reverse().join(":");
}
function timestampToSecs (dateString) {
if (!showDate) {
showDate = dateString.split(" ")[0];
}
return Date.parse(dateString.replace(" ", "T") + "-07:00") / 1000;
}
// 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 track info
prompt("Paste into Mixcloud", Array.prototype.map.call(expandedTracks, function (item) {
return {
start: timestampToSecs(item.querySelector('input[name="data[Track][played]"]').value),
end: timestampToSecs(item.querySelector('input[name="data[Track][ended]"]').value),
artist: item.querySelector('input[name="data[Track][artist]"]').value,
title: item.querySelector('input[name="data[Track][title]"]').value
};
}).sort(function (a, b) {
if (a.start > b.start) return 1;
if (b.start > a.start) return -1;
return 0;
}).reduce(function (finalTracklisting, currentTrack, idx, allTracks) {
var previousTrackEnd = idx > 0 && allTracks[idx-1].end || showTime();
// Auto detect talk breaks
if ((currentTrack.start - previousTrackEnd) > TALK_BREAK_MINIMUM) {
finalTracklisting.push("Talk Break\t" + formatOffset(previousTrackEnd));
}
finalTracklisting.push([
currentTrack.artist,
currentTrack.title,
formatOffset(currentTrack.start)].join(" - ")
);
return finalTracklisting;
}, []).join("\n"));
}, 300);
})(prompt("What time did you start broadcasting? e.g. `20:01:30`", "20:00:00"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment