-
Notifications
You must be signed in to change notification settings - Fork 1
/
get-downloads.js
106 lines (96 loc) · 2.63 KB
/
get-downloads.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
const fs = require("fs");
const path = require("path");
const puppeteer = require("puppeteer");
const MAX_PAGE = 5;
const FILE_PATH = path.join(path.resolve(), process.argv[2]);
const WRITE_STREAM = fs.createWriteStream(
path.join(path.dirname(FILE_PATH), "download-links.txt"),
{ flags: "wx" }
);
let START_INDEX = +process.argv[3] - 1;
let END_INDEX = +process.argv[4];
if (!fs.existsSync(FILE_PATH)) {
throw new Error(" Error with the episode link files");
}
if (
isNaN(START_INDEX) ||
isNaN(END_INDEX) ||
START_INDEX < 0 ||
END_INDEX < START_INDEX ||
END_INDEX <= 0
) {
START_INDEX = 0;
END_INDEX = 1000;
console.log("Error with entered start index and end index");
console.log(
`Setting start Index ${START_INDEX} and end index to ${END_INDEX}`
);
}
(async () => {
const links = fs
.readFileSync(FILE_PATH, "utf-8")
.split("\n")
.filter(Boolean)
.reverse();
const browser = await puppeteer.launch();
const urls = [];
const pages = links.slice(START_INDEX, END_INDEX);
console.log("Total number download links to be fetched", pages.length);
try {
while (pages.length > 0) {
const newUrls = await Promise.all(
pages
.splice(0, MAX_PAGE)
.map((link, index) => downloadLink(browser, link, index))
);
urls.push(...newUrls);
for (const { url, name } of newUrls) {
filename = path.join(path.dirname(FILE_PATH), name) + ".mp4";
WRITE_STREAM.write(filename + " " + url + "\n");
}
console.log(urls.length, "number of urls fetched");
}
} catch (err) {
console.log(err);
}
console.log("Total urls fetched", urls.length);
WRITE_STREAM.end();
browser.close();
})();
async function downloadLink(browser, link, index) {
const page = await browser.newPage();
await page.goto(link);
await page.setRequestInterception(true);
let url;
page.on("request", (interceptedRequest) => {
if (interceptedRequest.url().includes(".m3u8")) {
url = interceptedRequest.url();
}
interceptedRequest.continue();
});
await page.screenshot({
path: `${index}-screenshot-intial.png`,
fullPage: true,
});
await page.click("div.click-to-load");
await page.waitForTimeout(20000);
await page.screenshot({
path: `${index}-screenshot-afterClicked.png`,
fullPage: true,
});
if (!url) {
throw new Error(`${index} No url found, took too much time executing`);
}
const name = await page.evaluate(() =>
document
.getElementsByTagName("h1")[0]
.innerText.toString()
.replace(/\n/g, " ")
.replace(/\s/g, "_")
);
await page.close();
return {
name,
url,
};
}