mirror of
https://github.com/knightcrawler-stremio/knightcrawler.git
synced 2024-12-20 03:29:51 +00:00
[addon] add named queue to run single unrestrict
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
const { encode } = require('magnet-uri');
|
||||
const RealDebridClient = require('real-debrid-api');
|
||||
const namedQueue = require('named-queue');
|
||||
const isVideo = require('../lib/video');
|
||||
const { cacheWrapUnrestricted } = require('../lib/cache');
|
||||
|
||||
@@ -8,6 +9,10 @@ const PROXY_HOST = process.env.PROXY_HOST;
|
||||
const PROXY_USERNAME = process.env.PROXY_USERNAME;
|
||||
const PROXY_PASSWORD = process.env.PROXY_PASSWORD;
|
||||
|
||||
const unrestrictQueue = new namedQueue((task, callback) => task.method()
|
||||
.then(result => callback(false, result))
|
||||
.catch((error => callback(error))));
|
||||
|
||||
async function applyMoch(streams, apiKey) {
|
||||
const RD = new RealDebridClient(apiKey);
|
||||
const hashes = streams.map(stream => stream.infoHash);
|
||||
@@ -32,15 +37,17 @@ async function unrestrict(apiKey, infoHash, cachedFileIds, fileIndex) {
|
||||
if (!apiKey || !infoHash || !cachedFileIds || !cachedFileIds.length) {
|
||||
return Promise.reject("No valid parameters passed");
|
||||
}
|
||||
const key = `${apiKey}_${infoHash}_${fileIndex}`;
|
||||
return cacheWrapUnrestricted(key, () => _unrestrict(apiKey, infoHash, cachedFileIds, fileIndex))
|
||||
const id = `${apiKey}_${infoHash}_${fileIndex}`;
|
||||
const method = () => cacheWrapUnrestricted(id, () => _unrestrict(apiKey, infoHash, cachedFileIds, fileIndex));
|
||||
|
||||
return new Promise(((resolve, reject) => {
|
||||
unrestrictQueue.push({ id, method }, (error, result) => result ? resolve(result) : reject(error));
|
||||
}));
|
||||
}
|
||||
|
||||
async function _unrestrict(apiKey, infoHash, cachedFileIds, fileIndex) {
|
||||
console.log(`Unrestricting ${infoHash} [${fileIndex}]`);
|
||||
const RD = new RealDebridClient(apiKey);
|
||||
const torrentId = await _createOrFindTorrentId(RD, infoHash, cachedFileIds);
|
||||
console.log(`Retrieved torrentId: ${torrentId}`);
|
||||
if (torrentId) {
|
||||
const info = await RD.torrents.info(torrentId);
|
||||
const targetFile = info.files.find(file => file.id === fileIndex + 1)
|
||||
@@ -80,10 +87,7 @@ async function _unrestrictLink(RD, link) {
|
||||
return Promise.reject("No available links found");
|
||||
}
|
||||
return RD._post('unrestrict/link', { form: { link }, proxy: getProxy() })
|
||||
.then(unrestrictedLink => {
|
||||
console.log(`Unrestricted ${link} to ${unrestrictedLink.download}`);
|
||||
return Promise.resolve(unrestrictedLink.download);
|
||||
});
|
||||
.then(unrestrictedLink => unrestrictedLink.download);
|
||||
// .then(unrestrictedLink => RD.streaming.transcode(unrestrictedLink.id))
|
||||
// .then(transcodedLink => {
|
||||
// const url = transcodedLink.apple && transcodedLink.apple.full
|
||||
|
||||
Reference in New Issue
Block a user