-
Notifications
You must be signed in to change notification settings - Fork 0
/
songkick.js
348 lines (302 loc) · 10.6 KB
/
songkick.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
'use strict';
let songkickApikey = 'QNx6YgjKNSA5gGs5';
var googleApiKey = 'AIzaSyDbgYeUer3RCMYNeRR5yMV_t6RtNug_Hu4';
let lastfmApiKey = '87f6734e61f01465bdd78f72d81de0bd';
var fs = require('fs');
let request = require('request');
function getSongkickLocations(pos, callback) {
let songkickLocationUrl = 'http://api.songkick.com/api/3.0/search/locations.json?location=geo:' +
pos.lat + ',' + pos.lng +
'&apikey=' + songkickApikey;
request(songkickLocationUrl, function(error, response, body) {
if (!error && response.statusCode === 200) {
let data = JSON.parse(body);
if (data.resultsPage.status === 'ok') {
callback(null, data.resultsPage.results);
} else {
callback('ERROR getting nearby locations');
}
} else {
callback('ERROR ' + error + ' apistatus: ' + response.statusCode);
}
});
}
function getEventDataFromAreas(areas, callback) {
var events = [];
var cbInit = false;
for (var areaName in areas) {
let areaInfo = areas[areaName];
getAreaConcertData(areaInfo, 1, (err, eventInfo) => {
if (err) {
console.log(err);
} else {
events.push(eventInfo);
if (!cbInit) {
callback((cb2) => {
parseEventData(events[0], (err, d) => {
events.splice(0, 1);
cb2(d, events.length);
});
});
cbInit = true;
}
}
});
}
}
function parseLocations(pos, data, callback) {
var areasToGet = {};
for (var i in data.location) {
let loc = data.location[i];
let metroArea = loc.metroArea;
let city = loc.city;
let distToArea = getDistanceFromLatLonInKm(metroArea.lat, metroArea.lng, pos.lat, pos.lng);
if (distToArea < 10) {
areasToGet[metroArea.displayName] = metroArea;
}
}
callback(areasToGet);
}
function eventUrl(id, page) {
if (!page) page = 1;
return 'http://api.songkick.com/api/3.0/metro_areas/' + id + '/calendar.json?apikey=' + songkickApikey + '&page=' + page;
}
function getAreaConcertData(area, page, callback) {
request(eventUrl(area.id, page), function(err, resp, body) {
if (err) {
callback('ERROR getting event info ' + err);
} else {
let events = JSON.parse(body);
if (events.resultsPage.status === 'ok') {
for (var i in events.resultsPage.results.event) {
callback(null, events.resultsPage.results.event[i]);
}
let numEvents = events.resultsPage.totalEntries;
let numPages = Math.ceil(numEvents / events.resultsPage.perPage);
if (page < numPages) {
getAreaConcertData(area, page + 1, callback);
}
} else {
callback('ERROR area event response ' + events.resultsPage.status);
}
}
});
}
function parseEventData(event, callback) {
let eventInfo = {};
eventInfo.title = event.displayName;
eventInfo.location = event.location;
eventInfo.bands = [];
eventInfo.eventInfo = {
title: event.venue.displayName,
ticketUrl: event.uri,
venueUrl: event.venue.uri,
address: '',
date: event.start.date,
};
reverseGeocodeLatlng(eventInfo.location, parseBandInfo);
function parseBandInfo(err, stringLocation) {
if (err)
console.log('SK ERROR reverse geo', err);
eventInfo.eventInfo.address = stringLocation;
let j = event.performance.length;
for (var i in event.performance) {
var band = event.performance[i];
let bandName = band.artist.displayName;
getArtistSongs(bandName, (err, uri) => {
let artistSpotifyUri;
let artistBio;
let artistPic;
if (err) {
artistSpotifyUri = null;
} else {
artistSpotifyUri = uri;
}
getArtistInfo(bandName, (err, info) => {
if (err) {
artistBio = null;
artistPic = null;
} else {
artistBio = info.bio;
artistPic = info.artistPic;
}
eventInfo.bands.push({
name: bandName,
desc: artistBio,
spotifyUri: artistSpotifyUri,
fullimage: artistPic ? artistPic : 'images/placeholder.png',
thumbnail: artistPic ? artistPic : 'images/thumbnails/avatar.svg',
});
j--;
if (j === 0)
callback(null, eventInfo);
});
});
}
}
}
function getArtistSongs(artistName, callback) {
artistName = escape(artistName);
let spotifyUrl = 'https://api.spotify.com/v1/search?q=' + artistName + '&type=artist';
request(spotifyUrl, function(err, resp, body) {
if (err) {
callback('ERROR getting spotify url ' + err);
} else {
let data;
try {
data = JSON.parse(body);
} catch (e) {
console.log('SPOTIFY ERROR JSON parse', e);
}
if (data) {
if (data.error) {
callback('SPOTIFY ERROR api ' + data.error);
} else {
if (data.artists.items.length > 0) {
let uri = data.artists.items[0].uri;
callback(null, uri);
} else {
callback('No results');
}
}
} else {
callback('SPOTIFY ERROR parsing api body');
}
}
});
}
function getArtistInfo(artistName, callback) {
artistName = escape(artistName);
var bioUrl = 'http://ws.audioscrobbler.com/2.0/\?method\=artist.getinfo\&artist\=' + artistName + '\&api_key\=' + lastfmApiKey + '\&format\=json';
request(bioUrl, function(err, resp, body) {
if (err) {
console.log('ERROR getting artist bio from lastfm', err);
callback(err);
} else {
let data;
try {
data = JSON.parse(body);
} catch (e) {
console.log('JSON parse error', e);
}
if (data) {
if (data.artist) {
let bio = data.artist.bio.summary;
let artistPic;
for (var i in data.artist.image) {
var img = data.artist.image[i];
if (img.size === 'mega') artistPic = img['#text'];
}
callback(null, {
bio,
artistPic
});
} else {
callback('No artist found');
}
} else {
callback('JSON parse error');
}
}
});
}
function reverseGeocodeLatlng(pos, callback) {
var url = 'https://maps.googleapis.com/maps/api/geocode/json?latlng=' + pos.lat + ',' + pos.lng + '&key=' + googleApiKey;
request(url, (err, resp, body) => {
if (err) {
callback(err);
} else {
var res = JSON.parse(body);
if (res.error_message) {
callback(res.error_message);
} else if (res.results[0])
callback(null, res.results[0].formatted_address);
else
callback('No reverse geo results');
}
})
}
// http://stackoverflow.com/questions/27928/calculate-distance-between-two-latitude-longitude-points-haversine-formula
function getDistanceFromLatLonInKm(lat1, lon1, lat2, lon2) {
var R = 6371; // Radius of the earth in km
var dLat = deg2rad(lat2 - lat1); // deg2rad below
var dLon = deg2rad(lon2 - lon1);
var a =
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
Math.sin(dLon / 2) * Math.sin(dLon / 2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var d = R * c; // Distance in km
return d;
}
function deg2rad(deg) {
return deg * (Math.PI / 180)
}
function getNextEvent(nextEvent, callback) {
nextEvent((event, left) => {
callback(false, event);
if (left > 0) {
setTimeout(() => {
getNextEvent(nextEvent, callback);
}, 20);
} else {
callback(true, null);
}
});
}
/**
* Returns a random number between min (inclusive) and max (exclusive)
*/
function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}
function addRandomLatlng(pos, n) {
return {
lat: pos.lat + getRandomArbitrary(0, n),
lng: pos.lng + getRandomArbitrary(0, n),
}
}
module.exports = (function() {
let api = {};
api.nearestAreas = function(coord, callback) {
getSongkickLocations(coord, (err, locations) => {
if (err) {
callback(err);
} else {
parseLocations(coord, locations, (results) => {
callback(null, results);
});
}
})
};
api.updateAreas = function(areas, db, callback) {
getEventDataFromAreas(areas, (nextEvent) => {
getNextEvent(nextEvent, (done, event) => {
if (done) {
callback();
} else {
if (event.location.lat && event.location.lng) {
event.location = addRandomLatlng(event.location, 0.001);
event.eventInfo.date = new Date(event.eventInfo.date);
db.addEvent(event, (err) => {
if (err)
console.log(err);
});
}
}
});
});
}
return api;
})();
if (!module.parent) {
console.log('Starting as standalone');
var sk = module.exports;
sk.nearestArea(currentpos, (err, metroArea) => {
console.log(err, metroArea);
var db = require('./mongowrapper.js');
sk.updateAreas(metroArea, db, () => {
console.log('done?');
});
});
}