forked from butterproviders/butter-provider-vodo
-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
213 lines (181 loc) · 5.7 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
'use strict';
var GenericProvider = require('butter-provider');
var querystring = require('querystring');
var Q = require('q');
var request = require('request');
var inherits = require('util').inherits;
var _ = require('lodash');
var Datastore = require('nedb');
var apiUrl = 'http://butter.vodo.net/popcorn',
db = new Datastore();
function Vodo() {
if (!(this instanceof Vodo)) {
return new Vodo();
}
GenericProvider.call(this);
}
inherits(Vodo, GenericProvider);
Vodo.prototype.config = {
name: 'vodo',
uniqueId: 'imdb_id',
tabName: 'Vodo',
type: 'movie',
/* should be removed */
//subtitle: 'ysubs',
metadata: 'trakttv:movie-metadata'
};
function formatForButter(items) {
var results = {};
var movieFetch = {};
movieFetch.results = [];
movieFetch.hasMore = (Number(items.length) > 1 ? true : false);
_.each(items, function (movie) {
if (movie.Quality === '3D') {
return;
}
var imdb = movie.ImdbCode;
// Calc torrent health
var seeds = 0; //XXX movie.TorrentSeeds;
var peers = 0; //XXX movie.TorrentPeers;
var torrents = {};
torrents[movie.Quality] = {
url: movie.TorrentUrl,
size: movie.SizeByte,
filesize: movie.Size,
seed: seeds,
peer: peers
};
var ptItem = results[imdb];
if (!ptItem) {
ptItem = {
imdb_id: imdb,
title: movie.MovieTitleClean.replace(/\([^)]*\)|1080p|DIRECTORS CUT|EXTENDED|UNRATED|3D|[()]/g, ''),
year: movie.MovieYear,
genre: [movie.Genre],
rating: movie.MovieRating,
image: movie.CoverImage,
cover: movie.CoverImage,
backdrop: movie.CoverImage,
torrents: torrents,
subtitle: {}, // TODO
trailer: false,
synopsis: movie.Synopsis || 'No synopsis available.',
type: 'movie'
};
movieFetch.results.push(ptItem);
} else {
_.extend(ptItem.torrents, torrents);
}
results[imdb] = ptItem;
});
return movieFetch.results;
}
Vodo.prototype.extractIds = function (items) {
return _.pluck(items.results, 'imdb_id');
};
Vodo.prototype.updateAPI = function () {
var self = this;
var defer = Q.defer();
console.info('Request to Vodo', apiUrl);
request({
uri: apiUrl,
strictSSL: false,
json: true,
timeout: 10000
},
function (err, res, data) {
/*
data = _.map (helpers.formatForButter(data), function (item) {
item.rating = item.rating.percentage * Math.log(item.rating.votes);
return item;
});
*/
db.insert(formatForButter(data.downloads), function (err, newDocs) {
if (err) {
console.error('Vodo.updateAPI(): Error inserting', err);
}
db.find({}).limit(2).exec(function (err, docs) {
//console.debug('FIND ---->', err, docs);
});
defer.resolve(newDocs);
});
});
return defer.promise;
};
Vodo.prototype.fetch = function (filters) {
var self = this;
if (!self.fetchPromise) {
self.fetchPromise = this.updateAPI();
}
var defer = Q.defer();
var params = {
sort: 'rating',
limit: 50
};
var findOpts = {};
if (filters.keywords) {
findOpts = {
title: new RegExp(filters.keywords.replace(/\s/g, '\\s+'))
};
}
if (filters.genre) {
params.genre = filters.genre;
}
if (filters.order) {
params.order = filters.order;
}
if (filters.sorter && filters.sorter !== 'popularity') {
params.sort = filters.sorter;
}
var sortOpts = {};
sortOpts[params.sort] = params.order;
self.fetchPromise.then(function () {
db.find(findOpts)
.sort(sortOpts)
.skip((filters.page - 1) * params.limit)
.limit(Number(params.limit))
.exec(function (err, docs) {
docs.forEach(function (entry) {
entry.type = 'movie';
});
return defer.resolve({
results: docs,
hasMore: docs.length ? true : false
});
});
});
return defer.promise;
};
Vodo.prototype.random = function () {
var defer = Q.defer();
function get(index) {
var options = {
uri: apiUrl + Math.round((new Date()).valueOf() / 1000),
json: true,
timeout: 10000
};
var req = _.extend(true, {}, apiUrl[index], options);
request(req, function (err, res, data) {
if (err || res.statusCode >= 400 || (data && !data.data)) {
console.error('Vodo API endpoint \'%s\' failed.', apiUrl);
if (index + 1 >= apiUrl.length) {
return defer.reject(err || 'Status Code is above 400');
} else {
get(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(Common.sanitize(data.data));
}
});
}
get(0);
return defer.promise;
};
Vodo.prototype.detail = function (torrent_id, old_data) {
return Q(old_data);
};
module.exports = Vodo