[addon] filter realdebrid cached entries to contain ony videos

This commit is contained in:
TheBeastLT
2020-03-17 16:29:36 +01:00
parent ea0250c910
commit 727c763e43
4 changed files with 48 additions and 15 deletions

View File

@@ -131,7 +131,11 @@ function applyMochs(streams, config) {
return Object.keys(config) return Object.keys(config)
.filter(configKey => MOCHS[configKey]) .filter(configKey => MOCHS[configKey])
.reduce(async (streams, moch) => { .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); }, streams);
} }

22
addon/lib/video.js Normal file
View File

@@ -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());
};

View File

@@ -1,23 +1,23 @@
const needle = require('needle'); const needle = require('needle');
const { encode } = require('magnet-uri'); const { encode } = require('magnet-uri');
const RealDebridClient = require('real-debrid-api'); const RealDebridClient = require('real-debrid-api');
const isVideo = require('../lib/video');
const { cacheWrapUnrestricted } = require('../lib/cache'); const { cacheWrapUnrestricted } = require('../lib/cache');
const REAL_DEBRID_API_URL = 'https://api.real-debrid.com/rest/1.0'; const ADDON_HOST = process.env.TORRENTIO_ADDON_HOST || 'http://localhost:7050';
const REAL_DEBRID_UNRESTRICTER_URL = 'http://localhost:7050';
async function applyMoch(streams, token) { async function applyMoch(streams, token) {
const streamMapping = streams.reduce((map, stream) => (map[stream.infoHash] = stream, map), {}); const streamMapping = streams.reduce((map, stream) => (map[stream.infoHash] = stream, map), {});
const hashes = streams.map(stream => stream.infoHash); const hashes = streams.map(stream => stream.infoHash);
const available = await _instantAvailability(hashes, token).catch(() => undefined); const available = await _instantAvailability(hashes, token);
if (available) { if (available) {
Object.entries(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(([key]) => key.toLowerCase())
.map(cachedInfoHash => streams.find(stream => stream.infoHash === cachedInfoHash)) .map(cachedInfoHash => streams.find(stream => stream.infoHash === cachedInfoHash))
.forEach(stream => { .forEach(stream => {
stream.name = `[RD Cached]\n${stream.name}`; 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.infoHash;
delete stream.fileIndex; 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)) { 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), []) .reduce((a, b) => a.concat(b), [])
.filter(cached => isNaN(stream.fileIdx) && Object.keys(cached).length || cached[stream.fileIdx + 1]) .filter(cached => isNaN(fileIndex) && Object.keys(cached).length || cached[fileIndex + 1])
.length; .find(cached => Object.values(cached).every(file => isVideo(file.filename)));
return cachedTorrent && Object.keys(cachedTorrent) || [];
} }
async function _instantAvailability(hashes, token) { async function _instantAvailability(hashes, token) {
const endpoint = `/torrents/instantAvailability/${hashes.join('/')}`; const endpoint = `torrents/instantAvailability/${hashes.join('/')}`;
return _request(endpoint, { token }); return _request(endpoint, { token })
.catch(error => {
console.warn('Failed cached torrent availability request: ', error)
return undefined;
});
} }
async function _request(endpoint, config) { 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 method = config.method || 'get';
const headers = { 'Authorization': 'Bearer ' + config.token }; const headers = { 'Authorization': 'Bearer ' + config.token };

View File

@@ -21,7 +21,8 @@
}, },
"env": { "env": {
"MONGODB_URI": "@mongodb-uri", "MONGODB_URI": "@mongodb-uri",
"DATABASE_URI": "@database-uri" "DATABASE_URI": "@database-uri",
"TORRENTIO_ADDON_HOST": "@torrentio-addon-host"
} }
} }