From 3aa36ab681f90702333fb885a97de8c50edbaf04 Mon Sep 17 00:00:00 2001 From: TheBeastLT Date: Sun, 31 Jan 2021 19:19:56 +0100 Subject: [PATCH] [scraper] extract isPack to a function --- scraper/lib/parseHelper.js | 18 +++++++++++++++++- scraper/lib/torrentEntries.js | 3 ++- scraper/lib/torrentFiles.js | 21 ++++++++------------- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/scraper/lib/parseHelper.js b/scraper/lib/parseHelper.js index 7dd4ff2..38aca4f 100644 --- a/scraper/lib/parseHelper.js +++ b/scraper/lib/parseHelper.js @@ -1,6 +1,8 @@ const { parse } = require('parse-torrent-title'); const { Type } = require('./types'); +const MULTIPLE_FILES_SIZE = 4 * 1024 * 1024 * 1024; // 4 GB + function parseSeriesVideos(torrent, videos) { const parsedTorrentName = parse(torrent.title); const hasMovies = parsedTorrentName.complete || !!torrent.title.match(/movies?(?:\W|$)/i); @@ -68,4 +70,18 @@ function isMovieVideo(video, otherVideos, type, hasMovies) { && otherVideos.filter(other => other.title === video.title && other.year === video.year) < 3; } -module.exports = { parseSeriesVideos } \ No newline at end of file +function isPackTorrent(torrent) { + if (torrent.pack) { + return true; + } + const parsedInfo = parse(torrent.title); + if (torrent.type === Type.MOVIE) { + return parsedInfo.complete || typeof parsedInfo.year === 'string' || /movies/i.test(torrent.title); + } + const hasMultipleEpisodes = parsedInfo.complete || torrent.size > MULTIPLE_FILES_SIZE || + (parsedInfo.seasons && parsedInfo.seasons.length > 1); + const hasSingleEpisode = Number.isInteger(parsedInfo.episode) || (!parsedInfo.episodes && parsedInfo.date); + return hasMultipleEpisodes && !hasSingleEpisode; +} + +module.exports = { parseSeriesVideos, isPackTorrent } \ No newline at end of file diff --git a/scraper/lib/torrentEntries.js b/scraper/lib/torrentEntries.js index 5dbb313..95e48e7 100644 --- a/scraper/lib/torrentEntries.js +++ b/scraper/lib/torrentEntries.js @@ -5,6 +5,7 @@ const repository = require('./repository'); const { getImdbId, getKitsuId } = require('./metadata'); const { parseTorrentFiles } = require('./torrentFiles'); const { assignSubtitles } = require('./torrentSubtitles'); +const { isPackTorrent } = require('./parseHelper') async function createTorrentEntry(torrent, overwrite = false) { const titleInfo = parse(torrent.title); @@ -30,7 +31,7 @@ async function createTorrentEntry(torrent, overwrite = false) { .catch(() => undefined); } - if (!torrent.imdbId && !torrent.kitsuId && !titleInfo.complete && typeof titleInfo.year !== 'string') { + if (!torrent.imdbId && !torrent.kitsuId && !isPackTorrent(torrent)) { console.log(`imdbId or kitsuId not found: ${torrent.provider} ${torrent.title}`); return; } diff --git a/scraper/lib/torrentFiles.js b/scraper/lib/torrentFiles.js index 84989e5..cada339 100644 --- a/scraper/lib/torrentFiles.js +++ b/scraper/lib/torrentFiles.js @@ -4,12 +4,11 @@ const { parse } = require('parse-torrent-title'); const Promises = require('../lib/promises'); const { torrentFiles } = require('../lib/torrent'); const { getMetadata, getImdbId, getKitsuId } = require('../lib/metadata'); -const { parseSeriesVideos } = require('../lib/parseHelper'); +const { parseSeriesVideos, isPackTorrent } = require('../lib/parseHelper'); const { Type } = require('./types'); const { isDisk } = require('./extension'); const MIN_SIZE = 5 * 1024 * 1024; // 5 MB -const MULTIPLE_FILES_SIZE = 4 * 1024 * 1024 * 1024; // 4 GB async function parseTorrentFiles(torrent) { const parsedTorrentName = parse(torrent.title); @@ -34,7 +33,7 @@ async function parseTorrentFiles(torrent) { } async function parseMovieFiles(torrent, parsedName, metadata) { - const { contents, videos, subtitles } = await getMoviesTorrentContent(torrent, parsedName); + const { contents, videos, subtitles } = await getMoviesTorrentContent(torrent); const filteredVideos = videos .filter(video => video.size > MIN_SIZE) .filter(video => !isFeaturette(video)); @@ -65,7 +64,7 @@ async function parseMovieFiles(torrent, parsedName, metadata) { } async function parseSeriesFiles(torrent, parsedName, metadata) { - const { contents, videos, subtitles } = await getSeriesTorrentContent(torrent, parsedName); + const { contents, videos, subtitles } = await getSeriesTorrentContent(torrent); const parsedVideos = await Promise.resolve(videos) .then(videos => videos.filter(video => videos.length === 1 || video.size > MIN_SIZE)) .then(videos => parseSeriesVideos(torrent, videos)) @@ -80,28 +79,24 @@ async function parseSeriesFiles(torrent, parsedName, metadata) { return { contents, videos: parsedVideos, subtitles }; } -async function getMoviesTorrentContent(torrent, parsedName) { - const hasMultipleMovie = parsedName.complete || typeof parsedName.year === 'string'; +async function getMoviesTorrentContent(torrent) { const files = await torrentFiles(torrent) .catch(error => { - if (!hasMultipleMovie) { + if (!isPackTorrent(torrent)) { return { videos: [{ name: torrent.title, path: torrent.title, size: torrent.size }] } } return Promise.reject(error); }); - if (files.contents && files.contents.length && !files.length && isDiskTorrent(files.contents)) { + if (files.contents && files.contents.length && !files.videos.length && isDiskTorrent(files.contents)) { files.videos = [{ name: torrent.title, path: torrent.title, size: torrent.size }]; } return files; } -async function getSeriesTorrentContent(torrent, parsedName) { - const hasMultipleEpisodes = parsedName.complete || torrent.size > MULTIPLE_FILES_SIZE || - (parsedName.seasons && parsedName.seasons.length > 1); - const hasSingleEpisode = Number.isInteger(parsedName.episode) || (!parsedName.episodes && parsedName.date); +async function getSeriesTorrentContent(torrent) { return torrentFiles(torrent) .catch(error => { - if (!hasMultipleEpisodes && hasSingleEpisode) { + if (!isPackTorrent(torrent)) { return { videos: [{ name: torrent.title, path: torrent.title, size: torrent.size }] } } return Promise.reject(error);