mirror of
https://github.com/knightcrawler-stremio/knightcrawler.git
synced 2024-12-20 03:29:51 +00:00
adds horriblesubs scrapper
This commit is contained in:
@@ -2,17 +2,36 @@ const torrentStream = require('torrent-stream');
|
||||
const cheerio = require('cheerio');
|
||||
const needle = require('needle');
|
||||
const parseTorrent = require('parse-torrent');
|
||||
const cloudscraper = require('cloudscraper');
|
||||
const Tracker = require("peer-search/tracker");
|
||||
|
||||
const MAX_PEER_CONNECTIONS = process.env.MAX_PEER_CONNECTIONS || 20;
|
||||
const EXTENSIONS = ["3g2", "3gp", "avi", "flv", "mkv", "mov", "mp2", "mp4", "mpe", "mpeg", "mpg", "mpv", "webm", "wmv"];
|
||||
let TRACKERS = [];
|
||||
|
||||
module.exports.init = async function() {
|
||||
TRACKERS = await getTrackerList();
|
||||
};
|
||||
|
||||
module.exports.torrentFiles = function(torrent) {
|
||||
return filesFromKat(torrent.infoHash)
|
||||
return filesFromTorrentFile(torrent)
|
||||
.catch(() => filesFromKat(torrent.infoHash))
|
||||
.catch(() => filesFromTorrentStream(torrent))
|
||||
.then((files) => files
|
||||
.filter((file) => isVideo(file))
|
||||
.map((file) => `${file.fileIndex}@@${file.path}`));
|
||||
.then((files) => files.filter((file) => isVideo(file)));
|
||||
};
|
||||
|
||||
module.exports.currentSeeders = function (torrent) {
|
||||
if (!torrent.infoHash) {
|
||||
return Promise.reject(new Error("no infoHash"));
|
||||
}
|
||||
return new Promise((resolve) =>
|
||||
Promise.all(TRACKERS.map((tracker) => new Promise((resolve) => {
|
||||
const t = new Tracker(tracker, { }, torrent.infoHash);
|
||||
t.run();
|
||||
t.on("info", (inf) => resolve([inf.seeders, inf.leechers]));
|
||||
setTimeout(() => resolve([0, 0]), 1000);
|
||||
}))
|
||||
).then((results) => resolve(results.reduce((seeders, next) => seeders + next[0], 0)))
|
||||
);
|
||||
};
|
||||
|
||||
// async function filesFromBtSeeds(infoHash) {
|
||||
@@ -24,10 +43,13 @@ module.exports.torrentFiles = function(torrent) {
|
||||
// }
|
||||
|
||||
function filesFromKat(infoHash) {
|
||||
if (!infoHash) {
|
||||
return Promise.reject(new Error("no infoHash"));
|
||||
}
|
||||
const url = `http://kat.rip/torrent/${infoHash}.html`;
|
||||
return needle('get', url, { open_timeout: 2000 })
|
||||
.then((response) => {
|
||||
if (!response.body) {
|
||||
if (!response.body || response.statusCode !== 200) {
|
||||
throw new Error('torrent not found in kat')
|
||||
}
|
||||
return response.body
|
||||
@@ -43,13 +65,41 @@ function filesFromKat(infoHash) {
|
||||
size: convertToBytes($(row).find('td[class=\'torFileSize\']').text())
|
||||
});
|
||||
});
|
||||
|
||||
if (!files[files.length - 1].size) {
|
||||
throw new Error('not full file list')
|
||||
}
|
||||
return files;
|
||||
})
|
||||
}
|
||||
|
||||
async function filesFromTorrentFile(torrent) {
|
||||
if (!torrent.torrentLink) {
|
||||
return Promise.reject(new Error("no torrentLink"));
|
||||
}
|
||||
|
||||
needle('get', torrent.torrentLink, { open_timeout: 2000 })
|
||||
.then((response) => {
|
||||
if (!response.body || response.statusCode !== 200) {
|
||||
throw new Error('torrent not found')
|
||||
}
|
||||
return response.body
|
||||
})
|
||||
.then((body) => parseTorrent(body))
|
||||
.then((info) => info.files.map((file, fileId) => ({
|
||||
fileIndex: fileId,
|
||||
name: file.name,
|
||||
path: file.path.replace(/^[^\/]+\//, ''),
|
||||
size: file.length
|
||||
})));
|
||||
}
|
||||
|
||||
async function filesFromTorrentStream(torrent) {
|
||||
if (!torrent.infoHash && !torrent.magnetLink) {
|
||||
return Promise.reject(new Error("no infoHash or magnetLink"));
|
||||
}
|
||||
return new Promise((resolve, rejected) => {
|
||||
const engine = new torrentStream(torrent.infoHash, { connections: MAX_PEER_CONNECTIONS });
|
||||
const engine = new torrentStream(torrent.magnetLink || torrent.infoHash, { connections: MAX_PEER_CONNECTIONS, trackers: TRACKERS });
|
||||
|
||||
engine.ready(() => {
|
||||
const files = engine.files
|
||||
@@ -74,7 +124,6 @@ function isVideo(title) {
|
||||
return EXTENSIONS.includes(title.path.match(/\.(\w{2,4})$/)[1]);
|
||||
}
|
||||
|
||||
|
||||
function convertToBytes(sizeString) {
|
||||
if (!sizeString) {
|
||||
return;
|
||||
@@ -88,22 +137,34 @@ function convertToBytes(sizeString) {
|
||||
return Math.floor(parseFloat(sizeString) * multiplier);
|
||||
}
|
||||
|
||||
|
||||
function dynamicTimeout(torrent) {
|
||||
if (torrent.seeders < 5) {
|
||||
return 2000;
|
||||
} else if (torrent.seeders < 10) {
|
||||
return 3000;
|
||||
} else if (torrent.seeders < 20) {
|
||||
} else if (torrent.seeders < 10) {
|
||||
return 4000;
|
||||
} else if (torrent.seeders < 30) {
|
||||
} else if (torrent.seeders < 20) {
|
||||
return 5000;
|
||||
} else if (torrent.seeders < 50) {
|
||||
} else if (torrent.seeders < 30) {
|
||||
return 7000;
|
||||
} else if (torrent.seeders < 50) {
|
||||
return 9000;
|
||||
} else if (torrent.seeders < 100) {
|
||||
return 10000;
|
||||
return 12000;
|
||||
} else {
|
||||
return 15000;
|
||||
}
|
||||
}
|
||||
|
||||
function getTrackerList() {
|
||||
return needle('get', 'https://torrents.me/tracker-list/', { open_timeout: 2000 })
|
||||
.then((response) => {
|
||||
if (!response.body || response.statusCode !== 200) {
|
||||
throw new Error('tracker list not found')
|
||||
}
|
||||
return response.body
|
||||
})
|
||||
.then((body) => cheerio.load(body))
|
||||
.then(($) => $('div[class="small-12 columns"] pre').text())
|
||||
.then((text) => text.replace(/"/g, '').trim().split('\n'))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user