-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
195 lines (159 loc) · 6.05 KB
/
script.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
$(document).ready(function(){
$('#navTabs a').click(function (e) {
e.preventDefault()
$(this).tab('show')
})
$("#navTabs button").click(authorize);
$("#allGenres button").click(updateAllGenres);
$("#hipHop button").click(updateHipHop);
$("#rnb button").click(updateRnB);
});
function updateAllGenres()
{
update("https://itunes.apple.com/us/rss/topsongs/limit=50/explicit=true/xml", "allGenres", "2V07Dnbb1oeOi1DuastrUV");
}
function updateHipHop()
{
update("https://itunes.apple.com/us/rss/topsongs/limit=50/genre=18/explicit=true/xml", "hipHop", "2Mvn8DwcRwa36CrX0YpVHt");
}
function updateRnB()
{
update("https://itunes.apple.com/us/rss/topsongs/limit=50/genre=15/explicit=true/xml", "rnb", "2aN17RtUI8TNUF8dQVLhnO");
}
function authorize()
{
var clientID = "8c1c8a8cfdee4f2b8a53f86de34d5ecd";
var scopes = "playlist-modify-public playlist-modify-private";
var redirectURI = "http://kylenburke.github.io/redirect.html";
//var redirectURI = "http://localhost/itunify/redirect.html";
var url = "https://accounts.spotify.com/authorize?response_type=token&client_id=" + clientID + "&scope=" + scopes + "&redirect_uri=" + redirectURI;
window.open(url);
$("button").removeAttr("disabled");
}
function update(url, genre, playlistURI)
{
$("#" + genre + " div").text("Updating...");
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function()
{
if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
{
responseSuccessful(xmlHttp.responseXML, genre, playlistURI);
}
};
xmlHttp.open("GET", url, true);
xmlHttp.send();
}
function responseSuccessful(doc, genre, playlistURI)
{
$("#"+genre+" table").html("");
clearPlaylsit(localStorage.getItem("accessToken"), playlistURI);
var entryNodes = doc.getElementsByTagName("entry");
var tracks = "";
var queries = [];
for(var i = 0; i < entryNodes.length; i++)
{
var n = entryNodes[i];
var currentField = n.childNodes[7].textContent;
var trackTitle;
var featured = null;
if(currentField.split(" (feat. ").length > 1)
{
trackTitle = currentField.split(" (feat. ")[0];
featured = currentField.split(" (feat. ")[1].split(")")[0];
}
else if(currentField.split(" [feat. ").length > 1)
{
trackTitle = currentField.split(" [feat. ")[0];
featured = currentField.split(" [feat. ")[1].split("]")[0];
}
else
trackTitle = currentField;
currentField = n.childNodes[17].textContent;
var artistName = currentField;
if(featured != null)
{
if(featured.split(" & ").length > 1)
artistName += ", " + featured.split(" & ")[0] + ", " + featured.split(" & ")[1];
else
artistName += ", " + featured;
}
var artwork = n.childNodes[21].textContent;
var title = n.childNodes[7].textContent;
var artist = n.childNodes[17].textContent;
var album = n.childNodes[31].textContent;
$("#"+genre+" table").append('<tr><td>' + (i + 1) +'</td><td><image src="' + artwork + '" width="32px" height="32px" /></td><td>' + title + "</td><td>" + artist + "</td><td>" + album + "</td></tr>");
var query = "track:" + encodeURI(trackTitle + " ") + "artist:" + encodeURI(artistName);
var forbiddenChar = false;
if(query.split("#").length > 1) //forbidden character
{
query = query.split("#")[0] + query.split("#")[1];
forbiddenChar = true;
}
$.ajax({
url: "https://api.spotify.com/v1/search?q=" + query + "&type=track",
type: "GET",
dataType: "json",
async: false,
success: function(data)
{
if(forbiddenChar)
$("#" + genre + " table tbody").children().eq(i).addClass("warning");
if(data.tracks.items.length == 0)
{
$("#" + genre + " table tbody").children().eq(i).addClass("danger");
return;
}
var uri = data.tracks.items[0].uri;
if(data.tracks.items.length > 1 && !data.tracks.items[0].explicit && data.tracks.items[1].explicit)
{
var name1 = data.tracks.items[0].name;
var artist1 = data.tracks.items[0].artists;
var name2 = data.tracks.items[0].name;
var artist2 = data.tracks.items[0].artists;
if(name1 == name2 && artist1 == artist2)
uri = data.tracks.items[1].uri;
}
tracks += uri + ",";
}
});
}
tracks = tracks.slice(0, -1);
$.ajax({
url: "https://api.spotify.com/v1/users/casualkyle/playlists/" + playlistURI +"/tracks?uris=" + tracks,
type: "POST",
headers: {
"Authorization": "Bearer " + localStorage.getItem("accessToken"),
"Content-Type": "application/json"
}
});
$("#" + genre + " div").text("Done!");
}
function clearPlaylsit(accessToken, playlistURI)
{
$.ajax({
url: "https://api.spotify.com/v1/users/casualkyle/playlists/" + playlistURI,
headers: {
"Authorization": "Bearer " + accessToken
},
dataType: "json",
success: function(data)
{
if(data.tracks.items.length == 0)
return;
var positions = "";
for(var i = 0; i < data.tracks.items.length; i++)
positions += i.toString() + ", ";
positions = positions.slice(0, -2);
$.ajax({
type: "DELETE",
url: "https://api.spotify.com/v1/users/casualkyle/playlists/" + playlistURI + "/tracks",
headers: {
"Authorization": "Bearer " + accessToken,
"Content-Type": "application/json"
},
data: "{\"positions\": ["+positions+"], \"snapshot_id\": \""+data.snapshot_id+"\"}"
});
}
});
}