-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
173 lines (157 loc) · 5.1 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
161
162
163
164
165
166
167
168
169
170
171
172
173
'use strict'
const debug = require('debug')
const https = require('https')
const { createGunzip } = require('zlib')
const { URL, URLSearchParams } = require('url')
const { name } = require('./package.json')
/**
* The model for the torrent object.
* @typedef {Object} Torrent
* @property {!string} hash The hash of the torrent.
* @property {!string} magnet The magnet of the torrent.
* @property {!string} title The title of the torrent.
* @property {!string} category The category of the torrent.
* @property {!string} link The link of the torrent.
*/
/**
* The default trackers to use for the hash.
* @type {Array<string>}
*/
exports.defaultTrackers = [
'udp%3A%2F%2Ftracker.coppersurfer.tk:6969/announce&',
'udp%3A%2F%2F9.rarbg.to:2710/announce&',
'udp%3A%2F%2F9.rarbg.me:2710/announce&',
'udp%3A%2F%2FIPv6.open-internet.nl:6969/announce&',
'udp%3A%2F%2Ftracker.internetwarriors.net:1337/announce&',
'udp%3A%2F%2Ftracker.opentrackr.org:1337/announce&',
'udp%3A%2F%2Fp4p.arenabg.com:1337/announce&',
'udp%3A%2F%2Feddie4.nl:6969/announce&',
'udp%3A%2F%2Fshadowshq.yi.org:6969/announce&',
'udp%3A%2F%2Ftracker.leechers-paradise.org:6969/announce&',
'udp%3A%2F%2Fexplodie.org:6969/announce&',
'udp%3A%2F%2Ftracker.tiny-vps.com:6969/announce&',
'udp%3A%2F%2Finferno.demonoid.pw:3391/announce&',
'udp%3A%2F%2Fipv4.tracker.harry.lu:80/announce&',
'udp%3A%2F%2Fpeerfect.org:6969/announce&',
'udp%3A%2F%2Ftracker.pirateparty.gr:6969/announce&',
'udp%3A%2F%2Ftracker.vanitycore.co:6969/announce&',
'udp%3A%2F%2Fopen.stealth.si:80/announce&',
'udp%3A%2F%2Ftracker.torrent.eu.org:451&',
'udp%3A%2F%2Ftracker.zer0day.to:1337/announce&',
'udp%3A%2F%2Ftracker.open-internet.nl:6969/announce'
]
/**
* An ETTV API wrapper for NodeJS.
* @type {EttvApi}
*/
module.exports = class EttvApi {
/**
* Create a new instance of the module.
* @param {!Object} [options={}] - The options for the module.
* @param {!string} [options.baseUrl=https://www.ettv.tv/] - The base url of
* ettv.tv
* @param {!Array<string>} [options.trackers=exprts.defaultTrackers] - A list
* of trackers to add to the hahs of the torrents.
*/
constructor({
baseUrl = 'https://www.ettv.tv/',
trackers = exports.defaultTrackers
} = {}) {
/**
* The base url of ettv.
* @type {string}
*/
this._baseUrl = baseUrl
/**
* A list of trackers to add to the hahs of the torrents.
* @type {Array<string>}
*/
this._trackers = trackers
/**
* Show extra output.
* @type {Function}
*/
this._debug = debug(name)
}
/**
* Make a GET request to ettv.tv.
* @param {!string} endpoint - The endpoint to make the request to.
* @returns {Promise<string, Error>} - The unzipped response from ettv.tv.
*/
_get(endpoint) {
return new Promise((resolve, reject) => {
this._debug(`Making GET request to: '${endpoint}'`)
return https.get(endpoint, res => {
let data = ''
res.pipe(createGunzip())
.on('error', err => reject(new Error(err)))
.on('data', chunk => {
data += chunk
})
.on('end', () => resolve(data))
})
})
}
/**
* Convert one chunk line to a more accessable JSON object.
* @param {!Array<string>} chunk - The chunk to convert to an object.
* @returns {Torrent} - The chunk converted to a torrent object.
*/
_convertChunk(chunk) {
const line = chunk.split('|')
const headers = ['hash', 'title', 'category', 'link']
const torrent = headers.reduce((acc, curr, i) => {
acc[curr] = line[i]
return acc
}, {})
Object.defineProperty(torrent, 'magnet', {
get() {
const qs = new URLSearchParams({
xt: `urn:btih:${torrent.hash}`,
dn: torrent.title,
tr: this._trackers
})
return `$magnet:?${qs}`
}
})
return torrent
}
/**
* Convert the database dump from ettv.tv to an array with Torrent objects.
* @param {!string} res - The response of the database dump from ettv.tv.
* @returns {Array<Torrent>} - The response converted to an array of torrent
* object.
*/
_convertToTorrents(res) {
const lines = res.split('\n').filter(line => line !== '')
return lines.map(this._convertChunk.bind(this))
}
/**
* Get the database dump file from ettv.tv and convert it to an array of
* Torrent object.
* @param {!string} file - The name of the file to get. Can either be 'daily`
* or 'full'.
* @returns {Promise<Array<Torrent>, Error>} - The torrents from the database
* dump.
*/
_getFile(file) {
const { href } = new URL(`dumps/ettv_${file}.txt.gz`, this._baseUrl)
return this._get(href).then(this._convertToTorrents.bind(this))
}
/**
* Get the daily database dump from ettv.tv.
* @returns {Promise<Array<Torrent>, Error>} - The torrents from the database
* dump.
*/
getDaily() {
return this._getFile('daily')
}
/**
* Get the full database dump from ettv.tv.
* @returns {Promise<Array<Torrent>, Error>} - The torrents from the database
* dump.
*/
getFull() {
return this._getFile('full')
}
}