format comoeubaixo provider

This commit is contained in:
TheBeastLT
2021-09-14 15:13:39 +02:00
committed by TheBeastLT
parent 58a0e8286f
commit 50882c30a9
2 changed files with 82 additions and 132 deletions

View File

@@ -3,7 +3,8 @@ const cheerio = require("cheerio");
const decode = require('magnet-uri'); const decode = require('magnet-uri');
const Promises = require('../../lib/promises'); const Promises = require('../../lib/promises');
const { escapeHTML } = require('../../lib/metadata'); const { escapeHTML } = require('../../lib/metadata');
const { getRandomUserAgent } = require("../../lib/requestHelper"); const { getRandomUserAgent } = require('../../lib/requestHelper');
const { isPtDubbed, sanitizePtName, sanitizePtLanguages } = require('../scraperHelper')
const defaultTimeout = 10000; const defaultTimeout = 10000;
const maxSearchPage = 50 const maxSearchPage = 50
@@ -26,7 +27,7 @@ function torrent(torrentId, config = {}, retries = 2) {
const proxyList = config.proxyList || defaultProxies; const proxyList = config.proxyList || defaultProxies;
const slug = torrentId.split("/")[3]; const slug = torrentId.split("/")[3];
return Promises.first(proxyList return Promises.first(proxyList
.map((proxyUrl) => singleRequest(`${proxyUrl}/${slug}/`, config))) .map((proxyUrl) => singleRequest(`${proxyUrl}/${slug}/`, config)))
.then((body) => parseTorrentPage(body)) .then((body) => parseTorrentPage(body))
.then((torrent) => torrent.map(el => ({ torrentId: slug, ...el }))) .then((torrent) => torrent.map(el => ({ torrentId: slug, ...el })))
.catch((err) => torrent(slug, config, retries - 1)); .catch((err) => torrent(slug, config, retries - 1));
@@ -42,7 +43,7 @@ function search(keyword, config = {}, retries = 2) {
const requestUrl = proxyUrl => `${proxyUrl}/${keyword}/${page}/` const requestUrl = proxyUrl => `${proxyUrl}/${keyword}/${page}/`
return Promises.first(proxyList return Promises.first(proxyList
.map(proxyUrl => singleRequest(requestUrl(proxyUrl), config))) .map(proxyUrl => singleRequest(requestUrl(proxyUrl), config)))
.then(body => parseTableBody(body)) .then(body => parseTableBody(body))
.then(torrents => torrents.length === 40 && page < extendToPage .then(torrents => torrents.length === 40 && page < extendToPage
? search(keyword, { ...config, page: page + 1 }).catch(() => []) ? search(keyword, { ...config, page: page + 1 }).catch(() => [])
@@ -61,7 +62,7 @@ function browse(config = {}, retries = 2) {
const requestUrl = proxyUrl => category ? `${proxyUrl}/${category}/${page}/` : `${proxyUrl}/${page}/`; const requestUrl = proxyUrl => category ? `${proxyUrl}/${category}/${page}/` : `${proxyUrl}/${page}/`;
return Promises.first(proxyList return Promises.first(proxyList
.map((proxyUrl) => singleRequest(requestUrl(proxyUrl), config))) .map((proxyUrl) => singleRequest(requestUrl(proxyUrl), config)))
.then((body) => parseTableBody(body)) .then((body) => parseTableBody(body))
.catch((err) => browse(config, retries - 1)); .catch((err) => browse(config, retries - 1));
} }
@@ -97,12 +98,13 @@ function parseTableBody(body) {
const row = $(element); const row = $(element);
torrents.push({ torrents.push({
name: row.find("a").text(), name: row.find("a").text(),
torrentId: row.find("a").attr("href") torrentId: row.find("a").attr("href"),
isTorrent: !!row.find("p:contains(\'Torrent\')")[0]
}); });
}); });
resolve(torrents); resolve(torrents);
}); });
} }
function parseTorrentPage(body) { function parseTorrentPage(body) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@@ -121,16 +123,19 @@ function parseTorrentPage(body) {
const isAnime = category === Categories.ANIME const isAnime = category === Categories.ANIME
const torrent = magnets.map(magnetLink => { const torrent = magnets.map(magnetLink => {
const name = escapeHTML(decode(magnetLink).name.replace(/\+/g, ' ')) const name = escapeHTML(decode(magnetLink).name.replace(/\+/g, ' '))
if(isDubled(name) || isAnime) { const sanitizedTitle = sanitizePtName(name);
const originalTitle = details.find('strong:contains(\'Baixar\')')[0].nextSibling.nodeValue.split('-')[0];
const year = details.find('strong:contains(\'Data de Lançamento: \')').next().text().trim();
const fallBackTitle = `${originalTitle.trim()} ${year.trim()} ${sanitizedTitle.trim()}`;
if (isPtDubbed(name) || isAnime) {
return { return {
name: parseText(name), title: sanitizedTitle.length > 4 ? sanitizedTitle : fallBackTitle,
original_name: parseName(details.find('strong:contains(\'Baixar\')')[0].nextSibling.nodeValue.split('-')[0]),
year: details.find('strong:contains(\'Data de Lançamento: \')').next().text().trim(),
infoHash: decode(magnetLink).infoHash, infoHash: decode(magnetLink).infoHash,
magnetLink: magnetLink, magnetLink: magnetLink,
category: category, category: category,
uploadDate: new Date($('time').attr('datetime')), uploadDate: new Date($('time').attr('datetime')),
imdbId: details.find('a[href*="imdb.com"]').attr('href').split('/')[4], imdbId: details.find('a[href*="imdb.com"]').attr('href').split('/')[4],
languages: sanitizePtLanguages(details.find('strong:contains(\'Idioma\')')[0].nextSibling.nodeValue)
}; };
} }
}) })
@@ -138,37 +143,4 @@ function parseTorrentPage(body) {
}); });
} }
function parseName(name) {
return name
.replace(/S01|S02|S03|S04|S05|S06|S07|S08|S09/g, '')
.trim()
}
function isDubled(name){
name = name.toLowerCase()
if(name.includes('dublado')){
return true
}
if(name.includes('dual')){
return true
}
if(name.includes('nacional')){
return true
}
return false
}
function parseText(text) {
return text
.replace(/\n|\t/g, "")
.replace(/1A|2A|3A|4A|5A|6A|7A|8A|9A/g, '')
.replace(/COMOEUBAIXO.COM|COMANDO.TO|TEMPORADA|COMPLETA/g, '')
.replace(/MKV|MP4/g, '')
.replace(/[-]/g, '')
.replace(/[.]/g, ' ')
.trim()
.replace(/ /g, '.')
.trim()
}
module.exports = { torrent, search, browse, Categories }; module.exports = { torrent, search, browse, Categories };

View File

@@ -1,15 +1,14 @@
const moment = require("moment"); const moment = require("moment");
const Bottleneck = require("bottleneck"); const Bottleneck = require("bottleneck");
const leetx = require("./comoeubaixo_api"); const comoeubaixo = require("./comoeubaixo_api");
const { Type } = require("../../lib/types"); const { Type } = require("../../lib/types");
const repository = require("../../lib/repository"); const repository = require("../../lib/repository");
const Promises = require("../../lib/promises"); const Promises = require("../../lib/promises");
const { createTorrentEntry, checkAndUpdateTorrent } = require("../../lib/torrentEntries"); const { createTorrentEntry, checkAndUpdateTorrent } = require("../../lib/torrentEntries");
const { updateCurrentSeeders, updateTorrentSize } = require("../../lib/torrent"); const { updateCurrentSeeders, updateTorrentSize } = require("../../lib/torrent");
const { getImdbId } = require("../../lib/metadata");
const NAME = "ComoEuBaixo"; const NAME = "ComoEuBaixo";
const UNTIL_PAGE = 5; const UNTIL_PAGE = 3;
const TYPE_MAPPING = typeMapping(); const TYPE_MAPPING = typeMapping();
const limiter = new Bottleneck({ maxConcurrent: 5 }); const limiter = new Bottleneck({ maxConcurrent: 5 });
@@ -20,116 +19,95 @@ async function scrape() {
console.log(`[${scrapeStart}] starting ${NAME} scrape...`); console.log(`[${scrapeStart}] starting ${NAME} scrape...`);
return scrapeLatestTorrents() return scrapeLatestTorrents()
.then(() => { .then(() => {
lastScrape.lastScraped = scrapeStart; lastScrape.lastScraped = scrapeStart;
return lastScrape.save(); return lastScrape.save();
}) })
.then(() => console.log(`[${moment()}] finished ${NAME} scrape`)); .then(() => console.log(`[${moment()}] finished ${NAME} scrape`));
} }
async function updateSeeders(torrent) { async function updateSeeders(torrent) {
return limiter.schedule(() => leetx.torrent(torrent.torrentId)); return limiter.schedule(() => comoeubaixo.torrent(torrent.torrentId));
} }
async function scrapeLatestTorrents() { async function scrapeLatestTorrents() {
const allowedCategories = [ const allowedCategories = [
leetx.Categories.MOVIE, comoeubaixo.Categories.MOVIE,
leetx.Categories.TV, comoeubaixo.Categories.TV,
leetx.Categories.DESENHOS comoeubaixo.Categories.DESENHOS
]; ];
return Promises.sequence( return Promises.sequence(allowedCategories
allowedCategories.map( .map((category) => () => scrapeLatestTorrentsForCategory(category)))
(category) => () => scrapeLatestTorrentsForCategory(category) .then((entries) => entries.reduce((a, b) => a.concat(b), []));
)
).then((entries) => entries.reduce((a, b) => a.concat(b), []));
} }
async function scrapeLatestTorrentsForCategory(category, page = 1) { async function scrapeLatestTorrentsForCategory(category, page = 180) {
console.log({Scraper: `Scrapping ${NAME} ${category} category page ${page}`}); console.log(`Scrapping ${NAME} ${category} category page ${page}`);
return leetx return comoeubaixo
.browse({ category, page }) .browse({ category, page })
.catch((error) => { .catch((error) => {
console.warn( console.warn(`Failed ${NAME} scrapping for [${page}] ${category} due: `, error);
`Failed ${NAME} scrapping for [${page}] ${category} due: `, return Promise.resolve([]);
error })
); .then((torrents) => Promise.all(torrents.map((torrent) => limiter.schedule(() => processEntry(torrent)))))
return Promise.resolve([]); .then((resolved) => resolved.length > 0 && page < untilPage(category)
}) ? scrapeLatestTorrentsForCategory(category, page + 1)
.then((torrents) => Promise.all(torrents.map((torrent) => limiter.schedule(() => processTorrentRecord(torrent))))) : Promise.resolve());
.then((resolved) => resolved.length > 0 && page < untilPage(category) ? scrapeLatestTorrentsForCategory(category, page + 1) : Promise.resolve());
} }
async function processTorrentRecord(record) { async function processEntry(entry) {
if (await checkAndUpdateTorrent({ provider: NAME, ...record })) { if (!entry.isTorrent) {
return record; return entry;
} }
const torrentEntrys = await leetx return comoeubaixo.torrent(entry.torrentId)
.torrent(record.torrentId) .then(records => Promises.sequence(records.map(record => () => processTorrentRecord(record))))
.catch(() => undefined); .catch(() => undefined);
if (torrentEntrys === undefined) { }
return Promise.resolve([])
}
return await Promise.allSettled(
torrentEntrys.map(async (torrentFound) => {
if (!torrentFound || !TYPE_MAPPING[torrentFound.category]) {
return Promise.resolve("Invalid torrent record");
}
if (isNaN(torrentFound.uploadDate)) {
console.warn(
`Incorrect upload date for [${torrentFound.infoHash}] ${torrentFound.name}`
);
return;
}
if (await checkAndUpdateTorrent(torrentFound)) {
return torrentFound;
}
if (!torrentFound.size) {
await updateTorrentSize(torrentFound)
.catch((err) => Promise.resolve(err))
}
if (!torrentFound.seeders) {
await updateCurrentSeeders(torrentFound)
.then(response => response.seeders === 0 ? delete response.seeders : response)
}
if (!torrentFound.imdbId) {
torrentFound.imdbId = await getImdbId(torrentFound.original_name, torrentFound.year, TYPE_MAPPING[torrentFound.category])
}
const torrent = { async function processTorrentRecord(foundTorrent) {
infoHash: torrentFound.infoHash, if (await checkAndUpdateTorrent({ provider: NAME, ...foundTorrent })) {
provider: NAME, return foundTorrent;
torrentId: torrentFound.torrentId, }
name: torrentFound.original_name,
title: torrentFound.name.replace(/\t|\s+/g, " ").trim(), if (!foundTorrent.size) {
type: TYPE_MAPPING[torrentFound.category], await updateTorrentSize(foundTorrent);
year: torrentFound.year, }
imdbId: torrentFound.imdbId, if (!Number.isInteger(foundTorrent.seeders)) {
uploadDate: torrentFound.uploadDate, await updateCurrentSeeders(foundTorrent);
seeders: torrentFound.seeders, }
size: torrentFound.size,
files: torrentFound.files const torrent = {
}; infoHash: foundTorrent.infoHash,
return createTorrentEntry(torrent); provider: NAME,
}) torrentId: foundTorrent.torrentId,
); title: foundTorrent.title,
type: TYPE_MAPPING[foundTorrent.category],
imdbId: foundTorrent.imdbId,
uploadDate: foundTorrent.uploadDate,
seeders: foundTorrent.seeders,
size: foundTorrent.size,
files: foundTorrent.files,
languages: foundTorrent.languages
};
return createTorrentEntry(torrent);
} }
function typeMapping() { function typeMapping() {
const mapping = {}; const mapping = {};
mapping[leetx.Categories.MOVIE] = Type.MOVIE; mapping[comoeubaixo.Categories.MOVIE] = Type.MOVIE;
mapping[leetx.Categories.TV] = Type.SERIES; mapping[comoeubaixo.Categories.TV] = Type.SERIES;
mapping[leetx.Categories.ANIME] = Type.ANIME; mapping[comoeubaixo.Categories.ANIME] = Type.ANIME;
mapping[leetx.Categories.DESENHOS] = Type.SERIES; mapping[comoeubaixo.Categories.DESENHOS] = Type.SERIES;
return mapping; return mapping;
} }
function untilPage(category) { function untilPage(category) {
if (leetx.Categories.DESENHOS === category) { if (comoeubaixo.Categories.DESENHOS === category) {
return 5; return UNTIL_PAGE;
} }
if (leetx.Categories.TV === category) { if (comoeubaixo.Categories.TV === category) {
return 5; return UNTIL_PAGE;
} }
return UNTIL_PAGE; return UNTIL_PAGE;
} }