mirror of
https://github.com/knightcrawler-stremio/knightcrawler.git
synced 2024-12-20 03:29:51 +00:00
[scraper] assign subtitles to videos
This commit is contained in:
@@ -204,6 +204,9 @@ function createFile(file) {
|
||||
if (file.id) {
|
||||
return File.upsert(file).then(() => upsertSubtitles(file.id, file.subtitles));
|
||||
}
|
||||
if (file.subtitles && file.subtitles.length) {
|
||||
file.subtitles = file.subtitles.map(subtitle => ({ infoHash: file.infoHash, title: subtitle.path, ...subtitle }));
|
||||
}
|
||||
return File.create(file, { include: [Subtitle] });
|
||||
}
|
||||
|
||||
@@ -229,8 +232,13 @@ function createSubtitles(infoHash, subtitles) {
|
||||
function upsertSubtitles(file, subtitles) {
|
||||
if (file.id && subtitles && subtitles.length) {
|
||||
return Promises.sequence(subtitles
|
||||
.map(subtitle => ({ fileId: file.id, infoHash: file.infoHash, title: subtitle.path, ...subtitle }))
|
||||
.map(subtitle => () => Subtitle.upsert(subtitle)));
|
||||
.map(subtitle => {
|
||||
subtitle.fileId = file.id;
|
||||
subtitle.infoHash = subtitle.infoHash || file.infoHash;
|
||||
subtitle.title = subtitle.title || subtitle.path;
|
||||
return subtitle;
|
||||
})
|
||||
.map(subtitle => () => subtitle.dataValues ? subtitle.save() : Subtitle.upsert(subtitle)));
|
||||
}
|
||||
return Promise.resolve();
|
||||
}
|
||||
@@ -239,6 +247,10 @@ function getSubtitles(torrent) {
|
||||
return Subtitle.findAll({ where: { infoHash: torrent.infoHash } });
|
||||
}
|
||||
|
||||
function getUnassignedSubtitles() {
|
||||
return Subtitle.findAll({ where: { fileId: null } });
|
||||
}
|
||||
|
||||
function createContents(infoHash, contents) {
|
||||
if (contents && contents.length) {
|
||||
return Content.bulkCreate(contents.map(content => ({ infoHash, ...content })))
|
||||
@@ -278,7 +290,9 @@ module.exports = {
|
||||
getFilesBasedOnTitle,
|
||||
deleteFile,
|
||||
createSubtitles,
|
||||
upsertSubtitles,
|
||||
getSubtitles,
|
||||
getUnassignedSubtitles,
|
||||
createContents,
|
||||
getContents,
|
||||
getSkipTorrent,
|
||||
|
||||
@@ -3,6 +3,7 @@ const { Type } = require('./types');
|
||||
const repository = require('./repository');
|
||||
const { getImdbId, getKitsuId } = require('./metadata');
|
||||
const { parseTorrentFiles } = require('./torrentFiles');
|
||||
const { assignSubtitles } = require('./torrentSubtitles');
|
||||
|
||||
async function createTorrentEntry(torrent, overwrite = false) {
|
||||
const titleInfo = parse(torrent.title);
|
||||
@@ -35,6 +36,7 @@ async function createTorrentEntry(torrent, overwrite = false) {
|
||||
|
||||
const { contents, videos, subtitles } = await parseTorrentFiles(torrent)
|
||||
.then(torrentContents => overwrite ? overwriteExistingFiles(torrent, torrentContents) : torrentContents)
|
||||
.then(torrentContents => assignSubtitles(torrentContents))
|
||||
.catch(error => {
|
||||
console.log(`Failed getting files for ${torrent.title}`, error.message);
|
||||
return {};
|
||||
@@ -68,7 +70,7 @@ async function overwriteExistingFiles(torrent, torrentContents) {
|
||||
: existingFiles[file.fileIndex !== undefined ? file.fileIndex : null];
|
||||
if (mapping) {
|
||||
const originalFile = mapping.shift();
|
||||
return { ...file, id: originalFile.id, size: originalFile.size || file.size };
|
||||
return { id: originalFile.id, ...file };
|
||||
}
|
||||
return file;
|
||||
});
|
||||
|
||||
63
scraper/lib/torrentSubtitles.js
Normal file
63
scraper/lib/torrentSubtitles.js
Normal file
@@ -0,0 +1,63 @@
|
||||
const { parse } = require('parse-torrent-title');
|
||||
|
||||
function assignSubtitles({ contents, videos, subtitles }) {
|
||||
if (videos && videos.length && subtitles && subtitles.length) {
|
||||
if (videos.length === 1) {
|
||||
videos[0].subtitles = subtitles;
|
||||
return { contents, videos, subtitles: [] };
|
||||
}
|
||||
|
||||
const parsedVideos = videos
|
||||
.map(video => _parseVideo(video));
|
||||
const assignedSubs = subtitles
|
||||
.map(subtitle => ({ subtitle, video: _mostProbableSubtitleVideo(subtitle, parsedVideos) }));
|
||||
const unassignedSubs = assignedSubs.filter(assignedSub => !assignedSub.video);
|
||||
|
||||
assignedSubs
|
||||
.filter(assignedSub => assignedSub.video)
|
||||
.forEach(assignedSub =>
|
||||
assignedSub.video.subtitles = (assignedSub.video.subtitles || []).concat(assignedSub.subtitle))
|
||||
return { contents, videos, subtitles: unassignedSubs };
|
||||
}
|
||||
return { contents, videos, subtitles };
|
||||
}
|
||||
|
||||
function _parseVideo(video) {
|
||||
return {
|
||||
videoFile: video,
|
||||
fileName: video.title.replace(/\.(\w{2,4})$/, ''),
|
||||
folderName: video.title.replace(/\/?[^/]+$/, ''),
|
||||
...parse(video.title)
|
||||
};
|
||||
}
|
||||
|
||||
function _mostProbableSubtitleVideo(subtitle, parsedVideos) {
|
||||
const subTitle = subtitle.title || subtitle.path;
|
||||
const parsedSub = parse(subTitle);
|
||||
const byFileName = parsedVideos.filter(video => subTitle.includes(video.fileName));
|
||||
if (byFileName.length === 1) {
|
||||
return byFileName[0].videoFile;
|
||||
}
|
||||
const byTitleSeasonEpisode = parsedVideos.filter(video => parsedSub.title.includes(video.title)
|
||||
&& video.seasons === parsedSub.seasons
|
||||
&& JSON.stringify(video.episodes) === JSON.stringify(parsedSub.episodes));
|
||||
if (byTitleSeasonEpisode.length === 1) {
|
||||
return byTitleSeasonEpisode[0].videoFile;
|
||||
}
|
||||
const bySeasonEpisode = parsedVideos.filter(video => video.seasons === parsedSub.seasons
|
||||
&& video.episodes === parsedSub.episodes);
|
||||
if (bySeasonEpisode.length === 1) {
|
||||
return bySeasonEpisode[0].videoFile;
|
||||
}
|
||||
const byTitle = parsedVideos.filter(video => parsedSub.title.includes(video.title));
|
||||
if (byTitle.length === 1) {
|
||||
return byTitle[0].videoFile;
|
||||
}
|
||||
const byEpisode = parsedVideos.filter(video => JSON.stringify(video.episodes) === JSON.stringify(parsedSub.episodes));
|
||||
if (byEpisode.length === 1) {
|
||||
return byEpisode[0].videoFile;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
module.exports = { assignSubtitles }
|
||||
Reference in New Issue
Block a user