Skip to content

Commit

Permalink
cache indexers (#43)
Browse files Browse the repository at this point in the history
* cache indexers

* behaviourhints
  • Loading branch information
tsaridas authored Jan 28, 2024
1 parent 652cc08 commit 1e0dde6
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions src/cache.js
Original file line number Diff line number Diff line change
@@ -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,
};
2 changes: 2 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions src/jackett.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit 1e0dde6

Please sign in to comment.