UNPKG

identify-media

Version:

Analyse file path and content to make search criteria for media APIs

117 lines 5.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isSameRelease = exports.isSample = exports.isExtras = exports.analyseFilePath = exports.isAnalysedTVShow = exports.isAnalysedMovie = void 0; const analyseString_1 = require("./analyseString"); const cleanString_1 = require("./cleanString"); const parseFilePath = (filePath) => { const parts = filePath.split(/[/\\]/); const fileName = parts[parts.length - 1]; const dirNames = parts.slice(0, -1); return { fileName, dirNames }; }; const cleanUpName = (name) => { return (0, cleanString_1.stripParenthesis)((0, cleanString_1.trimEnd)((0, cleanString_1.setSpaces)(name))).trim(); }; const isAnalysedMovie = (analysed) => { return analysed.type === 'movie'; }; exports.isAnalysedMovie = isAnalysedMovie; const analyseForMovie = (fileName, dirNames) => { const yearResult = (0, analyseString_1.findYear)(fileName) || (0, analyseString_1.findYear)(dirNames[dirNames.length - 1]); if (!!yearResult) { const name = cleanUpName(yearResult.restName || (0, cleanString_1.stripEnd)(fileName)); return { type: 'movie', year: yearResult.year, name }; } return undefined; }; const isAnalysedTVShow = (analysed) => { return analysed.hasOwnProperty('type') && analysed.type === 'tv'; }; exports.isAnalysedTVShow = isAnalysedTVShow; const searchYear = (fileName, dirNames, episodeRestName, seasonRestName) => { return (0, analyseString_1.findYear)(episodeRestName || seasonRestName) || (0, analyseString_1.findYear)(dirNames[dirNames.length - 2]) || (0, analyseString_1.findYear)(dirNames[dirNames.length - 1]) || (0, analyseString_1.findYear)(fileName); }; const guessName = (fileName, dirNames, seResult, yearResult) => { return cleanUpName((yearResult === null || yearResult === void 0 ? void 0 : yearResult.restName) && (0, cleanString_1.trimEnd)((0, cleanString_1.stripSeasons)(yearResult.restName)) || (seResult === null || seResult === void 0 ? void 0 : seResult.restName) || (!!yearResult ? dirNames[dirNames.length - 3] || dirNames[dirNames.length - 2] : dirNames[dirNames.length - 2] || dirNames[dirNames.length - 1]) || (0, cleanString_1.stripEnd)(fileName)); }; const tvBySeasonEpisode = (fileName, dirNames) => { const seasonEpisodeResult = (0, analyseString_1.findSeasonEpisode)(fileName); if (!!seasonEpisodeResult) { const seasonResult = (0, analyseString_1.findSeason)(dirNames[dirNames.length - 1]); const season = seasonEpisodeResult.season || (seasonResult === null || seasonResult === void 0 ? void 0 : seasonResult.season); const yearResult = searchYear(fileName, dirNames, seasonEpisodeResult.restName, seasonResult === null || seasonResult === void 0 ? void 0 : seasonResult.restName); const name = guessName(fileName, dirNames, seasonEpisodeResult.restName ? seasonEpisodeResult : seasonResult, yearResult); return { type: 'tv', name, season, episodes: seasonEpisodeResult.episodes, year: yearResult === null || yearResult === void 0 ? void 0 : yearResult.year, }; } return undefined; }; const tvBySeason = (fileName, dirNames) => { const seasonResult = (0, analyseString_1.findSeason)(dirNames[dirNames.length - 1]); if (!!seasonResult) { const episodeResult = (0, analyseString_1.findEpisode)(fileName, seasonResult.season); const yearResult = searchYear(fileName, dirNames, episodeResult === null || episodeResult === void 0 ? void 0 : episodeResult.restName, seasonResult.restName); const name = guessName(fileName, dirNames, (episodeResult === null || episodeResult === void 0 ? void 0 : episodeResult.restName) ? episodeResult : seasonResult, yearResult); return { type: 'tv', name, season: seasonResult.season, episodes: episodeResult === null || episodeResult === void 0 ? void 0 : episodeResult.episodes, year: yearResult === null || yearResult === void 0 ? void 0 : yearResult.year, }; } return undefined; }; const analyseForTv = (fileName, dirNames) => { return tvBySeasonEpisode(fileName, dirNames) || tvBySeason(fileName, dirNames); }; const analyseFilePath = (filePath) => { const { fileName, dirNames } = parseFilePath(filePath); return analyseForTv(fileName, dirNames) || analyseForMovie(fileName, dirNames) || cleanUpName((0, cleanString_1.stripEnd)(fileName)); }; exports.analyseFilePath = analyseFilePath; const extrasRegex = /\bextras?\b|\bfeaturettes?\b|\bbonus?\b|\btrailer?\b/i; const isExtras = (filePath) => { const { dirNames, fileName } = parseFilePath(filePath); return dirNames.some((dirName) => extrasRegex.test(dirName)) || extrasRegex.test(fileName); }; exports.isExtras = isExtras; const samplesRegex = /^samples?$/i; const sampleRegex = /\bsample\b/i; const isSample = (filePath) => { const { fileName, dirNames } = parseFilePath(filePath); return sampleRegex.test(fileName) || dirNames.some((dirName) => samplesRegex.test(dirName)); }; exports.isSample = isSample; const isSameRelease = (a, b) => { if (typeof a === 'string' && typeof b === 'string') { return a.toLowerCase() === b.toLowerCase(); } if ((0, exports.isAnalysedMovie)(a) && (0, exports.isAnalysedMovie)(b)) { return a.name.toLowerCase() === b.name.toLowerCase() && a.year === b.year; } if ((0, exports.isAnalysedTVShow)(a) && (0, exports.isAnalysedTVShow)(b)) { return a.name.toLowerCase() === b.name.toLowerCase() && a.year === b.year; } return false; }; exports.isSameRelease = isSameRelease; //# sourceMappingURL=analyseFilePath.js.map