-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.js
executable file
·75 lines (68 loc) · 2.35 KB
/
utils.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
const crunchyBase = "https://www.crunchyroll.com/";
const episodesRegex = /^(?:[1-9]{1}[0-9]{0,2}(?:-[1-9]{1}[0-9]{0,2})?){1,}$/;
const cheerio = require("cheerio");
const puppeteer = require("puppeteer");
let browser = false;
module.exports = {
async fetchData(show) {
if (!browser) browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setRequestInterception(true);
page.on("request", (req) => {
if (
["stylesheet", "font", "image", "script", "xhr"].includes(
req.resourceType()
)
) {
req.abort();
} else {
req.continue();
}
});
await page.setUserAgent(
"5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
);
await page.goto(`${crunchyBase}${show}?skip_wall=1`);
let text = await page.content();
await page.close();
const $ = cheerio.load(text);
let title = $("#showview-content-header .ellipsis span").text();
const hasSeasons = !!$("li.season").length;
const seasons = $("li.season a.season-dropdown")
.map((i, el) => $(el).attr("title"))
.get()
.filter((name) => !name.toLowerCase().includes("dub"))
.filter((name) => !name.endsWith(")"))
.filter((name) => !name.includes("Promotional"))
.reverse();
if (hasSeasons && !seasons.length) return { title, seasons: [title] };
if (!seasons.length) return false;
return { title, seasons };
},
async fetchSearch() {
if (!browser) {
console.log("Launching browser");
browser = await puppeteer.launch();
}
const page = await browser.newPage();
await page.setUserAgent(
"5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
);
const res = await page.goto(
`${crunchyBase}ajax/?req=RpcApiSearch_GetSearchCandidates`
);
let data = await res.text();
data = JSON.parse(data.replace("/*-secure-\n", "").replace("\n*/", ""));
await page.close();
console.log("Loaded search data.");
if (data && data.result_code == 1) {
return data.data
.filter((d) => d.type == "Series")
.map((d) => ({ name: d.name, link: d.link.replace("/", "") }));
} else {
return false;
}
},
crunchyBase: crunchyBase,
episodesRegex: episodesRegex,
};