diff --git a/addon/lib/repository.js b/addon/lib/repository.js index 53cae5d..7a49be7 100644 --- a/addon/lib/repository.js +++ b/addon/lib/repository.js @@ -48,6 +48,10 @@ function getTorrent(infoHash) { return Torrent.findOne({ where: { infoHash: infoHash } }); } +function getTorrentWithFiles(infoHash) { + return Torrent.findOne({ where: { infoHash: infoHash }, include: [File] }); +} + function getImdbIdMovieEntries(imdbId) { return File.findAll({ where: { @@ -105,6 +109,7 @@ function getKitsuIdSeriesEntries(kitsuId, episode) { module.exports = { getTorrent, + getTorrentWithFiles, getImdbIdMovieEntries, getImdbIdSeriesEntries, getKitsuIdMovieEntries, diff --git a/addon/moch/alldebrid.js b/addon/moch/alldebrid.js index 10f7d40..5d487a3 100644 --- a/addon/moch/alldebrid.js +++ b/addon/moch/alldebrid.js @@ -64,6 +64,7 @@ async function getItemMeta(itemId, apiKey) { id: `${KEY}:${torrent.id}`, type: Type.OTHER, name: torrent.filename, + infoHash: torrent.hash.toLowerCase(), videos: torrent.links .filter(file => isVideo(file.filename)) .map((file, index) => ({ diff --git a/addon/moch/debridlink.js b/addon/moch/debridlink.js index 847e39a..3d632bb 100644 --- a/addon/moch/debridlink.js +++ b/addon/moch/debridlink.js @@ -60,6 +60,7 @@ async function getItemMeta(itemId, apiKey, ip) { id: `${KEY}:${torrent.id}`, type: Type.OTHER, name: torrent.name, + infoHash: torrent.hashString.toLowerCase(), videos: torrent.files .filter(file => isVideo(file.name)) .map((file, index) => ({ diff --git a/addon/moch/moch.js b/addon/moch/moch.js index 6f5fe47..0bfb93b 100644 --- a/addon/moch/moch.js +++ b/addon/moch/moch.js @@ -9,7 +9,7 @@ const putio = require('./putio'); const StaticResponse = require('./static'); const { cacheWrapResolvedUrl } = require('../lib/cache'); const { timeout } = require('../lib/promises'); -const { BadTokenError, streamFilename, AccessDeniedError } = require('./mochHelper'); +const { BadTokenError, streamFilename, AccessDeniedError, enrichMeta } = require('./mochHelper'); const RESOLVE_TIMEOUT = 2 * 60 * 1000; // 2 minutes const MIN_API_KEY_SYMBOLS = 15; @@ -125,6 +125,7 @@ async function getMochItemMeta(mochKey, itemId, config) { } return moch.instance.getItemMeta(itemId, config[moch.key], config.ip) + .then(meta => enrichMeta(meta)) .then(meta => { meta.videos .map(video => video.streams) diff --git a/addon/moch/mochHelper.js b/addon/moch/mochHelper.js index a17104b..0fcbfc7 100644 --- a/addon/moch/mochHelper.js +++ b/addon/moch/mochHelper.js @@ -1,3 +1,6 @@ +const repository = require('../lib/repository') + +const METAHUB_URL = 'https://images.metahub.space' const BadTokenError = { code: 'BAD_TOKEN' } const AccessDeniedError = { code: 'ACCESS_DENIED' } @@ -16,4 +19,38 @@ function streamFilename(stream) { return encodeURIComponent(filename) } -module.exports = { chunkArray, streamFilename, BadTokenError, AccessDeniedError } +async function enrichMeta(itemMeta) { + const torrent = itemMeta.infoHash && await repository.getTorrentWithFiles(itemMeta.infoHash); + const commonImdbId = torrent && mostCommonValue(torrent.files.map(file => file.imdbId)); + if (commonImdbId) { + return { + ...itemMeta, + id: commonImdbId, + logo: `${METAHUB_URL}/logo/medium/${commonImdbId}/img`, + poster: `${METAHUB_URL}/poster/medium/${commonImdbId}/img`, + background: `${METAHUB_URL}/background/medium/${commonImdbId}/img`, + videos: itemMeta.videos.map(video => { + const file = torrent.files.find(file => video.title.includes(file.title)); + if (file && file.imdbId) { + if (file.imdbSeason && file.imdbEpisode) { + video.id = `${file.imdbId}:${file.imdbSeason}:${file.imdbEpisode}`; + video.season = file.imdbSeason; + video.episode = file.imdbEpisode; + video.thumbnail = `https://episodes.metahub.space/${commonImdbId}/${video.season}/${video.episode}/w780.jpg` + } else { + video.id = file.imdbId; + video.thumbnail = `${METAHUB_URL}/background/small/${file.imdbId}/img`; + } + } + return video; + }) + } + } + return itemMeta +} + +function mostCommonValue(array) { + return array.sort((a, b) => array.filter(v => v === a).length - array.filter(v => v === b).length).pop(); +} + +module.exports = { chunkArray, streamFilename, enrichMeta, BadTokenError, AccessDeniedError } diff --git a/addon/moch/realdebrid.js b/addon/moch/realdebrid.js index 3d50267..0f0afd1 100644 --- a/addon/moch/realdebrid.js +++ b/addon/moch/realdebrid.js @@ -147,6 +147,7 @@ async function getItemMeta(itemId, apiKey, ip) { id: `${KEY}:${torrent.id}`, type: Type.OTHER, name: torrent.filename, + infoHash: torrent.hash.toLowerCase(), videos: torrent.files .filter(file => file.selected) .filter(file => isVideo(file.path))