Skip to content

Commit

Permalink
multiple (#57)
Browse files Browse the repository at this point in the history
* multiple

* small

* nocache

* revert id
  • Loading branch information
tsaridas authored Mar 25, 2024
1 parent cb59bd4 commit f05268c
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 19 deletions.
5 changes: 2 additions & 3 deletions src/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ function getCacheVariable(key, renewalMinutes = 0) {
const cachedItem = cache[key];

if (cachedItem && Date.now() < cachedItem.expirationTime) {
// Renew the expiration time
if (renewalMinutes > 0) {
cachedItem.expirationTime = Date.now() + renewalMinutes * 60 * 1000;
if (renewalMinutes > 0 && cache[key]) {
cache[key].expirationTime = Date.now() + renewalMinutes * 60 * 1000;
}
return cachedItem.value;
}
Expand Down
4 changes: 4 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const defaultConfig = {

"searchByType": process.env.SEARCH_BY_TYPE || false,

"dontSearchByYear": process.env.DONT_SEARCH_BY_YEAR || false,

"responseTimeout": parseInt(process.env.RESPONSE_TIMEOUT) || 8000,

"addonPort": parseInt(process.env.PORT) || 7000,
Expand All @@ -30,6 +32,8 @@ const defaultConfig = {

"maximumSize": process.env.MAX_SIZE || "5GB",

"ignoreTitles": process.env.IGNORE_TITLES || "\\b(Telecine|CAMRip)\\b|\\b(?:HD-?)?T(?:ELE)?S(?:YNC)?\\b|\\b(?:HD-?)?CAM\\b|\\b(?:HQ-?)?CAM\\b",

"downloadTorrentQueue": parseInt(process.env.DOWNLOAD_TORRENT_QUEUE) || 10,

"cacheIndexersTime": parseInt(process.env.CACHE_INDEXERS_TIME) || 30,
Expand Down
9 changes: 7 additions & 2 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,18 @@ const helper = {
},

findQuality: (tag) => {
const regex = /DLRip|HDTV|\b(DivX|XviD)\b|\b(?:DL|WEB|BD|BR)MUX\b|\bWEB-?Rip\b|\bWEB-?DL\b|\bBluray\b|\bVHSSCR\b|\bR5\b|\bPPVRip\b|\bTC\b|\b(?:HD-?)?TVRip\b|\bDVDscr\b|\bDVD(?:R[0-9])?\b|\bDVDRip\b|\bBDRip\b|\bBRRip\b|\bHD-?Rip\b|\b(?:HD-?)?T(?:ELE)?S(?:YNC)?\b|\b(?:HD-?)?CAM\b|(4k)|([0-9]{3,4}[pi])/i;
const regex = /DLRip|HDTV|\b(DivX|XviD)\b|\b(?:DL|WEB|BD|BR)MUX\b|\bWEB-?Rip\b|\bWEB-?DL\b|\b(WEB|Telecine|CAMRip|HQCAM)\b|\bBluray\b|\bVHSSCR\b|\bR5\b|\bPPVRip\b|\bTC\b|\b(?:HD-?)?TVRip\b|\bDVDscr\b|\bDVD(?:R[0-9])?\b|\bDVDRip\b|\bBDRip\b|\bBRRip\b|\bHD-?Rip\b|\b(?:HD-?)?T(?:ELE)?S(?:YNC)?\b|\b(?:HD-?)?CAM\b/i;
const regexP = /(4k)|([0-9]{3,4}[pi])/i;
const match = tag.match(regex);
const matchP = tag.match(regexP);
let quality = "";
if (match !== null) {
quality = match[0];
} else if (matchP !== null) {
quality = matchP[0];
}
return quality
return quality;

},

normalizeTitle: (torrent, info) => {
Expand Down
23 changes: 9 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const manifest = {
"version": version,

"name": config.addonName,
"description": "Stremio Add-on to get torrent results from Jackett",
"description": "Stremio Add-on to get torrent results from Jackett.",

"icon": "https://svgur.com/i/12Ss.svg",
"logo": "https://uxwing.com/wp-content/themes/uxwing/download/clothes-and-accessories/hoodie-jacket-icon.png",
Expand All @@ -54,10 +54,7 @@ const manifest = {
"configurationRequired": false
},

// works for both movies and series
"types": ["movie", "series"],

// prefix of item IDs (ie: "tt0032138")
"idPrefixes": ["tt", "tmdb"],

"catalogs": []
Expand Down Expand Up @@ -187,7 +184,7 @@ function streamFromParsed(tor, parsedTorrent, streamInfo, cb) {
} else {
let regEx = null;
if (streamInfo.type === 'movie') {
regEx = new RegExp(`${streamInfo.name.split(' ').join('.*')}.*${config.searchByYear && streamInfo.year ? streamInfo.year : ''}.*`, 'i');
regEx = new RegExp(`${streamInfo.name.split(' ').join('.*')}.*${!config.dontSearchByYear && streamInfo.year ? streamInfo.year : ''}.*`, 'i');
} else {
regEx = new RegExp(`${streamInfo.name.split(' ').join('.*')}.*${helper.episodeTag(streamInfo.season, streamInfo.episode)}.*`, 'i');
}
Expand Down Expand Up @@ -313,10 +310,10 @@ addon.get('/stream/:type/:id.json', async (req, res) => {
config.debug && console.log("Received request for :", req.params.type, req.params.id);

// cache
if (config.cacheResultsTime && config.cacheResultsTime != 0) {
if (config.cacheResultsTime && config.cacheResultsTime != 0 && !req.headers['no-cache']) {
const cached = getCacheVariable(req.params.id, config.cacheResultsTime);
if (cached) {
console.log("C: Serving cached results for " + req.params.type + " id: " + req.params.id);
console.log("C: " + req.params.id + " cached.");
return respond(res, {
streams: cached,
"cacheMaxAge": 7200,
Expand Down Expand Up @@ -366,9 +363,7 @@ addon.get('/stream/:type/:id.json', async (req, res) => {
});
}

console.log(`Q: ${streamInfo.Id} / title: ${streamInfo.name} / type: ${streamInfo.type} / year: ${streamInfo.year}` +
(streamInfo.season && streamInfo.episode ? ` / season: ${streamInfo.season} / episode: ${streamInfo.episode}` : '') +
'.');
console.log(`Q: ${req.params.id} / title: ${streamInfo.name} / year: ${streamInfo.year}`);

let inProgressCount = 0;
let searchFinished = false;
Expand All @@ -387,7 +382,7 @@ addon.get('/stream/:type/:id.json', async (req, res) => {
clearInterval(intervalId);
const finalData = processTorrentList(streams);
config.debug && console.log("Sliced & Sorted data ", finalData);
console.log(`A: ${streamInfo.Id} / time: ${elapsedTime} / results: ${finalData.length} / timeout: ${(elapsedTime >= config.responseTimeout)} / search finished: ${searchFinished} / queue idle: ${asyncQueue.idle()} / pending downloads: ${inProgressCount} / discarded: ${(streams.length - finalData.length)}`);
console.log(`A: ${req.params.id} / time: ${elapsedTime} / results: ${finalData.length} / timeout: ${(elapsedTime >= config.responseTimeout)} / search finished: ${searchFinished} / queue idle: ${asyncQueue.idle()} / pending downloads: ${inProgressCount} / discarded: ${(streams.length - finalData.length)}`);
if (finalData.length > 0) {
res.setHeader('Cache-Control', 'max-age=7200, stale-while-revalidate=14400, stale-if-error=604800, public');
// Set cache-related headers if "streams" contains data
Expand All @@ -408,12 +403,12 @@ addon.get('/stream/:type/:id.json', async (req, res) => {
});
}
}
config.debug && console.log(`s: id: ${streamInfo.Id} / time pending: ${(config.responseTimeout - elapsedTime)} / search finished: ${searchFinished} / queue idle: ${asyncQueue.idle()} / pending downloads: ${inProgressCount} / processed streams: ${streams.length}`);
config.debug && console.log(`S: id: ${streamInfo.Id} / time pending: ${(config.responseTimeout - elapsedTime)} / search finished: ${searchFinished} / queue idle: ${asyncQueue.idle()} / pending downloads: ${inProgressCount} / processed streams: ${streams.length}`);

}, config.interval);

const processMagnets = async (task) => {
if (requestSent) { // Check the flag before processing each task
if (requestSent) {
return;
}
const uri = task.magneturl || task.link;
Expand All @@ -436,7 +431,7 @@ addon.get('/stream/:type/:id.json', async (req, res) => {
config.debug && console.log("Processing link: ", task.link);
const response = await axios.get(task.link, {
timeout: config.responseTimeout, // we don't want to overdo it here and neither set something in config. Request should timeout anyway.
maxRedirects: 0, // Equivalent to 'redirect: 'manual'' in fetch
maxRedirects: 0,
validateStatus: null,
signal: signal,
responseType: 'arraybuffer', // Specify the response type as 'arraybuffer'
Expand Down
9 changes: 9 additions & 0 deletions src/jackett.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const search = async (query, abortSignals, cb, end) => {
let countFinished = 0;
let searchedIndexers = {};
let sortedReults = [];
let ignoreTitles = null;

const simpleName = encodeURIComponent(helper.simpleName(query.name));
// This is not ideal and should probably be moved to a configuration file, but currently, I cannot think of any other items that are miscategorized.
Expand All @@ -109,6 +110,10 @@ const search = async (query, abortSignals, cb, end) => {
}
}

if (config.ignoreTitles) {
ignoreTitles = new RegExp(config.ignoreTitles, 'i');
}

await Promise.all(hostsAndApiKeys.map(async ({ host, apiKey }) => {
const apiIndexersArray = await getIndexers(host, apiKey, abortSignals);

Expand Down Expand Up @@ -184,6 +189,10 @@ const search = async (query, abortSignals, cb, end) => {
newObj[ofInterestElm] = tempObj[ofInterestElm];
});

if (ignoreTitles && ignoreTitles.test(newObj.title)) {
return;
}

const toInt = ['seeders', 'peers', 'size', 'files'];

toInt.forEach(toIntElm => {
Expand Down

0 comments on commit f05268c

Please sign in to comment.