mirror of
https://github.com/knightcrawler-stremio/knightcrawler.git
synced 2024-12-20 03:29:51 +00:00
adds movie packs handling and movies in series packs
This commit is contained in:
@@ -1,12 +1,32 @@
|
||||
const { torrentFiles } = require('../lib/torrent');
|
||||
const { getMetadata } = require('../lib/metadata');
|
||||
const { escapeTitle, getMetadata, getImdbId } = require('../lib/metadata');
|
||||
const { parse } = require('parse-torrent-title');
|
||||
const { Type } = require('./types');
|
||||
|
||||
const MIN_SIZE = 20 * 1024 * 1024; // 20 MB
|
||||
|
||||
async function parseTorrentFiles(torrent, imdbId) {
|
||||
const parsedTorrentName = parse(torrent.title);
|
||||
|
||||
if (torrent.type === Type.MOVIE) {
|
||||
if (parsedTorrentName.complete) {
|
||||
return torrentFiles(torrent)
|
||||
.then(files => files.filter(file => file.size > MIN_SIZE))
|
||||
.then(files => Promise.all(files
|
||||
.map((file) => findMovieImdbId(file.name)
|
||||
.then((newImdbId) => ({
|
||||
infoHash: torrent.infoHash,
|
||||
fileIndex: file.fileIndex,
|
||||
title: file.name,
|
||||
size: file.size,
|
||||
imdbId: newImdbId,
|
||||
})))))
|
||||
.catch(error => {
|
||||
console.log(`Failed getting files for ${torrent.title}`, error.message);
|
||||
return [];
|
||||
});
|
||||
}
|
||||
|
||||
return [{
|
||||
infoHash: torrent.infoHash,
|
||||
title: torrent.title,
|
||||
@@ -14,7 +34,7 @@ async function parseTorrentFiles(torrent, imdbId) {
|
||||
imdbId: imdbId,
|
||||
}];
|
||||
}
|
||||
const parsedTorrentName = parse(torrent.title);
|
||||
|
||||
if (parsedTorrentName.season && parsedTorrentName.episode) {
|
||||
return [{
|
||||
infoHash: torrent.infoHash,
|
||||
@@ -26,29 +46,47 @@ async function parseTorrentFiles(torrent, imdbId) {
|
||||
}];
|
||||
}
|
||||
|
||||
parsedTorrentName.hasMovies = parsedTorrentName.complete || !!torrent.title.match(/movies?(?:\W|$)/);
|
||||
return torrentFiles(torrent)
|
||||
.then(files => files
|
||||
.filter(file => file.size > MIN_SIZE)
|
||||
.map(file => parseFile(file, parsedTorrentName)))
|
||||
.then(files => decomposeAbsoluteEpisodes(files, torrent, imdbId))
|
||||
.then(files => files
|
||||
.filter(file => file.season && file.episodes && file.episodes.length)
|
||||
.map(file => file.episodes.map(episode => ({
|
||||
infoHash: torrent.infoHash,
|
||||
fileIndex: file.fileIndex,
|
||||
title: file.name,
|
||||
size: file.size,
|
||||
imdbId: imdbId,
|
||||
imdbSeason: file.season,
|
||||
imdbEpisode: episode})))
|
||||
.reduce((a, b) => a.concat(b), []))
|
||||
.catch(error => {
|
||||
.then((files) => files
|
||||
.filter((file) => file.size > MIN_SIZE)
|
||||
.map((file) => parseSeriesFile(file, parsedTorrentName)))
|
||||
.then((files) => decomposeAbsoluteEpisodes(files, torrent, imdbId))
|
||||
.then((files) => Promise.all(files.map(file => file.isMovie
|
||||
? mapSeriesMovie(file, torrent.infoHash)
|
||||
: mapSeriesEpisode(file, torrent.infoHash, imdbId))))
|
||||
.then((files) => files.reduce((a, b) => a.concat(b), []))
|
||||
.catch((error) => {
|
||||
console.log(`Failed getting files for ${torrent.title}`, error.message);
|
||||
return [];
|
||||
});
|
||||
}
|
||||
|
||||
function parseFile(file, parsedTorrentName) {
|
||||
async function mapSeriesEpisode(file, infoHash, imdbId) {
|
||||
if (!file.episodes) {
|
||||
return Promise.resolve([]);
|
||||
}
|
||||
return Promise.resolve(file.episodes.map(episode => ({
|
||||
infoHash: infoHash,
|
||||
fileIndex: file.fileIndex,
|
||||
title: file.name,
|
||||
size: file.size,
|
||||
imdbId: imdbId,
|
||||
imdbSeason: file.season,
|
||||
imdbEpisode: episode})))
|
||||
}
|
||||
|
||||
async function mapSeriesMovie(file, infoHash) {
|
||||
return findMovieImdbId(file).then((imdbId) => [{
|
||||
infoHash: infoHash,
|
||||
fileIndex: file.fileIndex,
|
||||
title: file.name,
|
||||
size: file.size,
|
||||
imdbId: imdbId
|
||||
}])
|
||||
}
|
||||
|
||||
function parseSeriesFile(file, parsedTorrentName) {
|
||||
const fileInfo = parse(file.name);
|
||||
// the episode may be in a folder containing season number
|
||||
if (!fileInfo.season && parsedTorrentName.season) {
|
||||
@@ -58,10 +96,21 @@ function parseFile(file, parsedTorrentName) {
|
||||
const pathInfo = parse(folders[folders.length - 2]);
|
||||
fileInfo.season = pathInfo.season;
|
||||
}
|
||||
fileInfo.isMovie = parsedTorrentName.hasMovies && !fileInfo.season && !fileInfo.episodes || !!fileInfo.year;
|
||||
|
||||
return { ...file, ...fileInfo };
|
||||
}
|
||||
|
||||
function findMovieImdbId(title) {
|
||||
const parsedTitle = typeof title === 'string' ? parse(title) : title;
|
||||
const searchQuery = {
|
||||
name: escapeTitle(parsedTitle.title).toLowerCase(),
|
||||
year: parsedTitle.year,
|
||||
type: Type.MOVIE
|
||||
};
|
||||
return getImdbId(searchQuery).catch((error) => undefined);
|
||||
}
|
||||
|
||||
async function decomposeAbsoluteEpisodes(files, torrent, imdbId) {
|
||||
if (files.every((file) => !file.episodes || file.episodes.every((ep) => ep < 100))) {
|
||||
return files; // nothing to decompose
|
||||
|
||||
Reference in New Issue
Block a user