-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
153 lines (122 loc) · 3.74 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
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
const fs = require("fs");
const path = require("path");
const chalk = require("chalk");
const puppeteer = require("puppeteer");
const request = require("request");
const progress = require("request-progress");
const download = ({ link, fname }) => {
return new Promise((resolve, reject) => {
let destination = path.join(process.cwd(), "Music", fname);
progress(request(link))
.on("error", err => reject(err))
.on("end", () => resolve(true))
.pipe(fs.createWriteStream(destination));
console.log(chalk.green("::: ->") + ` ${destination}\n`);
});
};
(async () => {
let browser = await puppeteer.launch({
userDataDir: path.join(process.cwd(), "tmp"),
headless: false
});
let page = await browser.newPage();
await page.emulateMedia("screen");
await page.setViewport({ width: 1280, height: 720 });
await page.setRequestInterception(true);
var interceptorCallback = req => req.continue();
page.on("request", interceptorCallback);
await page.goto("https://vk.com/", {
waitUntil: "domcontentloaded"
});
await page.waitForSelector("._audio_page_layout.audio_page_layout", {
timeout: 0
});
await page.evaluate(async () => {
await new Promise(resolve => {
var offset = -100;
let pageScroll = () => {
window.scrollBy(0, 50);
if (window.pageYOffset === offset) {
return resolve(true);
}
offset = window.pageYOffset;
setTimeout(pageScroll, 50);
};
pageScroll();
});
});
let nameList = await page.evaluate(() => {
function createIterator(array) {
var nextIndex = 0;
return {
next() {
return nextIndex < array.length ? array[nextIndex++] : false;
}
};
}
let audioList = Array.from(
document.querySelector(
"div.audio_page__audio_rows_list._audio_page__audio_rows_list._audio_pl.audio_w_covers.audio_owner_list_canedit"
).childNodes
).filter(el => !Array.from(el.classList).includes("audio_claimed"));
window.__audioIterator = createIterator(audioList);
let nameList = [];
for (let audio of audioList) {
let textArr = audio.innerText.split("\n").filter(Boolean);
let name = textArr[0] + " - " + textArr[1];
nameList.push(
name
.replace(/\//g, "")
.replace(/\\/g, "")
.replace(/\|/g, "")
.replace(/\?/g, "")
.replace(/\*/g, "")
.replace(/:/g, "")
.replace(/</g, "")
.replace(/>/g, "")
.replace(/"/g, "")
.replace(/'/g, "")
.replace(/\.$/, "")
.replace(/ $/, "")
);
}
return nameList;
});
page.off("request", interceptorCallback);
var position = 0;
var interceptorCallback = async req => {
req.continue();
if (new URL(req.url()).host.includes("vkuseraudio.net")) {
let musicType = new URL(req.url()).pathname.includes(".mp3")
? { ext: ".mp3" }
: new URL(req.url()).pathname.includes(".m3u")
? { ext: ".m3u" }
: null;
if (musicType) {
await download({
link: req.url(),
fname: nameList[position++] + musicType.ext
});
let audioRemains = await page.evaluate(() => {
let element = window.__audioIterator.next();
if (element) {
let evt = new Event("click");
element.dispatchEvent(evt);
}
return element;
});
if (!audioRemains) {
await browser.close();
}
}
}
};
page.on("request", interceptorCallback);
await page.evaluate(() => {
let element = window.__audioIterator.next();
if (element) {
let evt = new Event("click");
element.dispatchEvent(evt);
}
});
})();