-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotify.js
225 lines (197 loc) · 6.91 KB
/
spotify.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
var Promise = require('bluebird');
var fs = Promise.promisifyAll(require('fs'));
var util = require('util');
var http = require('http');
var levenshtein = require('fast-levenshtein');
var PlayMusic = require('playmusic');
var colors = require('colors');
var querystring = require('querystring');
var yargs = require('yargs').argv;
var SPOTIFY = "ws.spotify.com";
var SPOTIFY_URL = "/lookup/1/.json?uri=";
var errorlog = [];
//
// Prototype extension for PlayMusic to delete a Playlist
//
PlayMusic.prototype.deletePlaylist = function (playlistId, callback) {
var that = this;
var mutations = [
{
"delete" : playlistId
}
];
this.request({
method: "POST",
contentType: "application/json",
url: this._baseURL + 'playlistbatch?' + querystring.stringify({alt: "json"}),
data: JSON.stringify({"mutations": mutations})
}, function(err, body) {
callback(err ? new Error("error deleting playlist " + err) : null, body);
});
};
//
// Promise after added method
//
var pm = Promise.promisifyAll(new PlayMusic());
if (!yargs.file || !yargs.playlist || !yargs.email || !yargs.playlist) {
return console.error('Usage: spotify.js --file=csv --playlist=name --email=email --password=password [--concurrency=10]');
}
//
// config
//
var config = {email: yargs.email, password: yargs.password, concurrency: 30};
if (yargs.concurrency) {
config.concurrency = yargs.concurrency;
}
var prequest = Promise.method(function(options, maxRetries) {
if (typeof maxRetries === 'undefined') {
maxRetries = 0;
}
return new Promise(function(resolve, reject) {
var request = http.get(options, function(response) {
// Bundle the result
var result = {
'httpVersion': response.httpVersion,
'httpStatusCode': response.statusCode,
'headers': response.headers,
'body': '',
'trailers': response.trailers,
};
// Build the body
response.on('data', function(chunk) {
result.body += chunk;
});
response.on('end', () => { resolve(result); });
}).on('error', function(error) {
if (maxRetries <= 0) {
console.log('Problem with request:', error.message);
reject(error);
} else {
console.log('Retrying %s'.red, options.path);
return prequest(options, maxRetries - 1);
}
});
});
});
//
// accepts json response from call to getPlayLists. Filters by name
//
var deletePlaylist = function(gmusicPlaylists, filter) {
if (!gmusicPlaylists || !gmusicPlaylists.data || !gmusicPlaylists.data.items) {
return;
}
gmusicPlaylists.data.items.filter((a) => { return a.name == filter }).forEach((val) => {
console.log('Deleting playlist: %s'.yellow, filter);
return pm.deletePlaylistAsync(val.id).done();
});
};
//
// gets spotify line and requests track information from spotify web service. continues the chain
// by getting matching song from Google Play Music and adding closest match to Playlist
//
var getTrackListing = function(line) {
//
// translate web spotify track to web service
//
line = line.replace('https://open.spotify.com/track/', 'spotify:track:');
var options = {
hostname: SPOTIFY,
path: SPOTIFY_URL + line,
headers: {
'Connection': 'keep-alive'
}
};
return prequest(options, 3)
.then((data) => {
try {
var listing = JSON.parse(data.body);
} catch (e) {
console.log(data);
throw e;
}
return info = { artist: listing.track.artists[0].name, album: listing.track.album.name, track: listing.track.name, spotify: line };
})
.then((song) => {
return getBestMatch(song);
})
.then((match) => {
if (match && match.track) {
console.log('Adding: %s - %s - %s to Playlist %s'.green, match.track.artist, match.track.album, match.track.title, yargs.playlist);
return pm.addTrackToPlayListAsync(match.track.nid, config.playlistId);
} else {
console.log('Song not found: %s - %s - %s'.red, match.song.artist, match.song.album, match.song.track);
errorlog.push(util.format('Song not found: %s - %s - %s'.red, match.song.artist, match.song.album, match.song.track));
return;
}
})
.catch((e) => {
console.log('Error requesting Spotify track info: %s'.red, e);
});
}
var lv = Promise.promisify(levenshtein.get);
var getBestMatch = function(listing) {
//
// sanitize track names for better matching
//
listing.track = listing.track.replace(/-/g, ' ');
listing.track = listing.track.replace(/remastered/i, '');
listing.track = listing.track.replace(/\(\s?[0-9]{4}\)/, '');
return pm.searchAsync(listing.artist + ' ' + listing.track, 10)
.then((x) => {
if (!x || !x.entries) return {song: listing};
var match = x.entries.filter((entry) => entry.type == '1')
.sort((a, b) => lv(listing.artist, a.track.artist) - lv(listing.artist, b.track.artist))
.sort((a, b) => lv(listing.track, a.track.title) - lv(listing.track, b.track.title))
.sort((a, b) => {
var alive = a.track.album.search(/live/i);
var blive = b.track.album.search(/live/i);
if (alive == -1 && blive >= 0) {
return -1
}
if (alive >= 0 && blive == -1) {
return 1;
}
return 0;
})
.shift();
if (!match) {
match = {};
}
match.song = listing;
return match;
});
}
//
// begin chain. See README for instructions
//
pm.loginAsync(config)
.then((login) => {
config.masterToken = login.masterToken;
return pm.initAsync(config);
})
.then(() => {
return pm.getPlayListsAsync();
})
.then((response) => {
return deletePlaylist(response, yargs.playlist);
})
.then(() => {
return pm.addPlayListAsync(yargs.playlist);
})
.then((re) => {
config.playlistId = re.mutate_response[0].id;
return fs.readFileAsync(yargs.file);
})
.then((data) => {
return data.toString().trim(/\r\n|\n/).split(/\n|\r\n/).filter((x) => x != undefined);
})
.map(getTrackListing, {concurrency: config.concurrency})
.then(() => {
errorlog.forEach((x) => {
console.log(colors.red(x));
})
})
.catch((err) => {
return console.error(err);
})
.done();