-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
388 lines (354 loc) · 11.7 KB
/
index.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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#!/usr/bin/env tsx
import axios from "axios";
import chalk from "chalk";
import { MultiBar } from "cli-progress";
import fs from "fs-extra";
import NodeID3, { Tags } from "node-id3";
import os from "os";
import PQueue from "p-queue";
import path from "path";
import sanitizeFilename from "sanitize-filename";
import invariant from "tiny-invariant";
import { URL } from "url";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
const queue = new PQueue({ concurrency: 25 });
const mulbar = new MultiBar({
format:
"{bar} | {percentage}% | {value}/{total} | Successful: {success} | Failures: {failure}",
barCompleteChar: "\u2588",
barIncompleteChar: "\u2591",
hideCursor: true,
// only the bars will be cleared, not the logged content
clearOnComplete: true,
stopOnComplete: true,
// important! redraw everything to avoid "empty" completed bars
forceRedraw: true,
});
// Reusing the function to get Spotify Access Token
let accessToken: string | null = null;
async function getSpotifyAccessToken() {
if (accessToken) {
return accessToken;
}
try {
const response = await axios.get(
"https://open.spotify.com/get_access_token"
);
accessToken = response.data.accessToken;
return accessToken;
} catch (error) {
console.error("Error fetching Spotify access token:", error);
throw new Error("Failed to get access token");
}
}
type SpotifyTrack = {
id: string;
name: string;
artist: string;
};
type SpotifyResource = {
id: string;
tracks: SpotifyTrack[];
} & (
| { type: "album"; title: string; artist: string }
| { type: "playlist"; name: string; owner: string }
);
const getArtistFromArtists = (artists: Record<string, any>[]) =>
artists.map((artist) => artist.name).join(", ");
function getTracks(items: Record<string, any>[]) {
return items.map(({ id, name, artists, track }) =>
id
? { id, name, artist: getArtistFromArtists(artists) }
: {
id: track.id,
name: track.name,
artist: getArtistFromArtists(track.artists),
}
);
}
const getTrackName = (track: SpotifyTrack) => `${track.name} - ${track.artist}`;
const padNumber = (number: number, reference: number): string => {
const referenceLength = reference.toString().length;
return number.toString().padStart(referenceLength, "0");
};
// Function to get album or playlist info
async function getSpotifyResource(
type: SpotifyResource["type"],
id: string
): Promise<SpotifyResource> {
try {
invariant(
["album", "playlist"].includes(type),
`Unexpected type '${type}'`
);
const token = await getSpotifyAccessToken();
const { data } = await axios.get(
`https://api.spotify.com/v1/${type}s/${id}`,
{
headers: {
Authorization: `Bearer ${token}`,
},
}
);
const tracks = getTracks(data.tracks.items);
let next = data.tracks.next;
while (next) {
const { data } = await axios.get(next, {
headers: {
Authorization: `Bearer ${token}`,
},
});
tracks.push(...getTracks(data.items));
next = data.next;
}
if (type === "album") {
return {
type: "album",
id: data.id,
title: data.name,
artist: getArtistFromArtists(data.artists),
tracks,
};
}
return {
type: "playlist",
id: data.id,
name: data.name,
owner: data.owner.display_name,
tracks,
};
} catch (error) {
console.error(`Error fetching Spotify ${type} info:`, error);
throw new Error(`Failed to fetch ${type} info`);
}
}
// Function to parse the Spotify URL and extract the type (album/playlist) and ID
function parseSpotifyUrl(urlString: string): {
type: "album" | "playlist";
id: string;
} {
const url = new URL(urlString);
if (!url.hostname.includes("spotify.com")) {
throw new Error("Invalid domain. Only Spotify URLs are supported.");
}
const pathParts = url.pathname.split("/");
const type = pathParts[1] as "album" | "playlist";
const id = pathParts[2];
if ((type === "album" || type === "playlist") && id) {
return { type, id };
} else {
throw new Error(
"Invalid Spotify URL. Must be a valid album or playlist URL."
);
}
}
// Function to determine the default downloads folder based on OS
function getDefaultDownloadPath(): string {
const platform = os.platform();
const homeDir = os.homedir();
if (platform === "win32") {
return path.join(homeDir, "Downloads");
} else if (platform === "darwin") {
return path.join(homeDir, "Downloads");
} else {
return path.join(homeDir, "Downloads");
}
}
async function downloadTrack(
track: SpotifyTrack,
saveTo: string,
index?: number,
lastIndex?: number
) {
try {
// Send the API request
const response = await axios.get(
`https://api.spotifydown.com/download/${track.id}`,
{
headers: {
Host: "api.spotifydown.com",
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0",
Accept: "*/*",
"Accept-Language": "en-US,en;q=0.5",
"Accept-Encoding": "gzip",
Referer: "https://spotifydown.com/",
Origin: "https://spotifydown.com",
DNT: "1",
Connection: "keep-alive",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-site",
"Sec-GPC": "1",
TE: "trailers",
},
}
);
if (response.data.success && response.data.metadata) {
const { metadata, link } = response.data;
const { title, artists, cover } = metadata;
// Define the filename based on the title and artist
const filename = sanitizeFilename(
`${
index !== undefined ? `${padNumber(index + 1, lastIndex!)} - ` : ""
}${getTrackName(track)}.mp3`
);
// Download the MP3 file
const mp3Response = await axios.get(link, {
responseType: "arraybuffer",
});
const mp3Buffer = Buffer.from(mp3Response.data);
// Download the album cover image
const coverResponse = await axios.get(cover, {
responseType: "arraybuffer",
});
const coverBuffer = Buffer.from(coverResponse.data);
// Save the MP3 file to disk
const filePath = `${saveTo}/${filename}`;
await fs.writeFile(filePath, mp3Buffer);
// Embed album cover in the MP3 file
const tags: Tags = {
title,
artist: artists,
album: metadata.album,
image: {
mime: "image/jpeg",
type: { id: 3 },
imageBuffer: coverBuffer,
description: "front cover",
},
...(index !== undefined && { trackNumber: `${index + 1}` }),
};
const success = NodeID3.write(tags, filePath);
if (!success) {
throw new Error(`Failed to tag the MP3 file: ${filename}`);
}
} else {
throw new Error("Failed to retrieve track metadata");
}
} catch (error) {
throw new Error(
axios.isAxiosError(error) ? error.message : (error as Error).message
);
}
}
// CLI Setup using yargs
yargs(hideBin(process.argv))
.command<{ url: string; path?: string }>(
"$0 <url>",
"Download tracks from a Spotify album or playlist",
(yargs) => {
yargs
.positional("url", {
describe: "Spotify album or playlist URL",
type: "string",
demandOption: true,
})
.option("path", {
alias: "p",
describe: "Custom folder path (absolute) for downloads",
type: "string",
default: getDefaultDownloadPath(),
});
},
async (argv) => {
const { url, path: customPath } = argv;
try {
const { type, id } = parseSpotifyUrl(url);
// console.log(`Fetching data for ${type} with ID: ${id}`);
const data = await getSpotifyResource(type, id);
// console.log(`${type} info retrieved successfully!`);
const subPath =
data.type === "album"
? `albums/${sanitizeFilename(`${data.artist} - ${data.title}`)}`
: `playlists/${sanitizeFilename(`${data.owner} - ${data.name}`)}`;
const fullPath = path.resolve(`${customPath}/spotify-dl/${subPath}`);
await fs.ensureDir(fullPath);
console.log(`Saving files to: ${chalk.bold(chalk.blue(fullPath))}`);
console.log(`Downloading Spotify resource:`);
console.log(` Type : ${chalk.bold(chalk.blue(data.type))}`);
console.log(` Id : ${chalk.bold(chalk.blue(data.id))}`);
if (data.type === "album") {
console.log(` Title : ${chalk.bold(chalk.blue(data.title))}`);
console.log(` Artist : ${chalk.bold(chalk.blue(data.artist))}`);
} else {
console.log(` Name : ${chalk.bold(chalk.blue(data.name))}`);
console.log(` Owner : ${chalk.bold(chalk.blue(data.owner))}`);
}
console.log(` Tracks : ${chalk.bold(chalk.blue(data.tracks.length))}`);
const maxRetries = 5;
const downloadStatus = {
expectedCount: data.tracks.length,
success: [] as SpotifyTrack[],
failures: [] as SpotifyTrack[],
};
console.log("");
const bar = mulbar.create(data.tracks.length, 0, {
success: 0,
failure: 0,
});
await queue.addAll(
data.tracks.map((track, index) => async () => {
const trackName = getTrackName(track);
const logStr = `(#${track.id}) ${trackName}`;
let attempt = 0;
while (attempt < maxRetries) {
try {
await downloadTrack(
track,
fullPath,
index,
data.tracks.length - 1
);
mulbar.log(chalk.green(`✓ ${logStr}\n`));
downloadStatus.success.push(track);
break; // Exit loop if successful
} catch (error) {
attempt++;
// mulbar.log(
// chalk.red(
// `(${attempt}/${maxRetries}) Error downloading track #${
// track.id
// } "${trackName}": ${(error as Error).message}\n`
// )
// );
if (attempt === maxRetries) {
mulbar.log(chalk.red(`x ${logStr}\n`));
mulbar.log(
chalk.red(
`Max retries reached. Last error: ${
(error as Error).message
}\n`
)
);
downloadStatus.failures.push(track);
} else {
// Optional: Exponential backoff or other delay strategies
await new Promise((res) => setTimeout(res, 1000));
}
} finally {
const { expectedCount, success, failures } = downloadStatus;
// console.log(
// `\r(${success.length}/${failures.length}/${expectedCount}) Downloading ...`
// );
bar.update(success.length + failures.length, {
success: success.length,
failure: failures.length,
});
if (bar.getProgress() === 1) {
mulbar.log(chalk.green("\n(Download Summary)\n"));
mulbar.log(chalk.green(` Total Count: ${expectedCount}\n`));
mulbar.log(chalk.green(` Successful : ${success.length}\n`));
mulbar.log(chalk.red(` Failures : ${failures.length}\n`));
}
}
}
})
);
} catch (error) {
console.error("Error:", (error as Error).message);
}
}
)
.help().argv;