mirror of
https://github.com/knightcrawler-stremio/knightcrawler.git
synced 2024-12-20 03:29:51 +00:00
[scraper] fixes torrent size update method for horriblesubs
This commit is contained in:
@@ -31,56 +31,15 @@ module.exports.updateCurrentSeeders = function (torrent) {
|
||||
ready();
|
||||
});
|
||||
}, callback);
|
||||
}).then((seeders) => ({ ...torrent, seeders: torrent.seeders || seeders }));
|
||||
}).then(seeders => ({ ...torrent, seeders: torrent.seeders || seeders }));
|
||||
};
|
||||
|
||||
module.exports.updateTorrentSize = function (torrent) {
|
||||
if (!torrent.infoHash && !torrent.magnetLink) {
|
||||
return Promise.reject(new Error("no infoHash or magnetLink"));
|
||||
}
|
||||
const magnetLink = torrent.magnetLink || decode.encode({ infoHash: torrent.infoHash });
|
||||
return new Promise((resolve, rejected) => {
|
||||
const engine = new torrentStream(magnetLink, { connections: MAX_PEER_CONNECTIONS });
|
||||
|
||||
engine.ready(() => {
|
||||
const size = engine.torrent.length;
|
||||
engine.destroy();
|
||||
resolve({ size });
|
||||
});
|
||||
setTimeout(() => {
|
||||
engine.destroy();
|
||||
rejected(new Error('No available connections for torrent!'));
|
||||
}, SEEDS_CHECK_TIMEOUT);
|
||||
}).then((size) => ({ ...torrent, size }));
|
||||
return filesAndSizeFromTorrentStream(torrent, SEEDS_CHECK_TIMEOUT)
|
||||
.then(result => ({ ...torrent, size: result.size, files: result.files }));
|
||||
};
|
||||
|
||||
module.exports.sizeAndFiles = function (torrent) {
|
||||
if (!torrent.infoHash && !torrent.magnetLink) {
|
||||
return Promise.reject(new Error("no infoHash or magnetLink"));
|
||||
}
|
||||
// const magnet = decode.encode({ infoHash: torrent.infoHash, announce: torrent.trackers });
|
||||
return new Promise((resolve, rejected) => {
|
||||
const engine = new torrentStream(torrent.infoHash, { connections: MAX_PEER_CONNECTIONS });
|
||||
|
||||
engine.ready(() => {
|
||||
const files = engine.files
|
||||
.map((file, fileId) => ({
|
||||
fileIndex: fileId,
|
||||
name: file.name,
|
||||
path: file.path.replace(/^[^\/]+\//, ''),
|
||||
size: file.length
|
||||
}));
|
||||
const size = engine.torrent.length;
|
||||
|
||||
engine.destroy();
|
||||
resolve({ files, size });
|
||||
});
|
||||
setTimeout(() => {
|
||||
engine.destroy();
|
||||
rejected(new Error('No available connections for torrent!'));
|
||||
}, 20000);
|
||||
});
|
||||
};
|
||||
module.exports.sizeAndFiles = torrent => filesAndSizeFromTorrentStream(torrent, 20000);
|
||||
|
||||
module.exports.torrentFiles = function (torrent) {
|
||||
return getFilesFromObject(torrent)
|
||||
@@ -131,11 +90,16 @@ async function filesFromTorrentFile(torrent) {
|
||||
}
|
||||
|
||||
async function filesFromTorrentStream(torrent) {
|
||||
return filesAndSizeFromTorrentStream(torrent, 60000).then(result => result.files);
|
||||
}
|
||||
|
||||
function filesAndSizeFromTorrentStream(torrent, timeout = 60000) {
|
||||
if (!torrent.infoHash && !torrent.magnetLink) {
|
||||
return Promise.reject(new Error("no infoHash or magnetLink"));
|
||||
}
|
||||
// const magnet = decode.encode({ infoHash: torrent.infoHash, announce: torrent.trackers });
|
||||
return new Promise((resolve, rejected) => {
|
||||
const engine = new torrentStream(torrent.magnetLink || torrent.infoHash, { connections: MAX_PEER_CONNECTIONS });
|
||||
const engine = new torrentStream(torrent.infoHash, { connections: MAX_PEER_CONNECTIONS });
|
||||
|
||||
engine.ready(() => {
|
||||
const files = engine.files
|
||||
@@ -145,14 +109,15 @@ async function filesFromTorrentStream(torrent) {
|
||||
path: file.path.replace(/^[^\/]+\//, ''),
|
||||
size: file.length
|
||||
}));
|
||||
const size = engine.torrent.length;
|
||||
|
||||
engine.destroy();
|
||||
resolve(files);
|
||||
resolve({ files, size });
|
||||
});
|
||||
setTimeout(() => {
|
||||
engine.destroy();
|
||||
rejected(new Error('No available connections for torrent!'));
|
||||
}, 60000);
|
||||
}, timeout);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user