forked from SwapnilSoni1999/spotify-dl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·197 lines (163 loc) · 6.45 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
#!/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');
const versionChecker = require('./util/versionChecker');
// 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'
},
},
}
);
const { input } = cli;
if (!input[0]) {
console.log('See spotifydl --help for instructions');
process.exit(1);
}
(async () => {
const update = await versionChecker();
if (update) {
console.log(update);
}
const spinner = ora(`Searching…`).start();
try {
var spotifye = new songdata();
for (const link of input) {
const urlType = await urlParser(await filter.removeQuery(link));
var songData = {};
const URL = link;
let outputDir = path.normalize((cli.flags.output != null) ? cli.flags.output : process.cwd());
switch(urlType) {
case 'song': {
songData = await spotifye.getTrack(URL);
const songName = songData.name + ' ' + songData.artists[0];
const output = path.resolve(outputDir, await filter.validateOutput(`${songData.name} - ${songData.artists[0]}.mp3`));
spinner.info(`Saving Song to: ${output}`);
spinner.succeed(`Song: ${songData.name} - ${songData.artists[0]}`);
const youtubeLink = await getLink(songName);
spinner.start("Downloading...");
await download(youtubeLink, output, spinner, async function() {
await mergeMetadata(output, songData, spinner);
});
break;
}
case 'playlist': {
var cacheCounter = 0;
songData = await spotifye.getPlaylist(URL);
var dir = path.join(outputDir, filter.validateOutputSync(songData.name));
spinner.info(`Total Songs: ${songData.total_tracks}`)
spinner.info(`Saving Playlist: ${dir}`);
cacheCounter = await cache.read(dir, spinner);
dir = path.join(dir, '.spdlcache');
async function downloadLoop(trackIds, counter) {
const songNam = await spotifye.extrTrack(trackIds[counter]);
counter++;
spinner.info(`${counter}. Song: ${songNam.name} - ${songNam.artists[0]}`);
counter--;
const ytLink = await getLink(songNam.name + ' ' + songNam.artists[0]);
const output = path.resolve(outputDir, filter.validateOutputSync(songData.name), filter.validateOutputSync(`${songNam.name} - ${songNam.artists[0]}.mp3`));
spinner.start("Downloading...");
download(ytLink, output, spinner, async function() {
await cache.write(dir, ++counter);
await mergeMetadata(output, songNam, spinner, 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(outputDir, await filter.validateOutput(songData.name));
spinner.info(`Total Songs: ${songData.total_tracks}`);
spinner.info(`Saving Album: ` + path.join(outputDir, songData.name));
cacheCounter = await cache.read(dir, spinner);
dir = path.join(dir, '.spdlcache');
async function downloadLoop(trackIds, counter) {
const songNam = await spotifye.extrTrack(trackIds[counter]);
counter++;
spinner.info(`${counter}. Song: ${songNam.name} - ${songNam.artists[0]}`);
counter--;
const ytLink = await getLink(songNam.name + ' ' + songNam.artists[0]);
const output = path.resolve(outputDir, await filter.validateOutput(songData.name, `${songNam.name} - ${songNam.artists[0]}.mp3`));
spinner.start("Downloading...");
download(ytLink, output, spinner, async function () {
await cache.write(dir, ++counter);
await mergeMetadata(output, songNam, spinner, function() {
if(counter == trackIds.length) {
console.log(`\nFinished. Saved ${counter} Songs at ${output}.`);
} else {
downloadLoop(trackIds, counter);
}
});
})
}
downloadLoop(songData.tracks, cacheCounter);
break;
}
case 'artist': {
spinner.warn("To download artists list, add them to a separate Playlist and download.");
break;
}
default: {
throw new Error('Invalid URL type');
}
}
}
} catch (error) {
spinner.fail(`Something went wrong!`);
console.log(error);
process.exit(1);
}
})();
process.on('SIGINT', () => {
process.exit(1);
});