[scraper] improves subtitle matching

This commit is contained in:
TheBeastLT
2020-12-01 10:52:21 +01:00
parent c1eb2c3164
commit 705570de53

View File

@@ -31,25 +31,25 @@ function _parseVideo(video) {
videoFile: video, videoFile: video,
fileName: fileName, fileName: fileName,
folderName: folderName, folderName: folderName,
...parse(video.title) ...parseFilename(video.title)
}; };
} }
function _mostProbableSubtitleVideos(subtitle, parsedVideos) { function _mostProbableSubtitleVideos(subtitle, parsedVideos) {
const subTitle = subtitle.title || subtitle.path; const subTitle = (subtitle.title || subtitle.path).split('/').pop().replace(/\.(\w{2,4})$/, '');
const parsedSub = parse(subTitle.replace(/\.(\w{2,4})$/, '')); const parsedSub = parseFilename(subTitle);
const byFileName = parsedVideos.filter(video => subTitle.includes(video.fileName)); const byFileName = parsedVideos.filter(video => subTitle.includes(video.fileName));
if (byFileName.length === 1) { if (byFileName.length === 1) {
return byFileName.map(v => v.videoFile); return byFileName.map(v => v.videoFile);
} }
const byTitleSeasonEpisode = parsedVideos.filter(video => video.title === parsedSub.title const byTitleSeasonEpisode = parsedVideos.filter(video => video.title === parsedSub.title
&& video.seasons === parsedSub.seasons && arrayEquals(video.seasons, parsedSub.seasons)
&& JSON.stringify(video.episodes) === JSON.stringify(parsedSub.episodes)); && arrayEquals(video.episodes, parsedSub.episodes));
if (singleVideoFile(byTitleSeasonEpisode)) { if (singleVideoFile(byTitleSeasonEpisode)) {
return byTitleSeasonEpisode.map(v => v.videoFile); return byTitleSeasonEpisode.map(v => v.videoFile);
} }
const bySeasonEpisode = parsedVideos.filter(video => video.seasons === parsedSub.seasons const bySeasonEpisode = parsedVideos.filter(video => arrayEquals(video.seasons, parsedSub.seasons)
&& video.episodes === parsedSub.episodes); && arrayEquals(video.episodes, parsedSub.episodes));
if (singleVideoFile(bySeasonEpisode)) { if (singleVideoFile(bySeasonEpisode)) {
return bySeasonEpisode.map(v => v.videoFile); return bySeasonEpisode.map(v => v.videoFile);
} }
@@ -57,7 +57,7 @@ function _mostProbableSubtitleVideos(subtitle, parsedVideos) {
if (singleVideoFile(byTitle)) { if (singleVideoFile(byTitle)) {
return byTitle.map(v => v.videoFile); return byTitle.map(v => v.videoFile);
} }
const byEpisode = parsedVideos.filter(video => JSON.stringify(video.episodes) === JSON.stringify(parsedSub.episodes)); const byEpisode = parsedVideos.filter(video => arrayEquals(video.episodes, parsedSub.episodes));
if (singleVideoFile(byEpisode)) { if (singleVideoFile(byEpisode)) {
return byEpisode.map(v => v.videoFile); return byEpisode.map(v => v.videoFile);
} }
@@ -68,4 +68,18 @@ function singleVideoFile(videos) {
return new Set(videos.map(v => v.videoFile.fileIndex)).size === 1; return new Set(videos.map(v => v.videoFile.fileIndex)).size === 1;
} }
function parseFilename(filename) {
const parsedInfo = parse(filename)
const titleEpisode = parsedInfo.title.match(/(\d+)$/);
if (!parsedInfo.episodes && titleEpisode) {
parsedInfo.episodes = [parseInt(titleEpisode[1], 10)];
}
return parsedInfo;
}
function arrayEquals(array1, array2) {
if (!array1 || !array2) return array1 === array2;
return array1.length === array2.length && array1.every((value, index) => value === array2[index])
}
module.exports = { assignSubtitles } module.exports = { assignSubtitles }