forked from nobuf/Pandora-To-Spotify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pandoratospotify.js
76 lines (73 loc) · 3.8 KB
/
pandoratospotify.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
var SPOTIFY_METADATA_API_ENDPOINT = 'http://ws.spotify.com/search/1/track.json';
var GOOGLE_GEOCODING_API_ENDPOINT = 'http://maps.googleapis.com/maps/api/geocode/json';
var countryCode = null;
$(document).ready(function(){
var $queue = $('<div></div>');
$queue.queue(function(){
navigator.geolocation.getCurrentPosition(function(position){
$.get(GOOGLE_GEOCODING_API_ENDPOINT, {sensor:false, latlng:position.coords.latitude+','+position.coords.longitude}, function(locationData){
if ('status' in locationData && locationData['status'] == 'OK') {
$.each(locationData['results'][0]['address_components'], function(index, value){
if (value['types'][0] == 'country') {
countryCode = value['short_name'];
$queue.dequeue();
}
});
}
}).error(function(){
$queue.dequeue();
});
}, function(error){
$queue.dequeue();
});
}).queue(function(){
chrome.extension.onRequest.addListener(function(data){
if (data && data.length > 0) {
$('#error').hide();
$('#table-wrapper').show();
$('#done').fadeIn();
}
$.each(data, function(index, value){
window.setTimeout(function(){
var query = 'artist:"' + value['artist'] + '"+track:"' + value['title'] + '"';
$.get(SPOTIFY_METADATA_API_ENDPOINT, {q:query}, function(trackInfo){
var isAvailable = false;
if ('tracks' in trackInfo && trackInfo['tracks'].length > 0 && 'href' in trackInfo['tracks'][0]) {
var idx = 0;
var flag = false;
if (countryCode !== null) {
for (; idx < trackInfo['tracks'].length; idx++) {
if (trackInfo['tracks'][idx]['album']['availability']['territories']
&& (trackInfo['tracks'][idx]['album']['availability']['territories'].indexOf(countryCode) != -1
|| trackInfo['tracks'][idx]['album']['availability']['territories'].indexOf('worldwide') != -1)) {
flag = true;
break;
}
}
} else {
flag = true;
}
if (flag === true) {
isAvailable = true;
$('textarea').val($('textarea').val()+trackInfo['tracks'][idx]['href']+"\n");
}
}
$('tbody').append('<tr><td>' + value['artist'] + '</td><td>' + value['title'] + '</td><td>' + (function(flag){
var okOrNo = flag ? 'ok' : 'no';
return '<span class="alert ' + okOrNo + '">' + okOrNo + '</span>';
}(isAvailable)) + '</td></tr>');
if (index === data.length - 1) {
$('textarea').focus().select();
}
});
}, 110); // no more than 10 requests per second
});
});
chrome.tabs.executeScript(null, {file: "lib/jquery-1.7.min.js"}, function(){
chrome.tabs.executeScript(null, {file: "lib/Thumbups.js"}, function(){
chrome.tabs.executeScript(null, {file: "content_script.js"});
});
});
$queue.dequeue();
});
});