const { parse } = require('parse-torrent-title'); const { Type } = require('./types'); function parseSeriesVideos(torrent, videos) { const parsedTorrentName = parse(torrent.title); const hasMovies = parsedTorrentName.complete || !!torrent.title.match(/movies?(?:\W|$)/i); const parsedVideos = videos.map(video => parseSeriesVideo(video, parsedTorrentName)); return parsedVideos.map(video => ({ ...video, isMovie: isMovieVideo(video, parsedVideos, torrent.type, hasMovies) })); } function parseSeriesVideo(video, parsedTorrentName) { const videoInfo = parse(video.name); const hasSeason = Number.isInteger(videoInfo.season); // the episode may be in a folder containing season number if (!hasSeason && video.path.includes('/')) { const folders = video.path.split('/'); const pathInfo = parse(folders[folders.length - 2]); videoInfo.season = pathInfo.season; } if (!hasSeason && parsedTorrentName.season) { videoInfo.season = parsedTorrentName.season; } if (!hasSeason && videoInfo.seasons && videoInfo.seasons.length > 1) { // in case single file was interpreted as having multiple seasons videoInfo.season = videoInfo.seasons[0]; } // sometimes video file does not have correct date format as in torrent title if (!videoInfo.episodes && !videoInfo.date && parsedTorrentName.date) { videoInfo.date = parsedTorrentName.date; } // limit number of episodes in case of incorrect parsing if (videoInfo.episodes && videoInfo.episodes.length > 20) { videoInfo.episodes = [videoInfo.episodes[0]]; videoInfo.episode = videoInfo.episodes[0]; } // force episode to any found number if it was not parsed if (!videoInfo.episodes && !videoInfo.date) { const epMatcher = videoInfo.title.match( /(? 3 && otherVideos.filter(other => other.title === video.title && other.year === video.year) < 3; } module.exports = { parseSeriesVideos }