-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (58 loc) · 1.59 KB
/
index.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
const axios = require("axios").default;
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const fs = require("fs").promises;
const BASIC_URL = `https://radio7.ru/playlist/2021/10/`;
const urls = [];
const songs = [];
for (let day = 1; day < 30; day++) {
for (let hour = 0; hour < 24; hour++) {
urls.push(
`${BASIC_URL}${day < 10 ? `0${day}` : day}/${
hour < 10 ? `0${hour}` : hour
}:00`
);
}
}
(async function () {
for await (const [i, url] of urls.entries()) {
console.log(`Now: ${i} Remain: ${urls.length - i - 1}`);
try {
const response = await axios.get(url);
if (!response.data || response.status > 300) {
throw new Error(`${response.status} Error`);
}
const dom = new JSDOM(response.data);
songs.push(parsePage(dom.window.document));
if (i === urls.length - 1) {
await fs.writeFile(
`${__dirname}/songs_array.txt`,
JSON.stringify(Array.from(new Set(songs.flat())))
);
await fs.writeFile(
`${__dirname}/songs.txt`,
Array.from(new Set(songs.flat())).join("\n")
);
}
} catch (e) {
console.error(e.message);
}
}
})();
function parsePage(document) {
const arr = [];
try {
const list = Array.from(document.querySelectorAll(`ul.alt-play-list li`));
for (const el of list) {
title = el.querySelector(`span.title`);
if (!title) continue;
title = title.textContent.trim();
if (!title) continue;
arr.push(title);
}
} catch (e) {
console.error(e.message);
return [];
}
return arr;
}