Last active
December 17, 2015 14:19
-
-
Save jonjenkins/5623725 to your computer and use it in GitHub Desktop.
Playlist capture all songs in Spotify format, played on Turntable tt.fm (if available)
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 () { | |
if (window.playlistt == undefined) window.playlistt = {}; | |
window.playlistt = $.extend(window.playlistt, { | |
i_awesome: null, | |
i_queuer: null, | |
i_watcher: null, | |
lamed: false, | |
room: '', | |
ttfm: null, | |
awesome: function () { | |
window.playlistt.vote('up'); | |
window.playlistt.stop(); | |
}, | |
destruct: function () { | |
clearInterval(window.playlistt.botResetAFKTimer); | |
clearInterval(window.playlistt.i_watcher); | |
window.playlistt.stop(); | |
}, | |
init: function () { | |
console.log('Initializing Playlistt-tt.fm socket connection...'); | |
$('.roomView').ready(function () { | |
window.playlistt.ttfm = window.turntable.buddyList.room; | |
if (window.playlistt.ttfm === null) { | |
console.log('Refresh page to find tt.fm objects'); | |
return; | |
} | |
window.playlistt.room = window.location.pathname; | |
$('#lame-button').unbind().bind('click', function () { | |
window.playlistt.lame(); | |
}); | |
turntable.addEventListener('message', window.playlistt.listener); | |
window.playlistt.awesome(); | |
window.playlistt.botResetAFKTimer = setInterval(function () { | |
$(window).focus(); | |
}, 60000); | |
window.playlistt.i_watcher = setInterval(function () { | |
if (window.location.pathname != window.playlistt.room) { | |
window.playlistt.destruct(); | |
} | |
}, 3000); | |
// http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css | |
// http://code.jquery.com/ui/1.10.3/themes/black-tie/jquery-ui.min.css | |
$('head').append($('<link rel="stylesheet" type="text/css" />').attr('href', 'http://code.jquery.com/ui/1.10.3/themes/black-tie/jquery-ui.min.css')); | |
document.onkeydown = window.playlistt.keydown; | |
}); | |
}, | |
keydown: function (evt) { | |
if (!evt) evt = event; | |
if (evt.ctrlKey == true || evt.metaKey == true) { | |
switch (evt.keyCode) { | |
case 16: | |
var songs = localStorage['playlistt'] || '[]'; | |
songs = JSON.parse(songs); | |
var arrSpotify = []; | |
for (var s in songs) | |
if (songs[s].spotify_href !== null) arrSpotify.push(songs[s].spotify_href); | |
window.playlistt.modal(JSON.stringify(arrSpotify)); | |
break; | |
} | |
} | |
}, | |
lame: function () { | |
if (!window.playlistt.lamed) { | |
window.playlistt.vote('up'); | |
window.playlistt.stop(); | |
window.playlistt.lamed = true; | |
} | |
setTimeout(function () { | |
window.playlistt.vote('down'); | |
}, 250); | |
}, | |
listener: function (d) { | |
console.log(d); | |
if (d.command == 'newsong') { | |
if (d.room.metadata.current_dj != window.turntable.user.id) { | |
clearTimeout(window.playlistt.i_awesome); | |
clearTimeout(window.playlistt.i_queuer); | |
window.playlistt.lamed = false; | |
var timeAmt = Math.floor(Math.random() * window.playlistt.ttfm.currentSong.metadata.length / 4 * 1000); | |
window.playlistt.i_awesome = setTimeout(function () { | |
window.playlistt.vote('up'); | |
}, timeAmt); | |
timeAmt = Math.floor(Math.random() * window.playlistt.ttfm.currentSong.metadata.length / 2 * 1000); | |
window.playlistt.i_queuer = setTimeout(function () { | |
$('.queue').click(); | |
}, timeAmt); | |
} | |
var songs = localStorage['playlistt'] || '[]'; | |
songs = JSON.parse(songs); | |
var song = turntable.current_songid; | |
if (localStorage['playlistt_lastsong'] != song) { | |
localStorage['playlistt_lastsong'] = song; | |
var heard = false; | |
for (var s in songs) | |
if (songs[s].tt_songid == song) heard = true; | |
if (!heard) | |
$.getJSON('http://ws.spotify.com/search/1/track.json?q=%22' + turntable.current_title.replace(/ *\([^)]*\) */g, '').replace(/(\r\n|\n|\r)/gm, '') + '%22%20%22' + turntable.current_artist.replace(/ *\([^)]*\) */g, '').replace(/(\r\n|\n|\r)/gm, '') + '%22', function (data) { | |
var time = Date.now(); | |
var song_href = null; | |
if (data.info.num_results > 0) song_href = data.tracks[0].href; | |
else console.log('Song "' + turntable.current_title + '" not found on Spotify. Lame.'); | |
songs.push({ tt_songid: turntable.current_songid, spotify_href: song_href, title: turntable.current_title, artist: turntable.current_artist, played: time }); | |
localStorage['playlistt'] = JSON.stringify(songs); | |
}); | |
} | |
} | |
else if (d.command == 'update_votes') { | |
$.each(d.room.metadata.votelog, function () { | |
if (this[0] == window.turntable.user.id) { | |
window.playlistt.stop(); | |
return false; | |
} | |
}); | |
} | |
}, | |
modal: function (text) { | |
var menudiv = document.createElement('div'); | |
menudiv.id = 'playlistt'; | |
menudiv.title = 'Dialog'; | |
$('body').append(menudiv); | |
$('#playlistt').dialog(); | |
var form = document.createElement('form'); | |
form.innerHTML = '<textarea style="width:100%;height:100%">' + text + '</textarea>'; | |
$('#playlistt').append(form); | |
$('#playlist_spotify').live('click', function () { | |
console.log('playlist_spotify clicked'); | |
}); | |
}, | |
socket: function (c, a) { | |
if (c.api == 'room.now') { | |
return; | |
} | |
c.msgid = turntable.messageId; | |
turntable.messageId++; | |
c.clientid = turntable.clientId; | |
if (turntable.user.id && !c.userid) { | |
c.userid = turntable.user.id; | |
c.userauth = turntable.user.auth; | |
} | |
var d = JSON.stringify(c); | |
var b = $.Deferred(); | |
turntable.whenSocketConnected(function () { | |
if (turntable.socket.transport.type == 'websocket') { | |
turntable.socketLog(turntable.socket.transport.sockets[0].id + ':<' + c.msgid); | |
} | |
turntable.socket.send(d); | |
turntable.socketKeepAlive(true); | |
turntable.pendingCalls.push({ | |
msgid: c.msgid, | |
handler: a, | |
deferred: b, | |
time: util.now() | |
}); | |
}); | |
return b.promise(); | |
}, | |
stop: function () { | |
clearTimeout(window.playlistt.i_awesome); | |
clearTimeout(window.playlistt.i_queuer); | |
}, | |
vote: function (c) { | |
var f = $.sha1(window.playlistt.ttfm.roomId + c + window.playlistt.ttfm.currentSong._id); | |
var d = $.sha1(Math.random() + ''); | |
var e = $.sha1(Math.random() + ''); | |
window.playlistt.socket({ | |
api: 'room.vote', | |
roomid: window.playlistt.ttfm.roomId, | |
section: window.playlistt.ttfm.section, | |
val: c, | |
vh: f, | |
th: d, | |
ph: e | |
}); | |
} | |
}); | |
window.playlistt.init(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment