-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.ts
60 lines (51 loc) · 1.82 KB
/
app.ts
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
/// <reference path="typings/minimist/minimist.d.ts" />
/// <reference path="DeezerTrack.ts" />
/// <reference path="DeezerClient.ts" />
import readline = require("readline");
import fs = require("fs");
import minimist = require("minimist");
import deezerTrack = require("./DeezerTrack");
import deezerClient = require("./DeezerClient");
// register your app on Deezer Developers portal
// https://developers.deezer.com/myapps
var appId = "";
var appSecret = "";
var argv = <any>minimist(process.argv.slice(2));
if (!argv.inputFile) {
console.log("Missing argument -inputFile path");
process.exit(-1);
}
if (!argv.playlistName) {
console.log("Missing argument -playlistName");
process.exit(-1);
}
var tracks: deezerTrack.DeezerTrack[] = [];
var deezer = new deezerClient.DeezerClient(appId, appSecret);
deezer.login(() => {
deezer.createPlaylist(argv.playlistName, (playlistId) => {
importFileToPlaylist(playlistId);
});
});
function importFileToPlaylist(playlistId: string) {
// open the file given as parameter and read it line by line
var rl = readline.createInterface(<any>{ input: fs.createReadStream(argv.inputFile) });
rl.on('line', line => { tracks.push(new deezerTrack.DeezerTrack(line)); });
// when we read end of file, search for all tracks
rl.on('close', () => {
console.log("Ready to search " + tracks.length + " track(s)");
var i = 0;
function searchTrack() {
var track = tracks[i];
deezer.search(track, () => {
i++;
if (i < tracks.length) {
// recursive call to look for next track
searchTrack();
} else {
deezer.addTrackToPlaylist(tracks, playlistId);
}
});
}
searchTrack();
});
}