This repository was archived by the owner on Jan 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
94 lines (83 loc) · 2.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
const got = require('got');
class eztvAPI {
/**
* @typedef {Object} eztvConfig
* @property {String} baseUrl - API base URL
*/
/**
* Create a new instance of eztvAPI
* @param {eztvConfig} config - API configuration
*/
constructor({ baseUrl = 'https://eztv.ag/api/' } = {}) {
/**
* eztv API base URL
* @type {String}
* @readonly
* @private
*/
this._baseUrl = baseUrl;
}
/**
* API request method
* @param {String} endpoint
* @param {Object} query
* @private
*/
async _get(endpoint, query) {
const uri = `${this._baseUrl}${endpoint}`;
const res = await got(uri, { query, json: true });
return res.body;
}
/**
* @typedef {Object} eztvTorrent
* @property {Number} id Torrent ID
* @property {String} hash Torrent hash
* @property {String} filename Torrent file name
* @property {String} episode_url eztv URL for episode (?)
* @property {String} torrent_url eztv URL for torrent
* @property {String} magnet_url Magnet URL for torrent
* @property {String} title Torrent title
* @property {String} imdb_id Show IMDB ID
* @property {String} season Season number
* @property {String} episode Episode number
* @property {String} small_screenshot
* @property {String} large_screenshot
* @property {Number} seeds Number of seeds
* @property {Number} peers Number of peers
* @property {Number} date_released_unix Release date in Unix epoch format
* @property {String} size_bytes Torrent size in bytes
*/
/**
* @typedef {Object} eztvOptions
* @property {Number} limit Limit of shows to fetch (1-100)
* @property {Number} page Fetch page number
* @property {String} imdb_id Filter shows by IMDB ID
*/
/**
* @typedef {Object} eztvResponse
* @property {Object} response
* @property {Number} response.torrents_count - Number of torrents found
* @property {Number} response.limit - Request limit
* @property {Number} response.page - Request page number
* @property {eztvTorrent[]} response.torrents - Array of matched torrents
*/
/**
* Fetch the list of show torrents
* @param {eztvOptions} options eztv API request options
* @returns {Promise<eztvResponse>} List of show torrents from eztv
*/
getTorrents({
limit = 50,
page = 1,
imdb_id = '', // eslint-disable-line
} = {}) {
if (!~~limit || limit < 1 || limit > 100) {
throw new Error(`${limit} is not a valid value for limit, expected a number in the range of 1 - 100!`);
}
if (!~~page || page < 1) {
throw new Error(`${page} is not a valid value for page, expected a number greater then 0!`);
}
return this._get('get-torrents', { limit, page, imdb_id });
}
}
module.exports = eztvAPI;