format animestorrent provider

This commit is contained in:
TheBeastLT
2021-09-14 15:09:37 +02:00
committed by TheBeastLT
parent a0fae89b81
commit 4b4cefb3c2
2 changed files with 93 additions and 121 deletions

View File

@@ -8,12 +8,12 @@ const defaultTimeout = 10000;
const maxSearchPage = 50;
const defaultProxies = [
"https://animestorrent.com"
'https://animestorrent.com'
];
const Categories = {
MOVIE: "filme",
ANIME: "tv",
MOVIE: 'filme',
ANIME: 'tv',
OVA: 'ova'
};
@@ -23,12 +23,10 @@ function torrent(torrentId, config = {}, retries = 2) {
}
const proxyList = config.proxyList || defaultProxies;
const slug = torrentId.split("/")[3];
return Promises.first(
proxyList.map((proxyUrl) => singleRequest(`${proxyUrl}/${slug}`, config))
)
.then((body) => parseTorrentPage(body))
.then((torrent) => torrent.map((el) => ({ torrentId: slug, ...el })))
.catch((err) => torrent(slug, config, retries - 1));
return Promises.first(proxyList.map((proxyUrl) => singleRequest(`${proxyUrl}/${slug}`, config)))
.then((body) => parseTorrentPage(body))
.then((torrent) => torrent.map((el) => ({ torrentId: slug, ...el })))
.catch((err) => torrent(slug, config, retries - 1));
}
function search(keyword, config = {}, retries = 2) {
@@ -40,18 +38,15 @@ function search(keyword, config = {}, retries = 2) {
const extendToPage = Math.min(maxSearchPage, config.extendToPage || 1);
const requestUrl = (proxyUrl) => `${proxyUrl}/page/${page}/?s=${keyword}`;
return Promises.first(
proxyList.map((proxyUrl) => singleRequest(requestUrl(proxyUrl), config))
)
.then((body) => parseTableBody(body))
.then((torrents) =>
torrents.length === 40 && page < extendToPage
? search(keyword, { ...config, page: page + 1 })
.catch(() => [])
.then((nextTorrents) => torrents.concat(nextTorrents))
: torrents
)
.catch((err) => search(keyword, config, retries - 1));
return Promises.first(proxyList.map((proxyUrl) => singleRequest(requestUrl(proxyUrl), config)))
.then((body) => parseTableBody(body))
.then((torrents) =>
torrents.length === 40 && page < extendToPage
? search(keyword, { ...config, page: page + 1 })
.catch(() => [])
.then((nextTorrents) => torrents.concat(nextTorrents))
: torrents)
.catch((err) => search(keyword, config, retries - 1));
}
function browse(config = {}, retries = 2) {
@@ -62,15 +57,13 @@ function browse(config = {}, retries = 2) {
const page = config.page || 1;
const category = config.category;
const requestUrl = (proxyUrl) =>
category
? `${proxyUrl}/tipo/${category}/page/${page}/`
: `${proxyUrl}/page/${page}/`;
category
? `${proxyUrl}/tipo/${category}/page/${page}/`
: `${proxyUrl}/page/${page}/`;
return Promises.first(
proxyList.map((proxyUrl) => singleRequest(requestUrl(proxyUrl), config))
)
.then((body) => parseTableBody(body))
.catch((err) => browse(config, retries - 1));
return Promises.first(proxyList.map((proxyUrl) => singleRequest(requestUrl(proxyUrl), config)))
.then((body) => parseTableBody(body))
.catch((err) => browse(config, retries - 1));
}
function singleRequest(requestUrl, config = {}) {
@@ -86,8 +79,8 @@ function singleRequest(requestUrl, config = {}) {
if (!body) {
throw new Error(`No body: ${requestUrl}`);
} else if (
body.includes("502: Bad gateway") ||
body.includes("403 Forbidden")
body.includes("502: Bad gateway") ||
body.includes("403 Forbidden")
) {
throw new Error(`Invalid body contents: ${requestUrl}`);
}
@@ -117,7 +110,7 @@ function parseTableBody(body) {
}
function parseTorrentPage(body) {
return new Promise(async(resolve, reject) => {
return new Promise(async (resolve, reject) => {
const $ = cheerio.load(body);
if (!$) {
@@ -129,18 +122,20 @@ function parseTorrentPage(body) {
magnets.push(magnet);
});
const details = $('div.infox')
const torrent = magnets.map((magnetLink) => {
const torrents = magnets.map((magnetLink) => {
return {
title: decode(magnetLink).name,
original_name: details.find('h1.entry-title').text(),
year: details.find('b:contains(\'Lançamento:\')')[0] ? details.find('b:contains(\'Lançamento:\')')[0].nextSibling.nodeValue.trim() : '',
originalName: details.find('h1.entry-title').text(),
year: details.find('b:contains(\'Lançamento:\')')[0]
? details.find('b:contains(\'Lançamento:\')')[0].nextSibling.nodeValue.trim()
: '',
infoHash: decode(magnetLink).infoHash,
magnetLink: magnetLink,
category: details.find('b:contains(\'Tipo:\')').next().attr('href').split('/')[4],
uploadDate: new Date($("time[itemprop=dateModified]").attr("datetime")),
};
})
resolve(torrent.filter((x) => x));
resolve(torrents);
});
}