forked from SwapnilSoni1999/spotify-dl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·243 lines (211 loc) · 9.08 KB
/
cli.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/usr/bin/env node
/*
Copyright (c) 2019 Swapnil Soni
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
const path = require('path');
const ora = require('ora');
const meow = require('meow');
const getLink = require('./util/get-link');
const songdata = require('./util/get-songdata');
const urlParser = require('./util/url-parser');
const filter = require('./util/filters');
const download = require('./lib/downloader');
const cache = require('./lib/cache');
const mergeMetadata = require('./lib/metadata');
const setup = require('./lib/setup');
// setup ffmpeg
setup.ffmpeg(process.platform);
const cli = meow(
`
Usage
$ spotifydl [Options] <link> …
Examples
$ spotifydl https://open.spotify.com/track/5tz69p7tJuGPeMGwNTxYuV
$ spotifydl https://open.spotify.com/playlist/4hOKQuZbraPDIfaGbM3lKI
Options
-o -takes valid path argument
eg. $ spotifydl -o ~/songs https://open.spotify.com/playlist/3PrZvfOSNShOC2JxgIhvL1
`,
{
flags: {
help: {
alias: 'h',
},
version: {
alias: 'v',
},
output: {
alias: 'o',
type: 'string'
},
spin: {
alias:'s',
type: 'boolean',
default: true
}
},
}
);
const { input } = cli;
var spinner;
if (!input[0]) {
console.log('See spotifydl --help for instructions');
process.exit(1);
}
(async () => {
if (cli.flags.spin == true) {
spinner = ora(`Searching…`).start();
}
else if (cli.flags.spin == false) {
console.log("Spinner is disabled: Remove flag -s false to enable!");
}
try {
var spotifye = new songdata();
for (const link of input) {
const urlType = await urlParser(await filter.removeQuery(link));
var songData = {};
const URL = link;
switch(urlType) {
case 'song': {
songData = await spotifye.getTrack(URL);
const songName = songData.name + songData.artists[0];
const output = path.resolve((cli.flags.output != null) ? cli.flags.output : process.cwd(), await filter.validateOutput(`${songData.name} - ${songData.artists[0]}.mp3`));
if(cli.flags.spin == true) {
spinner.info(`Saving Song to: ${output}\n`);
} else if(cli.flags.spin == false) {
console.log(`Saving Song to: ${output}\n`);
}
if(cli.flags.spin == true) {
spinner.succeed(`Song: ${songData.name} - ${songData.artists[0]}`);
} else if(cli.flags.spin == false) {
console.log(`Song: ${songData.name} - ${songData.artists[0]}`);
}
const youtubeLink = await getLink(songName);
if(cli.flags.spin == true) {
spinner.start("Downloading...");
} else if(cli.flags.spin == false) {
console.log("Downloading...");
}
await download(youtubeLink, output, (cli.flags.spin == true) ? spinner : null, async function() {
await mergeMetadata(output, songData, (cli.flags.spin == true) ? spinner : null);
});
break;
}
case 'playlist': {
var cacheCounter = 0;
songData = await spotifye.getPlaylist(URL);
if(cli.flags.spin == true) {
spinner.warn("Warning: Providing Playlist will download first 100 songs from the list. This is a drawback right now and will be fixed later.");
} else if(cli.flags.spin == false) {
console.log("Warning: Providing Playlist will download first 100 songs from the list. This is a drawback right now and will be fixed later.");
}
var dir = path.join((cli.flags.output != null) ? cli.flags.output : process.cwd(), songData.name);
if(cli.flags.spin == true) {
spinner.info(`Saving Playlist: ` + path.join( (cli.flags.output != null) ? cli.flags.output : process.cwd(), songData.name));
} else if(cli.flags.spin == false) {
console.log(`Saving Playlist: ` + path.join((cli.flags.output != null) ? cli.flags.output : process.cwd(), songData.name));
}
cacheCounter = await cache.read(dir, (cli.flags.spin == true) ? spinner : null);
dir = path.join(dir, '.spdlcache');
async function downloadLoop(trackIds, counter) {
const songNam = await spotifye.extrTrack(trackIds[counter]);
counter++;
if(cli.flags.spin == true) {
spinner.info(`${counter}. Song: ${songNam.name} - ${songNam.artists[0]}`);
} else if(cli.flags.spin == false) {
console.log(`${counter}. Song: ${songNam.name} - ${songNam.artists[0]}`);
}
counter--;
const ytLink = await getLink(songNam.name + songNam.artists[0]);
const output = path.resolve((cli.flags.output != null) ? cli.flags.output : process.cwd(), songData.name, await filter.validateOutput(`${songNam.name} - ${songNam.artists[0]}.mp3`));
if(cli.flags.spin == true) {
spinner.start("Downloading...");
} else if(cli.flags.spin == false) {
console.log("Downloading...");
}
download(ytLink, output, (cli.flags.spin == true) ? spinner : null, async function() {
await cache.write(dir, ++counter);
await mergeMetadata(output, songNam, (cli.flags.spin == true) ? spinner : null, function() {
if(counter == trackIds.length) {
console.log(`\nFinished. Saved ${counter} Songs at ${output}.`);
} else {
downloadLoop(trackIds, counter);
}
});
})
}
downloadLoop(songData.tracks, cacheCounter);
break;
}
case 'album': {
var cacheCounter = 0;
songData = await spotifye.getAlbum(URL);
songData.name = songData.name.replace('/', '-');
var dir = path.join((cli.flags.output != null) ? cli.flags.output : process.cwd(), songData.name);
if(cli.flags.spin == true) {
spinner.info(`Saving Album: ` + path.join((cli.flags.output != null) ? cli.flags.output : process.cwd(), songData.name));
} else if(cli.flags.spin == false) {
console.log(`Saving Album: ` + path.join((cli.flags.output != null) ? cli.flags.output : process.cwd(), songData.name));
}
cacheCounter = await cache.read(dir, (cli.flags.spin == true) ? spinner : null);
dir = path.join(dir, '.spdlcache');
async function downloadLoop(trackIds, counter) {
const songNam = await spotifye.extrTrack(trackIds[counter]);
counter++;
if(cli.flags.spin == true) {
spinner.info(`${counter}. Song: ${songNam.name} - ${songNam.artists[0]}`);
} else if(cli.flags.spin == false) {
console.log(`${counter}. Song: ${songNam.name} - ${songNam.artists[0]}`);
}
counter--;
const ytLink = await getLink(songNam.name + songNam.artists[0]);
const output = path.resolve((cli.flags.output != null) ? cli.flags.output : process.cwd(), songData.name, await filter.validateOutput(`${songNam.name} - ${songNam.artists[0]}.mp3`));
if(cli.flags.spin == true) {
spinner.start("Downloading...");
} else if(cli.flags.spin == false) {
console.log("Downloading...");
}
download(ytLink, output, (cli.flags.spin == true) ? spinner : null, async function () {
await cache.write(dir, ++counter);
await mergeMetadata(output, songNam, (cli.flags.spin == true) ? spinner : null, function() {
if(counter == trackIds.length) {
console.log(`\nFinished. Saved ${counter} Songs at ${output}.`);
} else {
downloadLoop(trackIds, counter);
}
});
})
}
downloadLoop(songData.tracks, cacheCounter);
break;
}
case 'artist': {
if(cli.flags.spin == true) {
spinner.warn("To download artists list, add them to a separate Playlist and download.");
} else if(cli.flags.spin == false) {
console.log("To download artists list, add them to a separate Playlist and download.");
}
break;
}
default: {
throw new Error('Invalid URL type');
}
}
}
} catch (error) {
console.log(`Something went wrong!`);
console.log(error);
process.exit(1);
}
})();
process.on('SIGINT', () => {
process.exit(1);
});