-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
75 lines (69 loc) · 1.91 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 axios = require("axios");
const { JSDOM } = require("jsdom");
const baseDomain = "https://w1.anime4up.com/";
function urlType(url) {
if (url.startsWith(`${baseDomain}anime`)) return "anime";
if (url.startsWith(`${baseDomain}episode`)) return "episode";
else return "provide a valid url ( episode or anime )";
}
function getLinks(dom, quality) {
if (quality === "1080")
return dom.window.document.querySelector(".dw-online a[href$=download_o]")
.href;
if (quality === "720")
return dom.window.document.querySelector(".dw-online a[href$=download_h]")
.href;
if (quality === "480")
return dom.window.document.querySelector(".dw-online a[href$=download_n]")
.href;
if (quality === "360")
return dom.window.document.querySelector(".dw-online a[href$=download_l]")
.href;
}
function anLinks(dom) {
const eps = [];
const nodeList = dom.window.document.querySelectorAll(
".episodes-card-title a[href^='https://w1.anime4up.com/episode/']"
);
nodeList.forEach((el) => {
eps.push(el.href);
});
return eps;
}
function getDownLink(url) {
var config = {
method: "get",
url: url,
headers: {
referer: "https://w1.anime4up.com/",
responseType: "stream",
},
maxContentLength: 1,
};
const epHtml = axios(config).catch(function(error) {
return error.request.res.responseUrl;
});
return epHtml;
}
async function anScraper(dom, quality) {
const animeEps = anLinks(dom);
const downLinks = [];
for (let i = 0; i < animeEps.length; i++) {
const html = await axios.get(animeEps[i]);
const newDom = new JSDOM(html.data);
console.log(
`Working on Ep ${i + 1} , Eps Left ${animeEps.length - (i + 1)}`
);
let one = await getLinks(newDom, quality);
let two = await getDownLink(one);
downLinks.push(two);
}
return downLinks;
}
module.exports = {
urlType,
getLinks,
anLinks,
getDownLink,
anScraper,
};