diff --git a/Dockerfile b/Dockerfile index 9c64de2..0d6647a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -24,7 +24,7 @@ ENV PARSE_TORRENT_FILES= ENV REMOVE_BLACKLIST_TRACKERS= ENV INDEXER_FILTERS= ENV ADDON_NAME= - +ENV CACHE_INDEXERS_TIME= # ENV NODE_ENV=production diff --git a/README.md b/README.md index 7da8f2a..a1c261f 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,8 @@ The below options can be set as an evironment variable. | `ADD_EXTRA_TRACKERS` | false | `true` | We add some extra trackers. Check trackers.js for more info. | | `REMOVE_BLACKLIST_TRACKERS` | false | `true` | Remove trackers that are blacklisted. Download list from : [Blacklisted trackers](https://raw.githubusercontent.com/ngosang/trackerslist/master/blacklist.txt") | | `INDEXER_FILTERS` | status:healthy,test:passed | `all` | This is the filter when we fetch indexers. This config is clear text, we encode it before using. [Jackett Documentation](https://github.com/Jackett/Jackett/tree/v0.21.1594?tab=readme-ov-file#filter-indexers) | +| `CACHE_INDEXERS_TIME` | 30 | `360` | The time in minutes to cache indexers and don't call jackett to get all of them every time.| + ## Builds diff --git a/src/cache.js b/src/cache.js new file mode 100644 index 0000000..c7c2d54 --- /dev/null +++ b/src/cache.js @@ -0,0 +1,24 @@ +const cache = {}; + +function setCacheVariable(key, value, expirationMinutes) { + cache[key] = { + value, + expirationTime: Date.now() + expirationMinutes * 60 * 1000, + }; +} + +function getCacheVariable(key) { + const cachedItem = cache[key]; + + if (cachedItem && Date.now() < cachedItem.expirationTime) { + return cachedItem.value; + } + + // Return null if the cache has expired or the key doesn't exist + return null; +} + +module.exports = { + setCacheVariable, + getCacheVariable, +}; \ No newline at end of file diff --git a/src/config.js b/src/config.js index c6f8707..468b58a 100644 --- a/src/config.js +++ b/src/config.js @@ -34,6 +34,8 @@ const defaultConfig = { "downloadTorrentQueue": parseInt(process.env.DOWNLOAD_TORRENT_QUEUE) || 10, + "cacheIndexersTime": parseInt(process.env.CACHE_INDEXERS_TIME) || 30, + "jackett": { "hosts": process.env.JACKETT_HOSTS || process.env.JACKETT_HOST || "http://127.0.0.1:9117/", // JACKETT_HOST is for backwards compatibility diff --git a/src/index.js b/src/index.js index 990e6e3..b34773e 100644 --- a/src/index.js +++ b/src/index.js @@ -215,7 +215,7 @@ function streamFromParsed(tor, parsedTorrent, streamInfo, cb) { stream.title = title; stream.seeders = tor.seeders; stream.behaviorHints = { - bingieGroup: "Jackett|" + quality + "|" + infoHash, + bingieGroup: "Jackett|" + infoHash, } cb(stream); } @@ -240,7 +240,8 @@ async function addResults(info, streams, source, abortSignals) { "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7", "Accept-Encoding": "gzip, deflate", - "Accept-Language": "en-US,en;q=0.9,el;q=0.8" + "Accept-Language": "en-US,en;q=0.9,el;q=0.8", + 'Cache-Control': 'public, max-age=604800' }, timeout: config.responseTimeout, signal: signal @@ -267,7 +268,7 @@ async function addResults(info, streams, source, abortSignals) { newStream.seeders = torrent.seeders; newStream.behaviorHints = { - bingieGroup: "Jackett|" + quality + "|" + newStream.infoHash, + bingieGroup: "Jackett|" + newStream.infoHash, } streams.push(newStream); @@ -339,7 +340,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: imdbiID: ${streamInfo.imdbId} / 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: T: ${elapsedTime} / imdbiID: ${streamInfo.imdbId} / Results ${finalData.length} / Timeout: ${(elapsedTime >= config.responseTimeout)} / Search Finished: ${searchFinished} / Queue Idle: ${asyncQueue.idle()} / Pending Downloads : ${inProgressCount} / Discarded : ${(streams.length - finalData.length)}`); return respond(res, { streams: finalData, "cacheMaxAge": 1440, diff --git a/src/jackett.js b/src/jackett.js index 9a8cf7e..11755f3 100644 --- a/src/jackett.js +++ b/src/jackett.js @@ -3,9 +3,15 @@ const axios = require('axios'); const { AbortController } = require('abort-controller'); const helper = require('./helpers'); const config = require('./config'); +const { setCacheVariable, getCacheVariable } = require('./cache'); const getIndexers = async (host, apiKey, abortSignals) => { try { + const cachedData = getCacheVariable(host); + if (cachedData) { + config.debug && console.log("Loading indexers for \"" + host + "\" from cache."); + return cachedData; + } const controller = new AbortController(); abortSignals.push(controller) const signal = controller.signal; @@ -32,6 +38,7 @@ const getIndexers = async (host, apiKey, abortSignals) => { if (indexers && indexers.elements && indexers.elements[0] && indexers.elements[0].elements) { indexers = indexers.elements[0].elements; + setCacheVariable(host, indexers, config.cacheIndexersTime); return indexers; } else { console.error("Could not find indexers for ", host);