-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
160 lines (139 loc) · 3.71 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
160
const bytes = require('bytes')
const debug = require('debug')
const got = require('got')
const { stringify } = require('querystring')
const { name } = require('./package')
/**
* A SolidTorrents API wrapper.
* @type {SolidTorrentsApi}
*/
module.exports = class SolidTorrentsApi {
/**
* Create a new instance of the module.
* @param {!Object} config={} - The configuration object for the module.
* @param {!string} baseUrl=https://katcr.co/ - The base url of katcr.
* @type {String}
*/
constructor({ baseUrl = 'https://solidtorrents.net/api/v1/' } = {}) {
/**
* The base url of katcr.
* @type {string}
*/
this._baseUrl = baseUrl
/**
* Show extra output.
* @type {Function}
*/
this._debug = debug(name)
/**
* The available categories to search for.
* @type {Object}
*/
this._category = {
video: 'Video',
}
/**
* The available ways to sort the results by.
* @type {Object}
*/
this._sort = {
seeders: 'seeders',
}
}
/**
* Make a get request to kat.co.
* @param {!string} endpoint - The endpoint to make the request to.
* @param {?Object} query - The query parameters of the HTTP request.
* @returns {Promise<Function, Error>} - The response body wrapped in
* cheerio.
*/
_get(endpoint, query) {
const uri = `${this._baseUrl}${endpoint}`
const opts = {
headers: {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36',
},
query,
}
this._debug(`Making request to: '${uri}?${stringify(query)}'`)
return got.get(uri, opts)
.then(({ body }) => JSON.parse(body))
}
/**
* Format the result page in the response object.
*/
_formatResponse(response, page, { verified = true }, date) {
return {
responseTime: date,
totalPages: parseInt(response.hits.value / 20, 10), // 20 is their page limit
page,
results: response.results.map((torrent) => {
if (!torrent.swarm.verified && verified) {
return false
}
return {
_id: torrent._id,
title: torrent.title,
category: torrent.category,
magnet: torrent.magnet,
fileSize: torrent.size,
size: torrent.size,
...torrent.swarm,
}
}).filter(Boolean),
}
}
/**
* Make an advanced search.
*/
_getData({
category,
page = 1,
query,
verified = null,
language,
}, date) {
let err
if (category && !this._category[category]) {
err = new Error(`'${category}' is not a valid value for category`)
}
if (err) {
return Promise.reject(err)
}
const args = {
fuv: verified === null || verified ? 'yes' : 'no',
q: query,
sort: 'seeders',
}
if (category) {
args.category = this._category[category]
}
if (page > 1) {
args.skip = (page - 1) * 20
}
return this._get('search', args)
.then(res => this._formatResponse(
res,
page,
{ verified },
Date.now() - date,
),
)
}
/**
* Search for content on katcr.co.
* @param {Object|string} query - Object for advanced search, string for
* simple search.
* @returns {Promise<Response, Error>} - The response object of the query.
*/
search(query) {
this.lastRequestTime = Date.now()
if (typeof query === 'string') {
return this._getData({ query }, this.lastRequestTime)
} else if (typeof query === 'object') {
return this._getData(query, this.lastRequestTime)
}
const err = new Error('search needs an object or string as a parameter!')
return Promise.reject(err)
}
}