@airplanegobrr/jackett-api
Version:
An api for 'Jackett' server requests
122 lines • 5.59 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.JackettService = void 0;
const axios_1 = require("axios");
const constants_1 = require("../utils/constants");
const commons_1 = require("../utils/commons");
const https = require("https");
const torznab_indexer_model_1 = require("../model/torznab-indexer.model");
const rss_result_model_1 = require("../model/rss-result.model");
const Path = require("path");
const Fs = require("fs");
const parseString = require("xml2js").parseStringPromise;
class JackettService {
constructor(settings) {
this.settings = settings;
this.axios = axios_1.default.create({
httpsAgent: new https.Agent({
rejectUnauthorized: !settings.selfSignedSSL,
}),
});
}
/**
* Retrieves full supported Torznab indexers list
*
* @return {Promise<Array<TorznabIndexerModel>>} Promise of indexers fetched from Jackett
*/
async getTorznabIndexers() {
const response = await this.axios.get(commons_1.Commons.buildUrl(constants_1.Constants.jackettAPI.getTorznabIndexers, this.settings.connectionSettings));
const parsedData = await parseString(response.data);
return parsedData.indexers.indexer.map((indexJson) => torznab_indexer_model_1.TorznabIndexerModel.fromJson(indexJson));
}
/**
* Retrieves a filtered list of configured indexers on Jackett
*
* @return {Promise<Array<TorznabIndexerModel>>} Promise of configured indexers fetched from Jackett
*/
async getConfiguredIndexers() {
const indexers = await this.getTorznabIndexers();
return indexers.filter((indexer) => indexer.configured === true);
}
/**
* Downloads a torrent file of given RssResultModel & saves it on given downloadFolderPath.
*
* @param rssResult The model of the desired torrent to download
* @param downloadFolderPath base dir for the file to be saved in
* @return promise of the download process
*/
async downloadTorrent(rssResult, downloadFolderPath) {
const fileName = rssResult.downloadLink.substr(rssResult.downloadLink.lastIndexOf(constants_1.Constants.jackettAPI.downloadFilePrefixNamePattern) + constants_1.Constants.jackettAPI.downloadFilePrefixNamePattern.length);
const path = Path.resolve(downloadFolderPath, `${fileName}${constants_1.Constants.downloadSettings.downloadNameSuffix}`);
const writer = Fs.createWriteStream(path);
const response = await this.axios.get(rssResult.downloadLink, {
responseType: "stream",
});
response.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on("finish", resolve);
writer.on("error", reject);
});
}
/**
* Fetch the latest Rss feed from a given indexer.
*
* @param indexerId Id of the indexer to fetch Rss feed from
* @return Promise<Array<RssResultModel>> Rss results from the feed of the indexer
*/
async getIndexerRss(indexerId) {
var _a;
const response = await this.axios.get(commons_1.Commons.buildUrl(constants_1.Constants.jackettAPI.getIndexerRss, this.settings.connectionSettings, {
"%indexerId%": indexerId,
}));
const parsedData = await parseString(response.data);
if (!((_a = parsedData.rss.channel[0]) === null || _a === void 0 ? void 0 : _a.item)) {
return [];
}
return parsedData.rss.channel[0].item.map((rssItem) => rss_result_model_1.RssResultModel.fromJson(rssItem));
}
/**
* Search a query and filter results by given indexers ids.
*
* @param query The search query to submit
* @param indexersId Array of index id's to filter the results with
* @return {Promise<Array<RssResultModel>>} Promise of filtered rss results
*/
async searchIndexers(query, indexersId) {
const results = await this.searchAll(query);
return results.filter((rssResult) => indexersId.includes(rssResult.indexerId));
}
/**
* Search a query in all indexers
*
* @param query The search query to submit
* @return {Promise<Array<RssResultModel>>} Promise of rss results from all configured indexers
*/
async searchAll(query) {
var _a;
const response = await this.axios.get(commons_1.Commons.buildUrl(constants_1.Constants.jackettAPI.searchAll, this.settings.connectionSettings, {
"%query%": encodeURIComponent(query),
}));
const parsedData = await parseString(response.data);
if (!((_a = parsedData.rss.channel[0]) === null || _a === void 0 ? void 0 : _a.item)) {
return [];
}
return parsedData.rss.channel[0].item.map((rssItem) => rss_result_model_1.RssResultModel.fromJson(rssItem));
}
/**
* Validate connection to the Jackett server.
* Sends a request to fetch data to see how the server behaves & tries to parse it to validate the content.
*
* @return {Promise<boolean>} State of the server connectivity
*/
async isValidServer() {
return this.searchAll(constants_1.Constants.jackettAPI.dummyValidationSearchQuery)
.then(() => {
parseString(parseString);
return true;
})
.catch(() => false);
}
}
exports.JackettService = JackettService;
//# sourceMappingURL=jackett.service.js.map