diff --git a/addon/lib/magnetHelper.js b/addon/lib/magnetHelper.js index 48662db..306243e 100644 --- a/addon/lib/magnetHelper.js +++ b/addon/lib/magnetHelper.js @@ -1,6 +1,9 @@ const needle = require('needle'); +const magnet = require('magnet-uri'); const { getRandomProxy, getProxyAgent, getRandomUserAgent } = require('../lib/requestHelper'); const { cacheWrapProxy } = require('../lib/cache'); +const { getTorrent } = require('../lib/repository'); +const { Type } = require('../lib/types'); const TRACKERS_URL = 'https://ngosang.github.io/trackerslist/trackers_best.txt'; const ANIME_TRACKERS = [ @@ -18,6 +21,17 @@ function getAllTrackers() { return ALL_TRACKERS; } +async function getMagnetLink(infoHash) { + const torrent = getTorrent(infoHash).catch(() => ({ infoHash })); + const torrentTrackers = torrent.trackers && torrent.trackers.split(','); + const animeTrackers = torrent.type === Type.ANIME ? ALL_TRACKERS : undefined; + const trackers = torrentTrackers || animeTrackers; + + return trackers + ? magnet.encode({ infoHash: infoHash, announce: trackers }) + : magnet.encode({ infoHash: infoHash }); +} + async function initBestTrackers() { const userAgent = getRandomUserAgent(); const proxy = await cacheWrapProxy('moch', () => getRandomProxy()).catch(() => getRandomProxy()); @@ -34,4 +48,4 @@ async function initBestTrackers() { ALL_TRACKERS = BEST_TRACKERS.concat(ANIME_TRACKERS); } -module.exports = { initBestTrackers, getAllTrackers }; \ No newline at end of file +module.exports = { initBestTrackers, getAllTrackers, getMagnetLink }; \ No newline at end of file diff --git a/addon/lib/repository.js b/addon/lib/repository.js index 5cf6d0e..4160211 100644 --- a/addon/lib/repository.js +++ b/addon/lib/repository.js @@ -44,6 +44,10 @@ const File = database.define('file', Torrent.hasMany(File, { foreignKey: 'infoHash', constraints: false }); File.belongsTo(Torrent, { foreignKey: 'infoHash', constraints: false }); +function getTorrent(infoHash) { + return Torrent.findOne({ where: { infoHash: infoHash } }); +} + function getImdbIdMovieEntries(imdbId) { return File.findAll({ where: { @@ -87,4 +91,10 @@ function getKitsuIdSeriesEntries(kitsuId, episode) { }); } -module.exports = { getImdbIdMovieEntries, getImdbIdSeriesEntries, getKitsuIdMovieEntries, getKitsuIdSeriesEntries }; \ No newline at end of file +module.exports = { + getTorrent, + getImdbIdMovieEntries, + getImdbIdSeriesEntries, + getKitsuIdMovieEntries, + getKitsuIdSeriesEntries +}; \ No newline at end of file diff --git a/addon/moch/alldebrid.js b/addon/moch/alldebrid.js index 5cd8860..42454c6 100644 --- a/addon/moch/alldebrid.js +++ b/addon/moch/alldebrid.js @@ -4,6 +4,7 @@ const { isVideo, isArchive } = require('../lib/extension'); const StaticResponse = require('./static'); const { getRandomProxy, getProxyAgent, getRandomUserAgent } = require('../lib/requestHelper'); const { cacheWrapProxy, cacheUserAgent } = require('../lib/cache'); +const { getMagnetLink } = require('../lib/magnetHelper'); const KEY = 'alldebrid'; @@ -122,7 +123,8 @@ async function _findTorrent(AD, infoHash) { } async function _createTorrent(AD, infoHash) { - const uploadResponse = await AD.magnet.upload(infoHash); + const magnetLink = await getMagnetLink(infoHash); + const uploadResponse = await AD.magnet.upload(magnetLink); const torrentId = uploadResponse.data.magnets[0].id; return AD.magnet.status(torrentId).then(statusResponse => statusResponse.data.magnets); } diff --git a/addon/moch/premiumize.js b/addon/moch/premiumize.js index 27d50fa..d5fe4db 100644 --- a/addon/moch/premiumize.js +++ b/addon/moch/premiumize.js @@ -1,9 +1,9 @@ const PremiumizeClient = require('premiumize-api'); -const { encode } = require('magnet-uri'); const { isVideo } = require('../lib/extension'); const StaticResponse = require('./static'); const { getRandomProxy, getProxyAgent, getRandomUserAgent } = require('../lib/requestHelper'); const { cacheWrapProxy, cacheUserAgent } = require('../lib/cache'); +const { getMagnetLink } = require('../lib/magnetHelper'); async function getCachedStreams(streams, apiKey) { const options = await getDefaultOptions(apiKey); @@ -82,7 +82,8 @@ async function _findTorrent(PM, infoHash) { } async function _createTorrent(PM, infoHash) { - return PM.transfer.create(encode({ infoHash })).then(() => _findTorrent(PM, infoHash)); + const magnetLink = await getMagnetLink(infoHash); + return PM.transfer.create(magnetLink).then(() => _findTorrent(PM, infoHash)); } async function _retryCreateTorrent(PM, infoHash, encodedFileName, fileIndex) { diff --git a/addon/moch/putio.js b/addon/moch/putio.js index b8b7967..1e0326f 100644 --- a/addon/moch/putio.js +++ b/addon/moch/putio.js @@ -1,8 +1,8 @@ const PutioAPI = require('@putdotio/api-client').default -const { encode } = require('magnet-uri'); const { isVideo } = require('../lib/extension'); const delay = require('./delay'); const StaticResponse = require('./static'); +const { getMagnetLink } = require('../lib/magnetHelper'); async function getCachedStreams(streams, apiKey) { return streams @@ -64,7 +64,7 @@ async function _findTorrent(Putio, infoHash) { } async function _createTorrent(Putio, infoHash) { - const magnetLink = encode({ infoHash }); + const magnetLink = await getMagnetLink(infoHash); // Add the torrent and then delay for 3 secs for putio to process it and then check it's status. return Putio.Transfers.Add({ url: magnetLink }) .then(response => _getNewTorrent(Putio, response.data.transfer.id)); diff --git a/addon/moch/realdebrid.js b/addon/moch/realdebrid.js index 218cf3f..6326076 100644 --- a/addon/moch/realdebrid.js +++ b/addon/moch/realdebrid.js @@ -1,11 +1,11 @@ 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'); const { getRandomProxy, getProxyAgent, getRandomUserAgent, blacklistProxy } = require('../lib/requestHelper'); const { cacheWrapProxy, cacheUserAgent, uncacheProxy } = require('../lib/cache'); +const { getMagnetLink } = require('../lib/magnetHelper'); const MIN_SIZE = 5 * 1024 * 1024; // 5 MB const CATALOG_MAX_PAGE = 5; @@ -192,7 +192,8 @@ async function _getTorrentInfo(RD, torrentId) { } async function _createTorrentId(RD, infoHash, cachedFileIds) { - const addedMagnet = await RD.torrents.addMagnet(encode({ infoHash })); + const magnetLink = await getMagnetLink(infoHash); + const addedMagnet = await RD.torrents.addMagnet(magnetLink); await _selectTorrentFiles(RD, { id: addedMagnet.id }, cachedFileIds); return addedMagnet.id; }