From 149b24fbf0b89a8fbbb1dca89833eb08a63eb51c Mon Sep 17 00:00:00 2001 From: TheBeastLT Date: Wed, 26 Feb 2020 00:31:13 +0100 Subject: [PATCH] handles series type mismatch if is in movie category --- lib/repository.js | 7 ++++++- lib/torrentEntries.js | 4 ++++ lib/torrentFiles.js | 2 +- manual/manual.js | 43 +++++++++++++++++++++++++++++++------------ package-lock.json | 2 +- 5 files changed, 43 insertions(+), 15 deletions(-) diff --git a/lib/repository.js b/lib/repository.js index d39ffa4..f1724ce 100644 --- a/lib/repository.js +++ b/lib/repository.js @@ -81,6 +81,10 @@ function getTorrent(torrent) { }) } +function getTorrentsBasedOnTitle(titleQuery, type) { + return Torrent.findAll({ where: { title: { [Op.regexp]: `${titleQuery}` }, type: type } }); +} + function createTorrent(torrent) { return Torrent.upsert(torrent); } @@ -133,8 +137,9 @@ module.exports = { connect, getProvider, updateProvider, - getTorrent, createTorrent, + getTorrent, + getTorrentsBasedOnTitle, createFile, getFiles, getFilesBasedOnTitle, diff --git a/lib/torrentEntries.js b/lib/torrentEntries.js index bb9b9fa..40cd0a2 100644 --- a/lib/torrentEntries.js +++ b/lib/torrentEntries.js @@ -8,6 +8,10 @@ async function createTorrentEntry(torrent) { const titleInfo = parse(torrent.title); const searchTitle = escapeTitle(titleInfo.title).toLowerCase(); + if (titleInfo.seasons && torrent.type === Type.MOVIE) { + // sometimes series torrent might be put into movies category + torrent.type = Type.SERIES; + } if (!torrent.imdbId && torrent.type !== Type.ANIME) { torrent.imdbId = await getImdbId({ name: searchTitle, year: titleInfo.year, type: torrent.type }) .catch(() => undefined); diff --git a/lib/torrentFiles.js b/lib/torrentFiles.js index a8b08e9..8ea7607 100644 --- a/lib/torrentFiles.js +++ b/lib/torrentFiles.js @@ -16,7 +16,7 @@ async function parseTorrentFiles(torrent) { // throw new Error(`Mismatching entry type for ${torrent.name}: ${torrent.type}!=${metadata.type}`); // } - if (torrent.type === Type.MOVIE) { + if (torrent.type === Type.MOVIE && !parsedTorrentName.seasons) { if (parsedTorrentName.complete) { return torrentFiles(torrent) .then(files => files.filter(file => file.size > MIN_SIZE)) diff --git a/manual/manual.js b/manual/manual.js index e505687..1d942f2 100644 --- a/manual/manual.js +++ b/manual/manual.js @@ -1,9 +1,12 @@ require('dotenv').config(); +const Bottleneck = require('bottleneck'); const { parse } = require('parse-torrent-title'); const repository = require('../lib/repository'); const { parseTorrentFiles } = require('../lib/torrentFiles'); const { Type } = require('../lib/types'); +const limiter = new Bottleneck({ maxConcurrent: 40 }); + async function addMissingEpisodes() { const torrent = { infoHash: '0ec780c2c7f8d5b38e61827f0b53c77c3d22f955' }; const torrentFiles = await require('../lib/torrent').torrentFiles(torrent); @@ -48,27 +51,42 @@ async function updateMovieCollections() { })); } -async function reapplyEpisodeDecomposing() { - const infoHash = '84fadd061f0d0bc356235b7fa6495a0f51fff311'; - const imdbId = 'tt0988824'; +async function reapplySeriesSeasonsSavedAsMovies() { + return repository.getTorrentsBasedOnTitle('(?:[^a-zA-Z0-9]|^)[Ss][012]?[0-9](?:[^0-9]|$)', Type.MOVIE) + .then(torrents => Promise.all(torrents + .filter(torrent => parse(torrent.title).seasons) + .map(torrent => limiter.schedule(() => reapplyEpisodeDecomposing(torrent.infoHash, false) + .then(() => { + torrent.type = Type.SERIES; + return torrent.save(); + }))))) + .then(() => console.log('Finished updating multiple torrents')); +} + +async function reapplyEpisodeDecomposing(infoHash, includeSourceFiles = true) { const torrent = await repository.getTorrent({ infoHash }); const storedFiles = await repository.getFiles({ infoHash }); const fileIndexMap = storedFiles .reduce((map, next) => (map[next.fileIndex] = (map[next.fileIndex] || []).concat(next), map), {}); - const files = Object.values(fileIndexMap) + const files = includeSourceFiles && Object.values(fileIndexMap) .map(sameIndexFiles => sameIndexFiles[0]) .map(file => ({ fileIndex: file.fileIndex, name: file.title, path: file.title, size: file.size })); + const imdbId = storedFiles[0].imdbId; return parseTorrentFiles({ ...torrent, imdbId, files }) .then(newFiles => newFiles.map(file => { - const originalFile = fileIndexMap[file.fileIndex].shift(); - originalFile.imdbSeason = file.imdbSeason; - originalFile.imdbEpisode = file.imdbEpisode; - originalFile.kitsuId = file.kitsuId; - originalFile.kitsuEpisode = file.kitsuEpisode; - return originalFile; + if (fileIndexMap[file.fileIndex]) { + const originalFile = fileIndexMap[file.fileIndex].shift(); + originalFile.imdbSeason = file.imdbSeason; + originalFile.imdbEpisode = file.imdbEpisode; + originalFile.kitsuId = file.kitsuId; + originalFile.kitsuEpisode = file.kitsuEpisode; + return originalFile; + } + return file; })) - .then(updatedFiles => Promise.all(updatedFiles.map(file => file.save()))) + .then(updatedFiles => Promise.all(updatedFiles + .map(file => file.id ? file.save() : repository.createFile(file)))) .then(() => console.log(`Updated files for ${torrent.title}`)); } @@ -148,4 +166,5 @@ async function findAllFiles() { //addMissingEpisodes().then(() => console.log('Finished')); //findAllFiles().then(() => console.log('Finished')); //updateMovieCollections().then(() => console.log('Finished')); -reapplyEpisodeDecomposing().then(() => console.log('Finished')); \ No newline at end of file +//reapplyEpisodeDecomposing().then(() => console.log('Finished')); +reapplySeriesSeasonsSavedAsMovies().then(() => console.log('Finished')); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index d9f7a1a..5e0464d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1714,7 +1714,7 @@ } }, "parse-torrent-title": { - "version": "git://github.com/TheBeastLT/parse-torrent-title.git#d6b677ad5e576c9294de1de75c0fd1396bd1ab7b", + "version": "git://github.com/TheBeastLT/parse-torrent-title.git#286c5f9b06a2ac38354ea249a1d96b17919ba930", "from": "git://github.com/TheBeastLT/parse-torrent-title.git#master" }, "parseurl": {