This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
pandoratospotify.js
83 lines (81 loc) · 3.3 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
77
78
79
80
81
82
83
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){
$('#loading').hide();
if (data && data.length > 0) {
$('#table-wrapper').show();
$('#done').fadeIn();
} else {
$('#error').show();
return;
}
$.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-2.0.3.min.js"}, function(result){
if (undefined === result) {
$('#loading').hide();
$('#error').fadeIn();
return;
}
chrome.tabs.executeScript(null, {file: "lib/Thumbups.js"}, function(){
chrome.tabs.executeScript(null, {file: "content_script.js"});
});
});
$queue.dequeue();
});
});