This repository was archived by the owner on Apr 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
159 lines (135 loc) · 3.84 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const got = require('got');
const util = require('./util');
class RarBGApi {
/**
* @typedef {Object} RarBGConfig
* @property {string} baseUrl - API base URL
*/
/**
* Create a new instance of RarBGApi
* @param {RarBGConfig} config - API configuration
*/
constructor({ baseUrl = 'https://torrentapi.org/pubapi_v2.php' } = {}) {
/**
* RarBG base URL.
* @type {string}
* @readonly
* @private
*/
this._baseUrl = baseUrl;
/**
* TorrentAPI Token.
* Don't use this variable directly!
* Use the {@link _getToken} method instead!
* @type {string}
* @private
*/
this._token = null;
}
/**
* Get current token/refresh current token.
* @param {boolean} force - Whether to force a refresh
* @private
*/
async _getToken(force = false) {
if(!this._token || refreshToken) {
const opts = {
query: {
get_token: 'get_token'
},
json: true
}
const res = await got.get(this._baseUrl, opts);
this._token = res.body.token;
}
return this._token;
}
/**
* Make a get request with correct token.
* @param {string} endpoint - The endpoint to make requests to
* @param {Object} query - The query parameters of the request
* @param {boolean} refreshToken - Whether to refresh the token
* @private
*/
async _get(query, refreshToken = false) {
const token = await this._getToken(refreshToken);
util.clean(query);
const opts = {
query: {
...query,
token
},
json: true
}
const res = await got.get(this._baseUrl, opts);
if(res.body.error_code == 4) {
// Token expired
return this._get(query, true);
}
if(res.body.error_code == 5) {
// Too many requests
await util.sleep(2);
return this._get(query);
}
return res;
}
/**
* Internal function for other getters.
* Will build query object and do some checks
* @param {string} mode - The mode (list, search)
* @param {Object} query - Optional query
* @private
*/
async _getAll(mode = 'list', {
category,
limit = 25,
sort = 'last',
min_seeders,
min_leechers,
format = 'json_extended',
ranked
} = {}) {
if(Array.isArray(category)) {
category = category.join(';');
}
if(!~[25, 50, 100].indexOf(limit)) {
throw new Error(`${limit} is not a valid value for limit.`);
}
if(!~['last', 'seeders', 'leechers'].indexOf(sort)) {
throw new Error(`${sort} is not a valid value for sort`);
}
if(!~['json', 'json_extended'].indexOf(format)) {
throw new Error(`${sort} is not a valid value for format`);
}
const result = await this._get({
mode,
category,
limit,
sort,
min_seeders,
min_leechers,
format,
ranked
});
return result.body.torrent_results;
}
/**
* Get list of torrents from RarBG.
* @param {Object} query - Optional query
*/
async getList(query) {
return this._getAll('list', query);
}
/**
* Search on RarBG by keywords.
* @param {string} keywords - The keywords to search on
* @param {Object} query - Optional query
*/
async search(keywords, query) {
return this._getAll('search', {
search_string: keywords,
...query
});
}
}
module.exports = RarBGApi;