-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsongs.js
33 lines (32 loc) · 1.04 KB
/
songs.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
Songs = new Meteor.Collection("songs");
if(Meteor.is_server) {
Meteor.methods({searchSong: function (query) {
this.unblock();
var url = "http://ws.spotify.com/search/1/track.json?q=" + encodeURI(query);
var result = Meteor.http.get(url);
if (result.statusCode === 200) {
eval("var json = " + result.content);
var tracks = json.tracks;
var firstTrack = json.tracks[0];
var requestedSong = {
name: firstTrack['name'],
artist: firstTrack['artists'][0]['name'],
href: firstTrack['href'],
loves: [ 'username-here-TODO' ] // fixme
}
var song = Songs.findOne({href: requestedSong['href']})
if (song) {
///////// TODO: already exists, do something cool //////////
// loves = song['loves']
// loves.push('next-username-here-TODO')
// Songs.update(song._id, {$set: {loves: loves}})
} else {
Songs.insert(requestedSong);
};
return requestedSong;
} else {
return result.content;
}
return false;
}});
};