mirror of
https://github.com/knightcrawler-stremio/knightcrawler.git
synced 2024-12-20 03:29:51 +00:00
updates the package structure and updates initial horriblesubs scraper WIP
This commit is contained in:
@@ -5,8 +5,14 @@ const { Type } = require('./types');
|
||||
|
||||
const MIN_SIZE = 20 * 1024 * 1024; // 20 MB
|
||||
|
||||
async function parseTorrentFiles(torrent, imdbId) {
|
||||
async function parseTorrentFiles(torrent, imdbId, kitsuId) {
|
||||
const parsedTorrentName = parse(torrent.title);
|
||||
parsedTorrentName.hasMovies = parsedTorrentName.complete || !!torrent.title.match(/movies?(?:\W|$)/);
|
||||
const metadata = await getMetadata(kitsuId || imdbId, torrent.type || Type.MOVIE).catch(() => undefined);
|
||||
|
||||
if (metadata && metadata.type !== torrent.type && torrent.type !== Type.ANIME) {
|
||||
throw new Error(`Mismatching entry type for ${torrent.name}: ${torrent.type}!=${metadata.type}`);
|
||||
}
|
||||
|
||||
if (torrent.type === Type.MOVIE) {
|
||||
if (parsedTorrentName.complete) {
|
||||
@@ -27,31 +33,20 @@ async function parseTorrentFiles(torrent, imdbId) {
|
||||
});
|
||||
}
|
||||
|
||||
return [{
|
||||
return [ {
|
||||
infoHash: torrent.infoHash,
|
||||
title: torrent.title,
|
||||
size: torrent.size,
|
||||
imdbId: imdbId,
|
||||
}];
|
||||
imdbId: imdbId || metadata && metadata.imdb_id,
|
||||
kitsuId: kitsuId || metadata && metadata.kitsu_id
|
||||
} ];
|
||||
}
|
||||
|
||||
if (parsedTorrentName.season && parsedTorrentName.episode) {
|
||||
return [{
|
||||
infoHash: torrent.infoHash,
|
||||
title: torrent.title,
|
||||
size: torrent.size,
|
||||
imdbId: imdbId,
|
||||
imdbSeason: parsedTorrentName.season,
|
||||
imdbEpisode: parsedTorrentName.episode
|
||||
}];
|
||||
}
|
||||
|
||||
parsedTorrentName.hasMovies = parsedTorrentName.complete || !!torrent.title.match(/movies?(?:\W|$)/);
|
||||
return torrentFiles(torrent)
|
||||
return getSeriesFiles(torrent, parsedTorrentName)
|
||||
.then((files) => files
|
||||
.filter((file) => file.size > MIN_SIZE)
|
||||
.map((file) => parseSeriesFile(file, parsedTorrentName)))
|
||||
.then((files) => decomposeAbsoluteEpisodes(files, torrent, imdbId))
|
||||
.then((files) => decomposeAbsoluteEpisodes(files, metadata))
|
||||
.then((files) => Promise.all(files.map(file => file.isMovie
|
||||
? mapSeriesMovie(file, torrent.infoHash)
|
||||
: mapSeriesEpisode(file, torrent.infoHash, imdbId))))
|
||||
@@ -62,6 +57,18 @@ async function parseTorrentFiles(torrent, imdbId) {
|
||||
});
|
||||
}
|
||||
|
||||
async function getSeriesFiles(torrent, parsedTorrentName) {
|
||||
if (parsedTorrentName.episode || parsedTorrentName.date) {
|
||||
return [ {
|
||||
name: torrent.title,
|
||||
path: torrent.title,
|
||||
size: torrent.size
|
||||
} ];
|
||||
}
|
||||
|
||||
return torrentFiles(torrent);
|
||||
}
|
||||
|
||||
async function mapSeriesEpisode(file, infoHash, imdbId) {
|
||||
if (!file.episodes) {
|
||||
return Promise.resolve([]);
|
||||
@@ -69,21 +76,22 @@ async function mapSeriesEpisode(file, infoHash, imdbId) {
|
||||
return Promise.resolve(file.episodes.map(episode => ({
|
||||
infoHash: infoHash,
|
||||
fileIndex: file.fileIndex,
|
||||
title: file.name,
|
||||
title: file.path || file.name,
|
||||
size: file.size,
|
||||
imdbId: imdbId,
|
||||
imdbSeason: file.season,
|
||||
imdbEpisode: episode})))
|
||||
imdbEpisode: episode
|
||||
})))
|
||||
}
|
||||
|
||||
async function mapSeriesMovie(file, infoHash) {
|
||||
return findMovieImdbId(file).then((imdbId) => [{
|
||||
return findMovieImdbId(file).then((imdbId) => [ {
|
||||
infoHash: infoHash,
|
||||
fileIndex: file.fileIndex,
|
||||
title: file.name,
|
||||
size: file.size,
|
||||
imdbId: imdbId
|
||||
}])
|
||||
} ])
|
||||
}
|
||||
|
||||
function parseSeriesFile(file, parsedTorrentName) {
|
||||
@@ -96,7 +104,8 @@ function parseSeriesFile(file, parsedTorrentName) {
|
||||
const pathInfo = parse(folders[folders.length - 2]);
|
||||
fileInfo.season = pathInfo.season;
|
||||
}
|
||||
fileInfo.isMovie = parsedTorrentName.hasMovies && !fileInfo.season && !fileInfo.episodes || !!fileInfo.year;
|
||||
fileInfo.isMovie = parsedTorrentName.hasMovies && !fileInfo.season &&
|
||||
(!fileInfo.episodes || !!fileInfo.year || !!file.name.match(/\b(?:\d+[ .]movie|movie[ .]\d+)\b/i));
|
||||
|
||||
return { ...file, ...fileInfo };
|
||||
}
|
||||
@@ -111,12 +120,11 @@ function findMovieImdbId(title) {
|
||||
return getImdbId(searchQuery).catch((error) => undefined);
|
||||
}
|
||||
|
||||
async function decomposeAbsoluteEpisodes(files, torrent, imdbId) {
|
||||
async function decomposeAbsoluteEpisodes(files, metadata) {
|
||||
if (files.every((file) => !file.episodes || file.episodes.every((ep) => ep < 100))) {
|
||||
return files; // nothing to decompose
|
||||
}
|
||||
|
||||
const metadata = await getMetadata(imdbId, torrent.type || Type.MOVIE);
|
||||
// decompose if season is inside path, but individual files are concatenated ex. 101 (S01E01)
|
||||
files
|
||||
.filter(file => file.season && metadata.episodeCount[file.season] < 100)
|
||||
|
||||
Reference in New Issue
Block a user