196 lines
6.2 KiB
JavaScript
196 lines
6.2 KiB
JavaScript
const axios = require("axios");
|
|
const cheerio = require("cheerio");
|
|
const fs = require("fs");
|
|
const pb = require("progress");
|
|
|
|
module.exports = {
|
|
getType: function (url) {
|
|
if (url.includes("category/")) {
|
|
return "series";
|
|
} else if (url.includes("episode-")) {
|
|
return "episode";
|
|
} else {
|
|
return "unk";
|
|
}
|
|
},
|
|
download: async function (url, type, dest) {
|
|
switch (type) {
|
|
case "episode":
|
|
if (!dest) dest = false;
|
|
resp = await axios({
|
|
method: "GET",
|
|
url: url,
|
|
headers: {
|
|
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Firefox/91.0",
|
|
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
|
|
}
|
|
});
|
|
$ = cheerio.load(resp.data);
|
|
let src = $(".play-video > iframe").attr("src");
|
|
let fe = await getFembed(src);
|
|
fe = await fetchFe(fe);
|
|
await actuallyDownload(fe, dest);
|
|
return true;
|
|
case "series":
|
|
let eps = [];
|
|
resp = await axios({
|
|
method: "GET",
|
|
url: url,
|
|
headers: {
|
|
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Firefox/91.0",
|
|
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
|
|
}
|
|
});
|
|
$ = cheerio.load(resp.data);
|
|
let id = $("#movie_id").attr("value");
|
|
let de = $("#default_ep").attr("value");
|
|
let al = url.split("/category/")?.[1];
|
|
let se = $("#episode_page > li > .active").text();
|
|
let s = se.split("-")[0];
|
|
let e = se.split("-")[1];
|
|
url = `https://ajax.gogo-load.com/ajax/load-list-episode?ep_start=${s}&ep_end=${e}&id=${id}&default_ep=${de}&alias=${al}`;
|
|
resp = await axios({
|
|
method: "GET",
|
|
url: url,
|
|
headers: {
|
|
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Firefox/91.0",
|
|
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
|
|
}
|
|
});
|
|
$ = cheerio.load(resp.data);
|
|
$("#episode_related li a").each(async function(e) {
|
|
let ele = $("#episode_related li a")[e];
|
|
let dest = `./downloads/${$("#episode_related li a .name")[e]?.children.find(e => e.type == "text").data}.mp4`;
|
|
if (dest.startsWith(" ")) dest = dest.substring(1, dest.length);
|
|
if (ele?.attribs?.href !== null && ele?.attribs?.href !== undefined) {
|
|
if (ele.attribs.href.startsWith(" ")) ele.attribs.href = ele.attribs.href.substring(1, ele.attribs.href.length);
|
|
if (ele.attribs.href.startsWith("/")) ele.attribs.href = `http://gogoanime.gg${ele.attribs.href}`;
|
|
eps.push({
|
|
url: ele.attribs.href,
|
|
dest: dest
|
|
});
|
|
}
|
|
});
|
|
increm(eps, 0);
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
|
|
async function getFembed(src) {
|
|
try {
|
|
src = `http:${src}`;
|
|
let s = new URL(src)?.host;
|
|
let resp = await axios({
|
|
method: "GET",
|
|
url: src,
|
|
headers: {
|
|
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Firefox/91.0",
|
|
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
|
|
"Referer": "https://gogoanime.gg",
|
|
"Host": s,
|
|
"Sec-Fetch-Dest": "iframe",
|
|
"Sec-Fetch-Mode": "navigate",
|
|
"Sec-Fetch-Site": "cross-site"
|
|
}
|
|
});
|
|
let $ = cheerio.load(resp.data);
|
|
let data = $(".linkserver:not(.active)");
|
|
for (let a in data) {
|
|
if (data[a]?.attribs?.["data-video"] !== null) {
|
|
let u = new URL(data[a].attribs["data-video"]);
|
|
if (u.host == "fembed-hd.com") {
|
|
return u.href.split("/v/")?.[1];
|
|
} else {
|
|
continue;
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
} catch(e) {
|
|
console.log(e.stack || e.message || e, src);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
async function fetchFe(id) {
|
|
try {
|
|
let resp = await axios({
|
|
method: "POST",
|
|
url: `https://fembed-hd.com/api/source/${id}`,
|
|
headers: {
|
|
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Firefox/91.0",
|
|
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
|
|
"Referer": "https://fembed-hd.com",
|
|
"Sec-Fetch-Dest": "iframe",
|
|
"Sec-Fetch-Mode": "navigate",
|
|
"Sec-Fetch-Site": "cross-site"
|
|
}
|
|
});
|
|
if (resp.data.success == true) {
|
|
return resp.data.data[resp.data.data.length - 1]?.file
|
|
} else {
|
|
console.log("Error retrieving fembed download link", resp.data);
|
|
return null;
|
|
}
|
|
} catch(e) {
|
|
console.log(e.stack || e.message || e, id);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
async function actuallyDownload(src, dest) {
|
|
try {
|
|
if (!dest) dest = "./downloads/video.mp4";
|
|
if (dest.startsWith("./downloads/")) {
|
|
if (!fs.existsSync("./downloads/")) fs.mkdirSync("./downloads");
|
|
}
|
|
let {data, headers} = await axios({
|
|
responseType: "stream",
|
|
onDownloadProgress: function (progressEvent) {
|
|
console.log(progressEvent)
|
|
},
|
|
url: src
|
|
});
|
|
|
|
let tl = parseInt(headers["content-length"]);
|
|
|
|
let p = new pb(`downloading to ${dest} [:bar] :percent :rate/bps`, {
|
|
renderThrottle: 1,
|
|
total: tl
|
|
});
|
|
|
|
data.on("data", function(ch) {
|
|
p.tick(ch.length);
|
|
if (p.complete) {
|
|
console.log(`${dest} has completed!`)
|
|
}
|
|
});
|
|
data.pipe(fs.createWriteStream(dest));
|
|
} catch(e) {
|
|
console.log(e.stack || e.message || e, src);
|
|
} finally {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
async function increm(array, index) {
|
|
if (!index) index = 0;
|
|
if (array[index]) {
|
|
console.log("- Now starting episode", array[index].dest);
|
|
let resp = await axios({
|
|
method: "GET",
|
|
url: array[index].url,
|
|
headers: {
|
|
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Firefox/91.0",
|
|
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
|
|
}
|
|
});
|
|
let $ = cheerio.load(resp.data);
|
|
let src = $(".play-video iframe").attr("src");
|
|
let fe = await getFembed(src);
|
|
fe = await fetchFe(fe);
|
|
if (fe !== null) await actuallyDownload(fe, array[index].dest);
|
|
increm(array, (index + 1));
|
|
}
|
|
} |