-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
79 lines (63 loc) · 2.32 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
import puppeteer from "puppeteer"
import argsParser from "args-parser"
import { mkdirSync, createWriteStream } from "node:fs"
import { Readable } from "node:stream"
import { finished } from "node:stream/promises"
import { ReadableStream } from 'stream/web'
import sanitize from "sanitize-filename"
(async() => {
const commandLineArguments = argsParser(process.argv);
const requestedPublisher = commandLineArguments.publisher as string;
const requestedAlbums = commandLineArguments.albums.split(",") as Array<string>;
console.log("Requested publisher:", requestedPublisher);
console.log("Requested albums:", requestedAlbums.join(", "));
const browser = await puppeteer.launch({
headless: true,
defaultViewport: null,
args: [
'--disable-infobars',
'--user-agent=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36',
'--no-sandbox',
]
});
const [page] = await browser.pages();
await page.close();
const assembledAlbumUrls = requestedAlbums.map(requestedAlbum => {
return `https://${requestedPublisher}.bandcamp.com/album/${requestedAlbum}`
});
const albums = new Array<{ title: string, tracks: Array<{ title: string, file: string }> }>();
for (const assembledAlbumUrl of assembledAlbumUrls) {
console.log(assembledAlbumUrl)
const page = await browser.newPage();
await page.goto(assembledAlbumUrl);
const playlist = await page.evaluate(() => {
// @ts-ignore
return Player.tralbum_data.trackinfo;
});
albums.push({
title: (await page.title()).split('|')[0].trim(),
tracks: playlist.map((track: any) => {
return {
title: track.title,
file: track.file['mp3-128'],
}
})
});
console.log(JSON.stringify(albums));
}
for (const album of albums) {
mkdirSync(sanitize(album.title));
for (const track of album.tracks) {
const stream = createWriteStream(`${sanitize(album.title)}/${sanitize(track.title)}.mp3`);
const { body } = await fetch(track.file);
if (!body) {
console.log(track.title + ' fail')
return;
}
await finished(Readable.fromWeb(body as ReadableStream<any>).pipe(stream));
stream.close();
console.log(album.title, "—", track.title);
}
}
await browser.close();
})()