-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathapp.js
221 lines (191 loc) · 5.42 KB
/
app.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
(function(exports) {
var cache = new WordCache();
var g_name = '';
var g_tracks = '';
function setStatus(text) {
if (text != '') {
$('#status').html(
'<div class="progress progress-striped active">' +
'<div class="progress-bar" role="progressbar" aria-valuenow="100" aria-valuemin="0" aria-valuemax="100" style="width: 100%">' +
text +
'</div>' +
'</div>'
);
} else {
$('#status').html('');
}
}
var Playlist = function() {
}
var splitText = function(inputtext) {
var words = inputtext
.split(/[ \n\r\t]/)
.map(function(w) { return w.toLowerCase().trim().replace(/^[.,-]+/,'').replace(/[.,-]+$/g,''); })
.filter(function(w) { return (w.length > 0); });
words = words.slice(0, 100);
return words;
}
var refreshText = function() {
setStatus('Updating text...');
g_name = $('#alltext').val().trim();
var words = splitText(g_name);
console.log('text changed.', g_name, words);
cache.lookupWords(words, function(worddata) {
setStatus('');
console.log('wordcache callback', worddata);
// $('#debug').text(JSON.stringify(worddata, null, 2));
var txt = '';
g_tracks = [];
worddata.forEach(function(data) {
console.log('word', data.word);
data.tracks.sort(function(a,b) {
return Math.random() - 0.5;
})
var names = data.tracks.map(function(track) {
return track.name;
});
console.log('names', names);
var f = new FuzzySet(names);
var fr = f.get(data.word);
console.log('fr', fr);
var found = null;
var title = '';
if (!found) {
data.tracks.forEach(function(track) {
if (track.name.toLowerCase().trim() === data.word.toLowerCase().trim()) {
found = track;
}
});
}
if (!found) {
if (fr && fr.length > 0) {
data.tracks.forEach(function(track) {
if (track.name === fr[0][1]) {
found = track;
}
});
}
}
if (!found) {
if (data.tracks.length > 0) {
found = data.tracks[0];
}
}
console.log('found', found);
if (found) {
g_tracks.push(found.uri);
txt += '<div class="media">' +
'<a class="pull-left" href="#"><img class="media-object" src="' + found.cover_url + '" /></a>' +
'<div class="media-body">' +
'<h4 class="media-heading"><a href="' + found.uri + '">' + found.name + '</a></h4>' +
'Album: <a href="' + found.album_uri + '">' + found.album +
'</a><br/>Artist: <a href="' + found.artist_uri + '">' + found.artist+'</a>' +
'</div>' +
'</div>\n';
} else {
txt += '<div class="media">No match found for the word "' + data.word+ '"</div>\n'
}
});
$('#debug').html(txt);
});
}
var doSearch = function(word, callback) {
console.log('search for ' + word);
var url = 'https://api.spotify.com/v1/search?type=track&limit=50&q=' + encodeURIComponent('track:"'+word+'"');
$.ajax(url, {
dataType: 'json',
success: function(r) {
console.log('got track', r);
callback({
word: word,
tracks: r.tracks.items
.map(function(item) {
var ret = {
name: item.name,
artist: 'Unknown',
artist_uri: '',
album: item.album.name,
album_uri: item.album.uri,
cover_url: '',
uri: item.uri
}
if (item.artists.length > 0) {
ret.artist = item.artists[0].name;
ret.artist_uri = item.artists[0].uri;
}
if (item.album.images.length > 0) {
ret.cover_url = item.album.images[item.album.images.length - 1].url;
}
return ret;
})
});
},
error: function(r) {
callback({
word: word,
tracks: []
});
}
});
}
var resolveOneWord = function() {
var word = cache.pop();
if (word) {
console.log('time to resolve word', word);
setStatus('Looking up the word "' + word + '"');
doSearch(word, function(result) {
console.log('got word result', result);
cache.store(word, result);
setTimeout(resolveOneWord, 1);
});
} else {
console.log('no words to look up...');
setTimeout(resolveOneWord, 1000);
}
}
var g_access_token = '';
var g_username = '';
var client_id = '';
var redirect_uri = '';
if (location.host == 'localhost:8000') {
client_id = 'd37a9e88667b4fb3bc994299de2a52bd';
redirect_uri = 'http://localhost:8000/callback.html';
} else {
client_id = '6f9391eff32647baa44b1a700ad4a7fc';
redirect_uri = 'http://lab.possan.se/playlistcreator-example/callback.html';
}
var doLogin = function(callback) {
var url = 'https://accounts.spotify.com/authorize?client_id=' + client_id +
'&response_type=token' +
'&scope=playlist-read-private%20playlist-modify%20playlist-modify-private' +
'&redirect_uri=' + encodeURIComponent(redirect_uri);
localStorage.setItem('createplaylist-tracks', JSON.stringify(g_tracks));
localStorage.setItem('createplaylist-name', g_name);
var w = window.open(url, 'asdf', 'WIDTH=400,HEIGHT=500');
}
var refreshtimer = 0;
var queueRefreshText = function() {
if (refreshtimer) {
clearTimeout(refreshtimer);
}
refreshtimer = setTimeout(function() {
refreshText();
}, 1000);
}
exports.startApp = function() {
setStatus('');
console.log('start app.');
$('#alltext').keyup(function() {
queueRefreshText();
});
$('#alltext').change(function() {
queueRefreshText();
});
$('#start').click(function() {
doLogin(function() {});
})
$('#alltext').text('hello world');
refreshText();
resolveOneWord();
}
})(window);