Skip to content

Instantly share code, notes, and snippets.

@AirplanegoBrr
Created December 11, 2024 18:00
Show Gist options
  • Save AirplanegoBrr/332129730b27b9c946c03656f135343f to your computer and use it in GitHub Desktop.
Save AirplanegoBrr/332129730b27b9c946c03656f135343f to your computer and use it in GitHub Desktop.
This is a simple script to help interface with the jackett API, its not done yet.
const axios = require('axios');
const { URL, URLSearchParams } = require('url');
let apiURL = process.env.JACKETT_API_URL || '';
let apiKey = process.env.JACKETT_API_KEY || '';
function toLowerCaseKeys(obj) {
if (Array.isArray(obj)) {
return obj.map(toLowerCaseKeys);
} else if (obj !== null && typeof obj === 'object') {
return Object.keys(obj).reduce((acc, key) => {
const lowerKey = key.toLowerCase()
acc[lowerKey] = toLowerCaseKeys(obj[key]);
return acc;
}, {});
}
return obj;
}
class Jackett {
/**
*
* @param {Object} settings
* @param {String} settings.apiURL
* @param {String} settings.apiKey
* @param {axios.AxiosInstance} settings.client
*/
constructor(settings = {}) {
this.apiURL = settings.apiURL
this.apiKey = settings.apiKey
this.client = settings.client || axios.create();
}
/**
* @typedef {Object} searchRequest
* @property {Array<String>} [searchRequest.trackers] Trackers to search
* @property {Array<String>} [searchRequest.categories] Category to search in
* @property {Array<String>} [searchRequest.query] Query to search for
* @property {Boolean} [searchRequest.configured] Query to search for
*/
/**
*
* @param {"indexers/all/results" | "indexers/{custom}/results" | "indexers"} sufix
* @param {searchRequest} fetchRequest
* @returns {Array<e.Search>}
*/
generateFetchURL(sufix, fetchRequest) { // indexers/all/results
const params = new URLSearchParams();
if (fetchRequest?.trackers) fetchRequest.trackers.forEach((tracker) => {
params.append('Tracker[]', tracker);
});
if (fetchRequest?.categories) fetchRequest.categories.forEach((category) => {
params.append('Category[]', category);
});
if (fetchRequest.query) {
params.set('query', fetchRequest.query);
}
if ("configured" in fetchRequest) {
params.set('configured', fetchRequest.configured ? true : false);
}
params.set('apikey', this.apiKey);
const url = new URL(`/api/v2.0/${sufix}`, this.apiURL);
url.search = params.toString();
return url.toString();
}
/**
* @typedef {Object} Indexer
* @property {String} Index.id ID of indexer
* @property {String} Index.name Name of indexer
* @property {Number} Index.status ID of indexer
* @property {Number} Index.results ID of indexer
* @property {String} Index.error ID of indexer
* @property {Number} Index.elapsedtime ID of indexer
*/
/**
* @typedef {Object} searchReturn
* @property {Array<import("@ctrl/tracker-definitions").Search["fields"]>} searchReturn.results
* @property {Array<Indexer>} searchReturn.indexers
*/
/**
*
* @param {searchRequest} fetchRequest
* @returns {Promise<searchReturn>}
*/
async search(fetchRequest) {
try {
const url = this.generateFetchURL("indexers/all/results",fetchRequest);
// console.log(url)
const response = await this.client.get(url);
let fetchResponse = response.data;
fetchResponse = toLowerCaseKeys(fetchResponse)
fetchResponse.results.forEach(result => {
if (result.firstseen) result.firstseen = new Date(result.firstseen);
if (result.publishdate) result.publishdate = new Date(result.publishdate);
});
return fetchResponse;
} catch (error) {
console.log(error)
throw new Error(`Failed to fetch data: ${error.message}`);
}
}
async searchers(){
try {
const url = this.generateFetchURL("indexers/all/results",{configured:true});
// console.log(url)
const response = await this.client.get(url);
let fetchResponse = response.data;
fetchResponse = toLowerCaseKeys(fetchResponse)
return fetchResponse.indexers
} catch (e) {
console.log(e)
console.log("error")
// console.log(e)
}
}
async getPoster(service, path) {
let url = `${this.apiURL}/img/${service}/?jackett_apikey=${this.apiKey}&path=${path}&file=poster`
// console.log(url)
let poster = await this.client.get(url, {responseType:'arraybuffer'})
return poster.data
}
async getLink(service, path, file) {
let url = `${this.apiURL}/dl/${service}/?jackett_apikey=${this.apiKey}&path=${path}&file=${file}`
// console.log(url)
let poster = await this.client.get(url, {responseType:'arraybuffer'})
return poster.data
}
}
module.exports = Jackett
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment