torrent-api-ts
Version:
[](https://github.com/semantic-release/semantic-release) [](https
138 lines • 4.5 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Token_1 = require("./Token/Token");
const axios_1 = require("axios");
const SearchParams_1 = require("./Request/SearchParams");
const Error_1 = require("./Error/Error");
const PromiseThrottler_1 = require("./Queue/PromiseThrottler");
class TorrentSearch {
/**
* The search object
* @param {string} appName needed TorrentApi to identify the client
* @param {string} userAgent in case the one we set stop workin (chrome vers. 60)
* @param {string} endpoint if the end point change and we didn't update fast enough, you can change it here
*/
constructor(appName, userAgent, endpoint) {
this._userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36';
this._token = Token_1.Token.expired();
this._queue = new PromiseThrottler_1.PromiseThrottler(2000);
this._endpoint = 'https://torrentapi.org/pubapi_v2.php';
this._appName = appName;
if (userAgent) {
this._userAgent = userAgent;
}
if (endpoint) {
this._endpoint = endpoint;
}
}
/**
* Change delay between each request
*
* Only change this if you know what you're doing
* @param {number} value
*/
set delayBetweenRequests(value) {
this._queue.delayBetweenPromise = value;
}
/**
* Advanced search with possibility to set all the different parameters
* @param {SearchParams} search
* @returns {Promise<TorrentCollection>}
*/
searchAdvanced(search) {
return this._request(search);
}
/**
* Search and in which category
* @param {string} search
* @param {SearchCategory} category
* @returns {Promise<TorrentCollection>}
*/
search(search, category) {
return this.searchAdvanced(new SearchParams_1.DefaultSearch(search, category));
}
/**
* Ensure we have a valid token to do requests on their API
* @returns {Promise<Token>}
*/
ensureToken() {
if (this._token.hasExpired()) {
return this._delayedRequest({ get_token: 'get_token' })
.then(data => {
return new Token_1.Token(data.token);
})
.then((token) => {
this._token = token;
return token;
})
.catch(e => {
console.warn("Couldn't get the token");
throw e;
});
}
return Promise.resolve(this._token);
}
/**
* Do request on the api
* @param {RequestParams} params
* @returns {Promise<T>}
* @private
*/
_request(params) {
return this.ensureToken()
.then(() => {
return this._delayedRequest(params);
})
.catch(e => {
if (e instanceof Error_1.ErrorResponse && e.code === 4) {
this._token.invalidate();
return this._request(params);
}
return Promise.reject(e);
});
}
/**
* The API is rate limited, be sure to only do the request in the right time to avoid hitting the rate limiting
* @param {RequestParams} params
* @returns {Promise<T>}
* @private
*/
_delayedRequest(params) {
return this._queue.add(() => this._processRequest(params));
}
/**
* Set all the query parameters for the request
*
* Check also for token expired
* @param {RequestParams} params
* @returns {Promise<T>}
* @private
*/
_processRequest(params) {
if (!this._token.hasExpired()) {
params.token = this._token.token;
}
params.app_id = this._appName;
const options = {
method: 'GET',
url: this._endpoint,
params: params,
headers: {
'User-Agent': this._userAgent
}
};
return axios_1.default.request(options)
.then((response) => response.data)
.then((data) => {
if (data.error_code) {
return Promise.reject(new Error_1.ErrorResponse(data.error, data.error_code));
}
return data;
})
.then((data) => {
return data;
});
}
}
exports.default = TorrentSearch;
//# sourceMappingURL=torrent-api-ts.js.map