mirror of
https://github.com/knightcrawler-stremio/knightcrawler.git
synced 2024-12-20 03:29:51 +00:00
[addon] adds premiumize moch
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
const realdebrid = require('./realdebrid');
|
||||
const premiumize = require('./premiumize');
|
||||
|
||||
const RESOLVER_HOST = process.env.RESOLVER_HOST || 'http://localhost:7050';
|
||||
const MOCHS = {
|
||||
@@ -6,6 +7,11 @@ const MOCHS = {
|
||||
key: 'realdebrid',
|
||||
instance: realdebrid,
|
||||
shortName: 'RD'
|
||||
},
|
||||
'premiumize': {
|
||||
key: 'premiumize',
|
||||
instance: premiumize,
|
||||
shortName: 'PM'
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
72
addon/moch/premiumize.js
Normal file
72
addon/moch/premiumize.js
Normal file
@@ -0,0 +1,72 @@
|
||||
const PremiumizeClient = require('premiumize-api');
|
||||
const namedQueue = require('named-queue');
|
||||
const { encode } = require('magnet-uri');
|
||||
const isVideo = require('../lib/video');
|
||||
const { getRandomProxy, getRandomUserAgent } = require('../lib/request_helper');
|
||||
const { cacheWrapResolvedUrl, cacheWrapProxy, cacheUserAgent } = require('../lib/cache');
|
||||
|
||||
const unrestrictQueue = new namedQueue((task, callback) => task.method()
|
||||
.then(result => callback(false, result))
|
||||
.catch((error => callback(error))));
|
||||
|
||||
async function getCachedStreams(streams, apiKey) {
|
||||
const options = await getDefaultOptions(apiKey);
|
||||
const PM = new PremiumizeClient(apiKey, options);
|
||||
const hashes = streams.map(stream => stream.infoHash);
|
||||
const available = await PM.cache.check(hashes)
|
||||
.catch(error => {
|
||||
console.warn('Failed Premiumize cached torrent availability request: ', error);
|
||||
return undefined;
|
||||
});
|
||||
return available && streams
|
||||
.reduce((cachedStreams, stream, index) => {
|
||||
const isCached = available.response[index];
|
||||
if (isCached) {
|
||||
const streamTitleParts = stream.title.replace(/\n👤.*/s, '').split('\n');
|
||||
const fileName = streamTitleParts[streamTitleParts.length - 1];
|
||||
const fileIndex = streamTitleParts.length === 2 ? stream.fileIdx : null;
|
||||
const encodedFileName = encodeURIComponent(fileName);
|
||||
cachedStreams[stream.infoHash] = `${apiKey}/${stream.infoHash}/${encodedFileName}/${fileIndex}`;
|
||||
}
|
||||
return cachedStreams;
|
||||
}, {})
|
||||
}
|
||||
|
||||
async function resolve({ ip, apiKey, infoHash, cachedEntryInfo, fileIndex }) {
|
||||
if (!apiKey || !infoHash || !cachedEntryInfo) {
|
||||
return Promise.reject("No valid parameters passed");
|
||||
}
|
||||
const id = `${apiKey}_${infoHash}_${fileIndex}`;
|
||||
const method = () => cacheWrapResolvedUrl(id, () => _unrestrict(ip, apiKey, infoHash, cachedEntryInfo, fileIndex));
|
||||
|
||||
return new Promise(((resolve, reject) => {
|
||||
unrestrictQueue.push({ id, method }, (error, result) => result ? resolve(result) : reject(error));
|
||||
}));
|
||||
}
|
||||
|
||||
async function _unrestrict(ip, apiKey, infoHash, encodedFileName, fileIndex) {
|
||||
console.log(`Unrestricting ${infoHash} [${fileIndex}]`);
|
||||
const options = await getDefaultOptions(apiKey, ip);
|
||||
const PM = new PremiumizeClient(apiKey, options);
|
||||
const cachedTorrent = await PM.transfer.directDownload(encode({ infoHash }));
|
||||
if (cachedTorrent.content && cachedTorrent.content.length) {
|
||||
const targetFileName = decodeURIComponent(encodedFileName);
|
||||
const videos = cachedTorrent.content.filter(file => isVideo(file.path));
|
||||
const targetVideo = Number.isInteger(fileIndex)
|
||||
? videos.find(video => video.path.includes(targetFileName))
|
||||
: videos.sort((a, b) => b.size - a.size)[0];
|
||||
const unrestrictedLink = targetVideo.stream_link || targetVideo.link;
|
||||
console.log(`Unrestricted ${infoHash} [${fileIndex}] to ${unrestrictedLink}`);
|
||||
return unrestrictedLink;
|
||||
}
|
||||
return Promise.reject("Failed Premiumize adding torrent");
|
||||
}
|
||||
|
||||
async function getDefaultOptions(id, ip) {
|
||||
const userAgent = await cacheUserAgent(id, () => getRandomUserAgent()).catch(() => getRandomUserAgent());
|
||||
const proxy = await cacheWrapProxy('moch', () => getRandomProxy()).catch(() => getRandomProxy());
|
||||
|
||||
return { proxy: proxy, headers: { 'User-Agent': userAgent } };
|
||||
}
|
||||
|
||||
module.exports = { getCachedStreams, resolve };
|
||||
@@ -15,7 +15,7 @@ async function getCachedStreams(streams, apiKey) {
|
||||
const hashes = streams.map(stream => stream.infoHash);
|
||||
const available = await RD.torrents.instantAvailability(hashes)
|
||||
.catch(error => {
|
||||
console.warn('Failed cached torrent availability request: ', error);
|
||||
console.warn('Failed RealDebrid cached torrent availability request: ', error);
|
||||
return undefined;
|
||||
});
|
||||
return available && streams
|
||||
@@ -72,7 +72,7 @@ async function _unrestrict(apiKey, infoHash, cachedFileIds, fileIndex) {
|
||||
console.log(`Unrestricted ${infoHash} [${fileIndex}] to ${unrestrictedLink}`);
|
||||
return unrestrictedLink;
|
||||
}
|
||||
return Promise.reject("Failed adding torrent");
|
||||
return Promise.reject("Failed RealDebrid adding torrent");
|
||||
}
|
||||
|
||||
async function _createOrFindTorrentId(RD, infoHash, cachedFileIds) {
|
||||
|
||||
8
addon/package-lock.json
generated
8
addon/package-lock.json
generated
@@ -1742,6 +1742,14 @@
|
||||
"integrity": "sha1-IZMqVJ9eUv/ZqCf1cOBL5iqX2lQ=",
|
||||
"dev": true
|
||||
},
|
||||
"premiumize-api": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/premiumize-api/-/premiumize-api-1.0.0.tgz",
|
||||
"integrity": "sha512-Qpi/0HF49t4s9qJtTpvQFNseIrTrTU5UniGzxvz5yJ7UYPmjhfbKMWKSs1E7DE3G4xB9eN1TlMZBhj86Xem4gA==",
|
||||
"requires": {
|
||||
"request": "^2.83.0"
|
||||
}
|
||||
},
|
||||
"process-nextick-args": {
|
||||
"version": "2.0.1",
|
||||
"resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz",
|
||||
|
||||
@@ -12,12 +12,13 @@
|
||||
"cache-manager": "^2.11.1",
|
||||
"cache-manager-mongodb": "^0.2.2",
|
||||
"express-rate-limit": "^5.1.1",
|
||||
"magnet-uri": "^5.1.7",
|
||||
"named-queue": "^2.2.1",
|
||||
"needle": "^2.2.4",
|
||||
"magnet-uri": "^5.1.7",
|
||||
"parse-torrent-title": "git://github.com/TheBeastLT/parse-torrent-title.git#49be4a2b4ab14e26fca4e52de82f6ad08948fdc7",
|
||||
"pg": "^7.8.2",
|
||||
"pg-hstore": "^2.3.2",
|
||||
"premiumize-api": "^1.0.0",
|
||||
"real-debrid-api": "git://github.com/TheBeastLT/node-real-debrid.git#935a5c23ae809edbcd2a111526a7f74d6767c50d",
|
||||
"sequelize": "^4.43.0",
|
||||
"stremio-addon-sdk": "^1.6.1",
|
||||
|
||||
Reference in New Issue
Block a user