[addon] adds debrid meta catalogs for RD and AD

This commit is contained in:
TheBeastLT
2020-12-13 13:43:59 +01:00
parent c92fba18e7
commit 37c1c0e298
10 changed files with 213 additions and 25 deletions

View File

@@ -1,9 +1,12 @@
const AllDebridClient = require('all-debrid-api');
const { Type } = require('../lib/types');
const { isVideo, isArchive } = require('../lib/extension');
const StaticResponse = require('./static');
const { getRandomProxy, getProxyAgent, getRandomUserAgent } = require('../lib/requestHelper');
const { cacheWrapProxy, cacheUserAgent } = require('../lib/cache');
const KEY = 'alldebrid';
async function getCachedStreams(streams, apiKey) {
const options = await getDefaultOptions(apiKey);
const AD = new AllDebridClient(apiKey, options);
@@ -28,10 +31,61 @@ async function getCachedStreams(streams, apiKey) {
}, {})
}
async function getCatalog(apiKey, offset = 0) {
if (offset > 0) {
return [];
}
const options = await getDefaultOptions(apiKey);
const AD = new AllDebridClient(apiKey, options);
return AD.magnet.status()
.then(response => response.data.magnets)
.then(torrents => (torrents || [])
.filter(torrent => statusReady(torrent.statusCode))
.map(torrent => ({
id: `${KEY}:${torrent.id}`,
type: Type.OTHER,
name: torrent.filename
})));
}
async function getItemMeta(itemId, apiKey) {
const options = await getDefaultOptions(apiKey);
const AD = new AllDebridClient(apiKey, options);
return AD.magnet.status(itemId)
.then(response => response.data.magnets)
.then(torrent => ({
id: `${KEY}:${torrent.id}`,
type: Type.OTHER,
name: torrent.filename,
videos: torrent.links
.filter(file => isVideo(file.filename))
.map((file, index) => ({
id: `${KEY}:${torrent.id}:${index}`,
title: file.filename,
released: new Date(torrent.uploadDate * 1000).toISOString(),
streams: [
{ url: `${apiKey}/${torrent.hash.toLowerCase()}/${encodeURIComponent(file.filename)}/${index}` }
]
}))
}))
}
async function resolve({ ip, apiKey, infoHash, cachedEntryInfo, fileIndex }) {
console.log(`Unrestricting AllDebrid ${infoHash} [${fileIndex}]`);
const options = await getDefaultOptions(apiKey, ip);
const AD = new AllDebridClient(apiKey, options);
return _resolve(AD, infoHash, cachedEntryInfo, fileIndex)
.catch(error => {
if (errorExpiredSubscriptionError(error)) {
console.log(`Access denied to AllDebrid ${infoHash} [${fileIndex}]`);
return StaticResponse.FAILED_ACCESS;
}
return Promise.reject(`Failed AllDebrid adding torrent ${error}`);
});
}
async function _resolve(AD, infoHash, cachedEntryInfo, fileIndex) {
const torrent = await _createOrFindTorrent(AD, infoHash);
if (torrent && statusReady(torrent.statusCode)) {
return _unrestrictLink(AD, torrent, cachedEntryInfo, fileIndex);
@@ -41,10 +95,8 @@ async function resolve({ ip, apiKey, infoHash, cachedEntryInfo, fileIndex }) {
} else if (torrent && statusHandledError(torrent.statusCode)) {
console.log(`Retrying downloading to AllDebrid ${infoHash} [${fileIndex}]...`);
return _retryCreateTorrent(AD, infoHash, cachedEntryInfo, fileIndex);
} else if (torrent && errorExpiredSubscriptionError(torrent)) {
console.log(`Access denied to AllDebrid ${infoHash} [${fileIndex}]`);
return StaticResponse.FAILED_ACCESS;
}
return Promise.reject(`Failed AllDebrid adding torrent ${torrent}`);
}
@@ -126,4 +178,4 @@ function errorExpiredSubscriptionError(error) {
return ['MUST_BE_PREMIUM', 'MAGNET_MUST_BE_PREMIUM', 'FREE_TRIAL_LIMIT_REACHED'].includes(error.code);
}
module.exports = { getCachedStreams, resolve };
module.exports = { getCachedStreams, resolve, getCatalog, getItemMeta };

View File

@@ -88,6 +88,31 @@ async function resolve(parameters) {
}));
}
async function getMochCatalog(mochKey, config) {
const moch = MOCHS[mochKey];
if (!moch) {
return Promise.reject(`Not a valid moch provider: ${mochKey}`);
}
return moch.instance.getCatalog(config[moch.key], config.skip);
}
async function getMochItemMeta(mochKey, itemId, config) {
const moch = MOCHS[mochKey];
if (!moch) {
return Promise.reject(`Not a valid moch provider: ${mochKey}`);
}
return moch.instance.getItemMeta(itemId, config[moch.key])
.then(meta => {
meta.videos
.map(video => video.streams)
.reduce((a, b) => a.concat(b), [])
.forEach(stream => stream.url = `${RESOLVER_HOST}/${moch.key}/${stream.url}`)
return meta;
});
}
function populateCachedLinks(streams, mochResult) {
streams
.filter(stream => stream.infoHash)
@@ -120,4 +145,4 @@ function populateDownloadLinks(streams, mochResults) {
return streams;
}
module.exports = { applyMochs, resolve, MochOptions: MOCHS }
module.exports = { applyMochs, getMochCatalog, getMochItemMeta, resolve, MochOptions: MOCHS }

View File

@@ -1,5 +1,6 @@
const RealDebridClient = require('real-debrid-api');
const { encode } = require('magnet-uri');
const { Type } = require('../lib/types');
const { isVideo, isArchive } = require('../lib/extension');
const delay = require('./delay');
const StaticResponse = require('./static');
@@ -7,6 +8,8 @@ const { getRandomProxy, getProxyAgent, getRandomUserAgent, blacklistProxy } = re
const { cacheWrapProxy, cacheUserAgent, uncacheProxy } = require('../lib/cache');
const MIN_SIZE = 15728640; // 15 MB
const CATALOG_MAX_PAGE = 5;
const KEY = "realdebrid"
async function getCachedStreams(streams, apiKey) {
const hashes = streams.map(stream => stream.infoHash);
@@ -65,6 +68,48 @@ function _getCachedFileIds(fileIndex, hosterResults) {
return cachedTorrents.length && cachedTorrents[0] || [];
}
async function getCatalog(apiKey, offset = 0) {
if (offset > 0) {
return [];
}
const options = await getDefaultOptions(apiKey);
const RD = new RealDebridClient(apiKey, options);
let page = 1;
return RD.torrents.get(page - 1, page)
.then(torrents => torrents && torrents.length === 50 && page < CATALOG_MAX_PAGE
? RD.torrents.get(page, page = page + 1).then(nextTorrents => torrents.concat(nextTorrents)).catch(() => [])
: torrents)
.then(torrents => (torrents || [])
.filter(torrent => statusReady(torrent.status))
.map(torrent => ({
id: `${KEY}:${torrent.id}`,
type: Type.OTHER,
name: torrent.filename
})));
}
async function getItemMeta(itemId, apiKey) {
const options = await getDefaultOptions(apiKey);
const RD = new RealDebridClient(apiKey, options);
return _getTorrentInfo(RD, itemId)
.then(torrent => ({
id: `${KEY}:${torrent.id}`,
type: Type.OTHER,
name: torrent.filename,
videos: torrent.files
.filter(file => file.selected)
.filter(file => isVideo(file.path))
.map(file => ({
id: `${KEY}:${torrent.id}:${file.id}`,
title: file.path,
released: torrent.added,
streams: [
{ url: `${apiKey}/${torrent.hash.toLowerCase()}/null/${file.id - 1}` }
]
}))
}))
}
async function resolve({ apiKey, infoHash, cachedEntryInfo, fileIndex }) {
console.log(`Unrestricting RealDebrid ${infoHash} [${fileIndex}]`);
const options = await getDefaultOptions(apiKey);
@@ -215,4 +260,4 @@ async function getDefaultOptions(id) {
return { timeout: 30000, agent: agent, headers: { 'User-Agent': userAgent } };
}
module.exports = { getCachedStreams, resolve };
module.exports = { getCachedStreams, resolve, getCatalog, getItemMeta };