forked from hackhackhack/butter-provider-yts
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
224 lines (193 loc) · 6.63 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
'use strict';
var Q = require('q');
var request = require('request');
var inherits = require('util').inherits;
var _ = require('lodash');
var Generic = require('butter-provider');
var sanitize = require('butter-sanitize');
var txtAPI = require('popcorn-txt-api');
function YTS(args) {
if (!(this instanceof YTS)) {
return new YTS(args);
}
Generic.call(this);
if (args.apiURL)
this.apiURL = Q.all(_.map(args.apiURL.split(','), function (url){
return txtAPI(url);
}));
this.quality = args.quality;
this.translate = args.translate;
this.language = args.language;
}
inherits(YTS, Generic);
YTS.prototype.config = {
name: 'yts',
uniqueId: 'imdb_id',
tabName: 'YTS',
type: 'movie',
metadata: 'trakttv:movie-metadata'
};
YTS.prototype.extractIds = function (items) {
return _.map(items.results, 'imdb_id');
};
var format = function (data) {
var results = _.chain(data.movies)
.filter(function (movie) {
// Filter any 3D only movies
return _.some(movie.torrents, function (torrent) {
return torrent.quality !== '3D';
});
}).map(function (movie) {
return {
type: 'movie',
imdb_id: movie.imdb_code,
title: movie.title_english,
year: movie.year,
genre: movie.genres,
rating: movie.rating,
runtime: movie.runtime,
image: movie.medium_cover_image,
cover: movie.large_cover_image,
backdrop: movie.background_image_original,
synopsis: movie.description_full,
trailer: 'https://www.youtube.com/watch?v=' + movie.yt_trailer_code || false,
certification: movie.mpa_rating,
torrents: _.reduce(movie.torrents, function (torrents, torrent) {
if (torrent.quality !== '3D') {
torrents[torrent.quality] = {
url: torrent.url,
magnet: 'magnet:?xt=urn:btih:' + torrent.hash + '&tr=udp://glotorrents.pw:6969/announce&tr=udp://tracker.opentrackr.org:1337/announce&tr=udp://torrent.gresille.org:80/announce&tr=udp://tracker.openbittorrent.com:80&tr=udp://tracker.coppersurfer.tk:6969&tr=udp://tracker.leechers-paradise.org:6969&tr=udp://p4p.arenabg.ch:1337&tr=udp://tracker.internetwarriors.net:1337',
size: torrent.size_bytes,
filesize: torrent.size,
seed: torrent.seeds,
peer: torrent.peers
};
}
return torrents;
}, {})
};
}).value();
return {
results: sanitize(results),
hasMore: data.movie_count > data.page_number * data.limit
};
};
var processCloudFlareHack = function (options, url) {
var req = options;
var match = url.match(/^cloudflare\+(.*):\/\/(.*)/)
if (match) {
req = _.extend(req, {
uri: match[1] + '://cloudflare.com/',
headers: {
'Host': match[2],
'User-Agent': 'Mozilla/5.0 (Linux) AppleWebkit/534.30 (KHTML, like Gecko) PT/3.8.0'
}
})
}
return req
}
YTS.prototype.fetch = function (filters) {
var that = this;
var params = {
sort_by: 'seeds',
limit: 50,
with_rt_ratings: true
};
if (filters.page) {
params.page = filters.page;
}
if (filters.keywords) {
params.query_term = filters.keywords;
}
if (filters.genre && filters.genre !== 'All') {
params.genre = filters.genre;
}
if (filters.order === 1) {
params.order_by = 'asc';
}
if (filters.sorter && filters.sorter !== 'popularity') {
switch (filters.sorter) {
case 'last added':
params.sort_by = 'date_added';
break;
case 'trending':
params.sort_by = 'trending_score';
break;
default:
params.sort_by = filters.sorter;
}
}
if (this.quality !== 'all') {
params.quality = this.quality;
}
if (this.translate) {
params.lang = this.language;
}
var defer = Q.defer();
function get(apis, index) {
var url = apis[index]
var options = {
uri: url + 'api/v2/list_movies_pct.json',
qs: params,
json: true,
timeout: 10000
};
var req = processCloudFlareHack(options, url)
request(req, function (err, res, data) {
if (err || res.statusCode >= 400 || (data && !data.data)) {
console.warn('YTS API endpoint \'%s\' failed.', url);
if (index + 1 >= apis.length) {
return defer.reject(err || 'Status Code is above 400');
} else {
get(apis, index + 1);
}
return;
} else if (!data || data.status === 'error') {
err = data ? data.status_message : 'No data returned';
return defer.reject(err);
} else {
return defer.resolve(format(data.data));
}
});}
this.apiURL.then(function(apis) {
get(apis, 0)
});
return defer.promise;
};
YTS.prototype.random = function () {
var that = this;
var defer = Q.defer();
function get(apis, index) {
var url = apis[index];
var options = {
uri: url + 'api/v2/get_random_movie.json?' + Math.round((new Date()).valueOf() / 1000),
json: true,
timeout: 10000
};
var req = processCloudFlareHack(options, url)
request(req, function (err, res, data) {
if (err || res.statusCode >= 400 || (data && !data.data)) {
console.warn('YTS API endpoint \'%s\' failed.', url);
if (index + 1 >= that.apiURL.length) {
return defer.reject(err || 'Status Code is above 400');
} else {
get(apis,index + 1);
}
return;
} else if (!data || data.status === 'error') {
err = data ? data.status_message : 'No data returned';
return defer.reject(err);
} else {
return defer.resolve(sanitize(data.data));
}
});
}
this.apiURL.then(function(apis) {
get(apis, 0)
});
return defer.promise;
};
YTS.prototype.detail = function (torrent_id, old_data) {
return Q(old_data);
};
module.exports = YTS;