-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
189 lines (129 loc) · 5.92 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
var Promise = require('promise');
var async = require('async');
var SpotifyWebApi = require('spotify-web-api-node');
var pm = new (require('playmusic'));
var config = require('./config');
pm.init({email: config.google.email, password: config.google.password}, function() {});
var spotifyUserId = config.spotify.userId;
var spotifyApi = new SpotifyWebApi({
clientId: config.spotify.clientId,
clientSecret: config.spotify.clientSecret,
redirectUri: 'http://example.com/callback'
});
var getLatestPlaylist = function() {
return new Promise(function (fulfill, reject) {
spotifyApi.getUserPlaylists(spotifyUserId)
.then(function(data) {
var playlists = data.body.items;
var latestPlaylist;
playlists.some(function(pl) {
var match = config.spotify.searchComparison(pl);
if (match) {
latestPlaylist = pl;
}
return match;
}, this);
if (typeof latestPlaylist !== 'undefined') {
var res = {id: latestPlaylist.id, name: latestPlaylist.name, songs: []};
spotifyApi.getPlaylist(spotifyUserId, latestPlaylist.id)
.then(function(data) {
//console.log(data.body);
var tracks = data.body.tracks.items;
var songs = [];
var idx = 0;
tracks.forEach(function(o) {
var track = o.track;
var artists = [];
track.artists.forEach(function(a) {
artists.push(a.name);
})
var song = {Index: idx, Title: track.name, Artists: artists, Album: track.album.name};
songs.push(song);
idx++;
//console.log(song);
}, this);
res.songs = songs;
fulfill(res);
//console.log(songs.length);
}, function(err) {
reject(err);
});
} else {
reject('Unable to find playlist that matches criteria');
}
}, function(err) {
reject(err);
});
});
};
var syncPlaylists = function(spotifyPlaylist, playlistId, addTracks) {
console.log('Syncing ' + spotifyPlaylist.name + '...');
var searchForSong = function(spotifySong, callback) {
var searchString = (spotifySong.Title + " " + spotifySong.Artists[0]); //+ " " + spotifySong.Album);
console.log('Searching for ' + searchString + '...');
pm.search(searchString, 5, function(err, data) {
var results = data.entries.filter(function(v) {return v.type === "1"});
var song = results.sort(function(a, b) { // sort by match score
return a.score < b.score;
}).shift(); // take first song
callback(null, {Index: spotifySong.Index, Track: song.track});
});
}
async.map(spotifyPlaylist.songs, searchForSong, function(err, results) {
var sorted = results.sort(function(a, b) {
return a.Index > b.Index;
})
sorted.forEach(function(v) {
if (addTracks) {
setTimeout(function() {
pm.addTrackToPlayList(v.Track.nid, playlistId, function(err, data) {
console.log('Added ' + v.Track.title + ' to playlist...');
});
}, 150 * v.Index);
}
}, this);
});
}
spotifyApi.clientCredentialsGrant()
.then(function(data) {
console.log('The access token expires in ' + data.body['expires_in']);
console.log('The access token is ' + data.body['access_token']);
// Save the access token so that it's used in future calls
spotifyApi.setAccessToken(data.body['access_token']);
getLatestPlaylist().then(function (res){
//console.log(res);
pm.getPlayLists(function(err, data) {
var playlists = data.data.items;
var foundPlaylist;
playlists.forEach(function(pl) {
if (pl.description === res.id) {
foundPlaylist = pl;
}
}, this);
if (typeof foundPlaylist === 'undefined') {
console.log('Creating playlist');
pm.addPlayList(res.name, function(err, data) {
pm.updatePlayListMeta(data.mutate_response[0].id, {description: res.id}, function () {});
syncPlaylists(res, data.mutate_response[0].id, true);
});
} else {
console.log('Found playlist');
pm.getPlayListEntries({}, function(err, data) {
var tracks = data.data.items.filter(function(v) {return v.playlistId === foundPlaylist.id});
var trackIds = [];
tracks.forEach(function(t) {
trackIds.push(t.id);
}, this);
console.log('Removing existing ' + trackIds.length + ' playlist entries...');
pm.removePlayListEntry(trackIds, function(err, data) {
syncPlaylists(res, foundPlaylist.id, true);
});
});
}
});
}, function(err) {
console.log('Something went wrong', err);
});
}, function(err) {
console.log('Something went wrong when retrieving an access token', err);
});