-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths2d.js
84 lines (66 loc) · 2.34 KB
/
s2d.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
const http = require('http');
const readline = require('readline');
const cp = require("clipboardy");
const APIURL = "http://spotizr.com/";
const deezerURL = "https://www.deezer.com/";
const sanitizePlaylistURL = playlist => {
const index = playlist.indexOf("?si=");
if (index != -1)
playlist = playlist.substring(0, index);
return playlist;
}
const parseArguments = () => {
const exp = /https?:\/\/open.spotify.com(\/user\/\w+)?\/playlist\/\w+(\?si=\w+)?/;
if (process.argv.includes('-h'))
console.log("USAGE\n\ts2d <playlist_url>\nOPTIONS\n\t--print (-p): Prints the output");
else if (process.argv[2] && process.argv[2].match(exp))
convert(sanitizePlaylistURL(process.argv[2]));
else {
const rl = readline.createInterface(process.stdin, process.stdout);
process.stdin.setRawMode(true);
process.stdin.on('keypress', (chunk, key) => {
if (key && key.ctrl && key.name == 'v')
rl.write(cp.readSync());
});
rl.question('Enter playlist URL: ', input => {
if (input.match(exp))
convert(sanitizePlaylistURL(input));
else {
console.log("Not a valid playlist URL");
setTimeout(() => { process.exit() }, 1500);
}
rl.close();
});
}
}
const convert = async playlist => {
const playlistData = await getTracks(playlist);
const tracks = playlistData.ids;
let output = "";
tracks.forEach(track => {
output += `${deezerURL}track/${track};`;
});
output = output.substring(0, output.length - 1);
cp.writeSync(output);
console.log("\nCopied to clipboard!");
setTimeout(() => { process.exit() }, 1500);
if (process.argv.includes("-p") || process.argv.includes("--print"))
console.log(output);
}
const getTracks = playlist => {
return new Promise(resolve => {
http.get(`${APIURL}?spurl=${playlist}`, res => {
let data = "";
res.on('data', chunk => {
data += chunk;
});
res.on('end', () => {
resolve(JSON.parse(data));
});
});
});
}
parseArguments();
process.on('beforeExit', () => {
setTimeout(() => { }, 1500);
});