-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmartradio.js
404 lines (368 loc) · 12.4 KB
/
smartradio.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
$.getScript('api.js', function() {
console.log('finished loading api.js');
setTimeout(parse_query, 2000);
});
var available_songs = {};
var played_songs = [];
var current_song;
var next_song;
var same_artist_bump = 6;
var global_configuration = {
use_normalized_scores: true
};
function init_settings() {
$('#artist_bump').slider({
orientation: "horizontal",
min: 0,
max: 9,
step: 1,
value: 9 - same_artist_bump,
animate: true,
slide: function(event, ui) {
new_bump = 10 - ui.value;
for(key in available_songs) {
cur_song = available_songs[key];
if(cur_song.artist == undefined) {
continue;
}
if(current_song.artist != undefined && cur_song.artist.name === current_song.artist.name || cur_song.artist.name === $('#tags').val()) {
console.log("Bumping song " + key);
cur_song.score = cur_song.score / same_artist_bump * new_bump;
}
}
same_artist_bump = new_bump;
compute_next_song();
}
});
//setInterval(show_motivational_modal, 150000);
}
function add_service(service) {
if(service.search_tags || service.search_artist) {
$('#available_services').append('<span class="service"><label for="' + service.name + '">' + service.name + '</label><div id="' + service.name + '_slider" class="service-weight-slider"/><div><input type="checkbox" checked name="' + service.name + '" id="' + service.name + '"/></div><i class="icon-question-sign icon-white service-icon" title="' + service.tooltip_text + '"></i></span>');
$('.service-icon').tooltip();
$('#' + service.name + "_slider").slider({
orientation: "vertical",
min: 0.1,
max: 5,
step: 0.1,
value: service.weight,
animate: true,
slide: function(event, ui) {
this_id = $(this).attr('id');
this_id = this_id.replace('_slider', '');
checkbox = $('#' + this_id);
if(!checkbox.prop('checked')) {
checkbox.prop('checked', true);
}
console.log("Setting " + this_id + " weight to " + ui.value);
service = get_service(this_id);
recompute_scores(service, ui.value);
}
});
}
}
function compute_next_song() {
found = undefined;
$.each(available_songs, function(index, value) {
if(value.deleted || value.disliked) {
return true;
}
if(!found || found.score < value.score) {
found = value;
}
});
console.log("Computed next song");
next_song = found;
console.log(next_song);
if(next_song) {
$('#next_song_info').html(next_song.artist.name + " - " + next_song.name);
}
}
function play_next_song() {
current_song = next_song;
next_song = undefined;
delete available_songs[current_song.key];
played_songs[current_song.key] = current_song;
//Stop any Soundcloud players
pause_link = $('.playing a.sc-pause');
if(pause_link) {
pause_link.click();
}
if(current_song.embed && current_song.embed.code) {
$('#player').html(current_song.embed.code);
$('#song_info').html(build_song_info(current_song)).show();
if(current_song.embed.service.name == get_service('youtube').name) {
start_youtube_player();
} else if(current_song.embed.service.name == get_service('Soundcloud').name) {
console.log("starting soundcloud");
start_soundcloud_player(current_song.embed);
}
} else {
console.log("embed not found, looking for it with key = " + current_song.embed.key);
current_song.embed.service.search_embed(current_song.embed, function(embed) {
if(!embed) {
delete available_songs[current_song.embed.key];
compute_next_song();
play_next_song();
} else {
console.log("Loaded embed: " + embed.service_id);
current_song.embed.code = embed.code;
$('#player').html(embed.code);
$('#song_info').html(build_song_info(current_song)).show();
Mixeeba.refresh();
setInterval(refreshMixeeba, 3000);
if(embed.service_name == get_service('youtube').name) {
start_youtube_player();
} else if(embed.service_name == get_service('Soundcloud').name) {
start_soundcloud_player(embed);
}
}
});
}
add_similar_songs();
compute_next_song();
}
function refreshMixeeba() {
if($('.mixeeba-links').html() == ' ') {
Mixeeba.refresh();
}
}
function build_song_info(song) {
if(song.embed.service.search_another_embed == undefined) {
$('#change_embed').hide();
} else {
$('#change_embed').show();
}
ret = '<div class="track-info"><span class="mixeeba-artist">' + song.artist.name + '</span> - <span class="mixeeba-title">' + song.name + '</span></div>';
ret += '<div class="service-info">Found via ' + song.service.name + '</div>';
ret += '<div class="mixeeba-links"> </div><hr/>'
var url = encodeURIComponent('http://raibaz.github.com/wrltzr/?q=' + song.key.replace(' ', "_"));
$('#twitter-iframe').attr('src', '//platform.twitter.com/widgets/tweet_button.html?url=' + url + '&text=I%20just%20listened%20to%20' + escape(song.name) + '%20by%20' + escape(song.artist.name) + '%20on%20%23Wrltzr&count=none');
$('#facebook-share-link').attr('href', 'https://www.facebook.com/dialog/feed?app_id=393236134020452&link=' + url + '&picture=http://raibaz.github.com/wrltzr/img/logo_bg.png&name=' + escape(song.name) + '%20by%20' + escape(song.artist.name) + '&caption=I%20just%20listened%20to%20a%20song%20on%20Wrltzr&redirect_uri=http://raibaz.github.com/wrltzr');
return ret;
}
function add_similar_songs() {
if(!current_song || current_song == undefined) {
return;
}
$('#available_services :checked').each(function(index, value) {
service = get_service($(this).attr('id'));
if(service.get_similar_artists) {
service.get_similar_artists(current_song, function(artists) {
if(typeof(artists) == 'string') {
console.log("No similar artists found on " + artists + " for " + current_song.key);
return;
}
console.log("Found similar artists: " + artists);
$.each(artists, function(index, value) {
service.search_artist(value, add_songs);
});
});
}
else if(service.get_song_tags) {
service.get_song_tags(current_song, function(tags) {
console.log("Found tags " + tags);
if(!tags) {
return;
}
$.each(tags, function(index, value) {
service.search_tags(value, add_songs);
});
});
}
});
}
function add_songs(songs) {
$.each(results, function(index, value) {
if(available_songs[value.key]) {
console.log("Found song " + value.key + ", adding " + value.score + " to its score");
available_songs[value.key].score += value.score;
//TODO if there is a better embed, replace it it
} else {
available_songs[value.key] = value;
}
if(current_song && current_song.artist.name === value.artist.name) {
console.log("Adding a song from current artist ==> score bump!");
available_songs[value.key].score *= same_artist_bump;
}
if(value.artist.name === $('#tags').val() && $('#search_type').val() === 'artist') {
console.log("Adding a song by searched artist ==> score bump!");
available_songs[value.key].score *= same_artist_bump;
}
if(played_songs[value.key]) {
console.log("Found song " + value.name + " already played, killing its score");
available_songs[value.key].score /= 5;
}
var li_id = value.service.name + "_" + index;
if(value.artist && value.artist.name && value.name) {
li = '<li class="result" id="' + li_id + '"><span class="song_info">' + value.artist.name + " - " + value.name + '</span></li>';
$('#results').append(li);
}
if(value.embed) {
value.embed.service.search_embed(value.embed, function(embed_data) {
if(!embed_data) {
delete available_songs[value.key];
} else {
value.embed.code = embed_data.code;
}
});
}
});
}
function start_youtube_player() {
player = new YT.Player('youtube-player', {
events: {
'onReady': function(event) {
event.target.playVideo();
}, 'onStateChange': function(event) {
if(event.data === YT.PlayerState.ENDED) {
play_next_song();
_gaq.push(['_trackEvent', 'user_inputs', 'song_finished', current_song.key, 0, true]);
}
}
}
});
}
function start_soundcloud_player(embed) {
if(!embed) {
embed = current_song.embed;
}
$('#player .sc-player').scPlayer({
links: [{url: embed.key, title: embed.title}],
autoPlay: true,
apiKey: sound_cloud.client_id,
onPlayerTrackFinish: function() {
play_next_song();
_gaq.push(['_trackEvent', 'user_inputs', 'song_finished', current_song.key, 0, true]);
}
});
}
function update_all_scores(coeff) {
if(current_song.service.get_song_tags) {
current_song.service.get_song_tags(current_song, function(tags) {
$.each(tags, function(index, value) {
current_song.service.search_tags(value, function(songs) {
$.each(songs, function(index, value) {
if(available_songs[value.key]) {
artist_coeff = 1;
if($('#search_type').val() === 'artist' && value.artist.name === current_song.artist.name) {
console.log("Further bump for same artist");
artist_coeff = same_artist_bump;
}
console.log("Updating score for " + value.key + " by adding " + (value.score * coeff * artist_coeff));
available_songs[value.key].score += (value.score * coeff * artist_coeff);
}
});
//TODO: this should be at the end of all tags, not here at the end of every tag
compute_next_song();
})
});
});
} else {
current_song.service.get_similar_artists(current_song, function(artists) {
$.each(artists, function(index, value) {
console.log(value);
for(cur in available_songs) {
if(!available_songs.hasOwnProperty(cur)) {
continue;
}
song = available_songs[cur];
if(song.artist && song.artist.name == value) {
console.log("Changing score for song " + song.key);
available_songs[song.key].score += value.score * coeff;
}
}
});
compute_next_song();
});
}
}
function recompute_scores(service, new_weight) {
for(cur in available_songs) {
if(!available_songs.hasOwnProperty(cur)) {
continue;
}
loop_song = available_songs[cur];
if(loop_song.service && loop_song.service.name == service.name) {
console.log("Updating score for " + cur + " from " + loop_song.score + " to " + ((loop_song.score / loop_song.service.weight) * new_weight));
loop_song.score = (loop_song.score / loop_song.service.weight) * new_weight;
}
}
service.weight = new_weight;
if(available_songs)
compute_next_song();
}
function play_random_song() {
var available_songs_count = 0;
for(key in available_songs) {
if(available_songs.hasOwnProperty(key)) {
available_songs_count++;
}
}
var song_index = Math.floor(Math.random() * available_songs_count);
for(key in available_songs) {
if(!available_songs.hasOwnProperty(key)) {
continue;
}
if(played_songs[key]) {
continue;
}
if(song_index-- <= 0) {
next_song = available_songs[key];
play_next_song();
available_songs = {};
available_songs_count = 0;
played_songs = {};
$('#results').empty();
add_similar_songs();
compute_next_song();
break;
}
}
}
function reload_embed() {
console.log("reload_embed");
current_song.embed.service.search_another_embed(current_song.embed, function(new_embed) {
console.log("Found other embed");
console.log(new_embed);
if(!new_embed || new_embed == undefined) {
delete available_songs[current_song.key];
play_next_song();
} else {
current_song.embed.code = new_embed.code;
next_song = current_song;
play_next_song();
}
});
}
function delete_next_song() {
available_songs[next_song.key].deleted = true;
compute_next_song();
}
function parse_query() {
var url_song = get_url_song();
if(url_song != undefined) {
$('#query').val(url_song);
do_search_tags();
}
}
function get_url_song() {
var query = location.search.substring(1);
if(query) {
query_params = query.split('&');
for(i in query_params) {
cur = query_params[i];
cur.replace('&', '');
if(cur.indexOf('q=') != -1) {
return cur.replace('q=', '').split('_').join(' ');
}
}
}
}
function show_motivational_modal() {
$('#modal_motivational').modal('show');
setTimeout(hide_motivational_modal, 5000);
}
function hide_motivational_modal() {
$('#modal_motivational').modal('hide');
}