-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
204 lines (179 loc) · 7.19 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
'use strict';
var querystring = require('querystring');
var request = require('request');
var Q = require('q');
var _ = require('lodash');
var inherits = require('util').inherits;
var Generic = require('butter-provider');
var sanitize = require('butter-sanitize');
var txtAPI = require('popcorn-txt-api');
var tvApiServer;
var URL = false;
var TVApi = function (args) {
var that = this;
try {
var Client = require('node-tvdb');
var tvdb = new Client('7B95D15E1BE1D75A');
tvdb.getLanguages()
.then(function (langlist) {
that.TVDBLangs = langlist
});
} catch (e) {
that.TVDBLangs = false
console.warn('Something went wrong with TVDB, overviews can\'t be translated.');
}
TVApi.super_.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(TVApi, Generic);
TVApi.prototype.config = {
name: 'tvapi',
uniqueId: 'tvdb_id',
tabName: 'TVApi',
type: 'tvshow',
metadata: 'trakttv:show-metadata'
};
TVApi.prototype.extractIds = function (items) {
return _.pluck(items.results, 'imdb_id');
};
TVApi.prototype.fetch = function (filters) {
var that = this;
var deferred = Q.defer();
var params = {};
params.sort = 'seeds';
params.limit = '50';
if (filters.keywords) {
params.keywords = filters.keywords.replace(/\s/g, '% ');
}
if (filters.genre) {
params.genre = filters.genre;
}
if (filters.order) {
params.order = filters.order;
}
if (filters.sorter && filters.sorter !== 'popularity') {
params.sort = filters.sorter;
}
function get(apis, index) {
var options = {
url: apis[index] + 'shows/' + filters.page + '?' + querystring.stringify(params).replace(/%25%20/g, '%20'),
json: true
};
tvApiServer = apis[index];
//document.getElementById('TVApi').setAttribute('data-original-title', tvApiServer);
var req = _.extend({}, apis[index], options);
console.info('Request to TVApi', req.url);
request(req, function (err, res, data) {
if (err || res.statusCode >= 400) {
console.warn('TVAPI endpoint \'%s\' failed.', apis[index]);
if (index + 1 >= apis.length) {
return deferred.reject(err || 'Status Code is above 400');
} else {
get(apis, index + 1);
}
return;
} else if (!data || (data.error && data.error !== 'No movies found')) {
err = data ? data.status_message : 'No data returned';
console.error('API error:', err);
return deferred.reject(err);
} else {
//document.getElementById('TVApi').setAttribute('data-original-title', tvApiServer)
data.forEach(function (entry) {
entry.type = 'show';
});
deferred.resolve({
results: sanitize(data),
hasMore: true
});
}
});
}
this.apiURL.then(function(apis) {
get(apis, 0)
});
return deferred.promise;
};
TVApi.prototype.detail = function (torrent_id, old_data, debug) {
// Single element query
var that = this;
debug === undefined ? debug = true : '';
return Q.Promise(function (resolve, reject) {
function get(apis, index) {
var options = {
url: apis[index] + 'show/' + torrent_id,
json: true
};
// document.getElementById('TVApi').setAttribute('data-original-title', tvApiServer);
var req = _.extend({}, apis[index], options);
console.info('Request to TVApi', req.url);
request(req, function (error, response, data) {
if (error || response.statusCode >= 400) {
console.warn('TVAPI endpoint \'%s\' failed.', apis[index]);
if (index + 1 >= apis.length) {
return reject(error || 'Status Code is above 400');
} else {
get(apis, index + 1);
}
return;
} else if (!data || (data.error && data.error !== 'No data returned') || data.episodes.length === 0) {
var err = (data && data.episodes.length !== 0) ? data.error : 'No data returned';
debug ? console.error('API error:', err) : '';
reject(err);
} else {
data = sanitize(data);
// we cache our new element or translate synopsis
if (that.translate && that.language !== 'en') {
var langAvailable;
for (var x = 0; x < that.TVDBLangs.length; x++) {
if (that.TVDBLangs[x].abbreviation.indexOf(that.language) > -1) {
langAvailable = true;
break;
}
}
if (!langAvailable) {
resolve(data);
} else {
var reqTimeout = setTimeout(function () {
resolve(data);
}, 2000);
var Client = require('node-tvdb');
var tvdb = new Client('7B95D15E1BE1D75A', that.language);
console.info('Request to TVDB API: \'%s\' - %s', old_data.title, that.language);
tvdb.getSeriesAllById(old_data.tvdb_id)
.then(function (localization) {
clearTimeout(reqTimeout);
_.extend(data, {
synopsis: localization.Overview
});
for (var i = 0; i < localization.Episodes.length; i++) {
for (var j = 0; j < data.episodes.length; j++) {
if (localization.Episodes[i].id.toString() === data.episodes[j].tvdb_id.toString()) {
data.episodes[j].overview = localization.Episodes[i].Overview;
break;
}
}
}
resolve(sanitize(data));
})
.catch(function (error) {
resolve(data);
});
}
} else {
resolve(data);
}
}
});
}
this.apiURL.then(function(apis) {
get(apis, 0)
});
});
};
module.exports = TVApi;