diff --git a/addon/addon.js b/addon/addon.js index 1f80ae3..909e31b 100644 --- a/addon/addon.js +++ b/addon/addon.js @@ -131,7 +131,11 @@ function applyMochs(streams, config) { return Object.keys(config) .filter(configKey => MOCHS[configKey]) .reduce(async (streams, moch) => { - return await MOCHS[moch].applyMoch(streams, config[moch]).catch(() => streams); + return await MOCHS[moch].applyMoch(streams, config[moch]) + .catch(error => { + console.warn(error); + return streams; + }); }, streams); } diff --git a/addon/lib/video.js b/addon/lib/video.js new file mode 100644 index 0000000..5027bc7 --- /dev/null +++ b/addon/lib/video.js @@ -0,0 +1,22 @@ +const EXTENSIONS = [ + "3g2", + "3gp", + "avi", + "flv", + "mkv", + "mov", + "mp2", + "mp4", + "mpe", + "mpeg", + "mpg", + "mpv", + "webm", + "wmv", + "ogm" +]; + +module.exports = (filename) => { + const extensionMatch = filename.match(/\.(\w{2,4})$/); + return extensionMatch && EXTENSIONS.includes(extensionMatch[1].toLowerCase()); +}; \ No newline at end of file diff --git a/addon/moch/realdebrid.js b/addon/moch/realdebrid.js index 75cd158..62109da 100644 --- a/addon/moch/realdebrid.js +++ b/addon/moch/realdebrid.js @@ -1,23 +1,23 @@ const needle = require('needle'); const { encode } = require('magnet-uri'); const RealDebridClient = require('real-debrid-api'); +const isVideo = require('../lib/video'); const { cacheWrapUnrestricted } = require('../lib/cache'); -const REAL_DEBRID_API_URL = 'https://api.real-debrid.com/rest/1.0'; -const REAL_DEBRID_UNRESTRICTER_URL = 'http://localhost:7050'; +const ADDON_HOST = process.env.TORRENTIO_ADDON_HOST || 'http://localhost:7050'; async function applyMoch(streams, token) { const streamMapping = streams.reduce((map, stream) => (map[stream.infoHash] = stream, map), {}); const hashes = streams.map(stream => stream.infoHash); - const available = await _instantAvailability(hashes, token).catch(() => undefined); + const available = await _instantAvailability(hashes, token); if (available) { Object.entries(available) - .filter(([key, value]) => isCachedFileAvailable(streamMapping[key.toLowerCase()], value)) + .filter(([key, value]) => getCachedFileIds(streamMapping[key.toLowerCase()].fileIdx, value).length) .map(([key]) => key.toLowerCase()) .map(cachedInfoHash => streams.find(stream => stream.infoHash === cachedInfoHash)) .forEach(stream => { stream.name = `[RD Cached]\n${stream.name}`; - stream.url = `${REAL_DEBRID_UNRESTRICTER_URL}/realdebrid/${token}/${stream.infoHash}/${stream.fileIdx}`; + stream.url = `${ADDON_HOST}/realdebrid/${token}/${stream.infoHash}/${stream.fileIdx}`; delete stream.infoHash; delete stream.fileIndex; }) @@ -73,23 +73,29 @@ async function _createOrFindTorrentId(RD, infoHash) { }); } -function isCachedFileAvailable(stream, hosterResults) { +function getCachedFileIds(fileIndex, hosterResults) { if (!hosterResults || Array.isArray(hosterResults)) { - return false; + return []; } - return !!Object.values(hosterResults) + // if not all cached files are videos, then the torrent will be zipped to a rar + const cachedTorrent = Object.values(hosterResults) .reduce((a, b) => a.concat(b), []) - .filter(cached => isNaN(stream.fileIdx) && Object.keys(cached).length || cached[stream.fileIdx + 1]) - .length; + .filter(cached => isNaN(fileIndex) && Object.keys(cached).length || cached[fileIndex + 1]) + .find(cached => Object.values(cached).every(file => isVideo(file.filename))); + return cachedTorrent && Object.keys(cachedTorrent) || []; } async function _instantAvailability(hashes, token) { - const endpoint = `/torrents/instantAvailability/${hashes.join('/')}`; - return _request(endpoint, { token }); + const endpoint = `torrents/instantAvailability/${hashes.join('/')}`; + return _request(endpoint, { token }) + .catch(error => { + console.warn('Failed cached torrent availability request: ', error) + return undefined; + }); } async function _request(endpoint, config) { - const url = REAL_DEBRID_API_URL + endpoint; + const url = new RealDebridClient().base_url + endpoint; const method = config.method || 'get'; const headers = { 'Authorization': 'Bearer ' + config.token }; diff --git a/now.json b/now.json index 6ca4fa6..d36f5c8 100644 --- a/now.json +++ b/now.json @@ -21,7 +21,8 @@ }, "env": { "MONGODB_URI": "@mongodb-uri", - "DATABASE_URI": "@database-uri" + "DATABASE_URI": "@database-uri", + "TORRENTIO_ADDON_HOST": "@torrentio-addon-host" } }