-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrapeAlbums.ts
54 lines (46 loc) · 1.25 KB
/
scrapeAlbums.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
import * as cheerio from "npm:cheerio";
import chalk from "npm:chalk";
type Album = {
pos: number;
title: string;
};
export default async function scrapeAlbums(
url: string
): Promise<Album[] | null> {
console.log("Getting the album names from Doom Charts...");
try {
const res = await fetch(url);
if (!res.ok) {
console.error(`Error: ${res.status} - ${res.statusText}`);
return null;
}
const html = await res.text();
const $ = cheerio.load(html);
// Black magic
const content = $(".entry-content").text();
const pattern = /(^\d{1,2}\.) ([^–]+ – [^\/]+)/;
const albums = content
.split("\n")
.map((line) => {
const match = line.match(pattern);
return match
? { pos: parseInt(match[1]), title: match[2].trim() }
: null;
})
.filter(Boolean) as Album[];
// Log
albums.forEach((a) => {
console.log(`#${a.pos} - ${chalk.green(a.title)}`);
});
if (albums.length !== 40)
console.log(
`Missing ${40 - albums.length} album${
40 - albums.length > 1 ? "s" : ""
}`
);
return albums;
} catch (err) {
console.error(err);
return null;
}
}