forked from yichahucha/surge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNFR_Overall.js
276 lines (258 loc) · 10.6 KB
/
NFR_Overall.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
const $tool = new Tool()
const consoleLog = false;
const imdbApikeyCacheKey = "ImdbApikeyCacheKey";
const netflixTitleCacheKey = "NetflixTitleCacheKey";
if (!$tool.isResponse) {
let url = $request.url;
const urlDecode = decodeURIComponent(url);
const videos = urlDecode.match(/"videos","(\d+)"/);
const videoID = videos[1];
const map = getTitleMap();
const title = map[videoID];
const isEnglish = url.match(/languages=en/) ? true : false;
if (!title && !isEnglish) {
const currentSummary = urlDecode.match(/\["videos","(\d+)","current","summary"\]/);
if (currentSummary) {
url = url.replace("&path=" + encodeURIComponent(currentSummary[0]), "");
}
url = url.replace(/&languages=(.*?)&/, "&languages=en-US&");
}
url += "&path=" + encodeURIComponent(`[${videos[0]},"details"]`);
$done({ url });
} else {
var IMDbApikeys = IMDbApikeys();
var IMDbApikey = $tool.read(imdbApikeyCacheKey);
if (!IMDbApikey) updateIMDbApikey();
let obj = JSON.parse($response.body);
if (consoleLog) console.log("Netflix Original Body:\n" + $response.body);
if (typeof obj.paths[0][1] == "string") {
const videoID = obj.paths[0][1];
const video = obj.value.videos[videoID];
const map = getTitleMap();
let title = map[videoID];
if (!title) {
title = video.summary.title;
setTitleMap(videoID, title, map);
}
let year = null;
let type = video.summary.type;
if (type == "show") {
type = "series";
}
if (video.details) {
if (type == "movie") {
year = video.details.releaseYear;
}
delete video.details;
}
const requestRatings = async () => {
const IMDb = await requestIMDbRating(title, year, type);
const Douban = await requestDoubanRating(IMDb.id);
const IMDbrating = IMDb.msg.rating;
const awards = IMDb.msg.awards;
const doubanRating = Douban.rating;
const imdbUnderline = (IMDbrating.length > 0) ? "_".repeat(14) : "";
const doubanUnderline = (doubanRating.length > 0) ? "_".repeat(16) : "";
const imdbUpperline = (IMDbrating.length > 0) ? "‾".repeat(14) : "";
const doubanUpperline = (doubanRating.length > 0) ? "‾".repeat(16) : "";
const spaceDecoration = (IMDbrating.length > 0 && doubanRating.length > 0) ? " ".repeat(11) : "";
const message = `${awards.length > 0 ? awards + "\n" : ""}${imdbUnderline}${spaceDecoration}${doubanUnderline}\n${IMDbrating.length > 0 ? IMDbrating + "\t\t" : ""}${doubanRating.length > 0 ? doubanRating : ""}\n${imdbUpperline}${spaceDecoration}${doubanUpperline}`;
return message;
}
let msg = "";
requestRatings()
.then(message => msg = message)
.catch(error => msg = error + "\n")
.finally(() => {
let summary = obj.value.videos[videoID].summary;
summary["supplementalMessage"] = `${msg}${summary && summary.supplementalMessage ? "\n" + summary.supplementalMessage : ""}`;
msg_obj = {"tagline":summary.supplementalMessage, "classification":"REGULAR"}
if (summary["supplementalMessages"]) {
summary["supplementalMessages"].push(msg_obj)
}else {
summary["supplementalMessages"] = [msg_obj]
}
if (consoleLog) console.log("Netflix Modified Body:\n" + JSON.stringify(obj));
$done({ body: JSON.stringify(obj) });
});
} else {
$done({});
}
}
function getTitleMap() {
const map = $tool.read(netflixTitleCacheKey);
return map ? JSON.parse(map) : {};
}
function setTitleMap(id, title, map) {
map[id] = title;
$tool.write(JSON.stringify(map), netflixTitleCacheKey);
}
function requestDoubanRating(imdbId) {
return new Promise(function (resolve, reject) {
const url = `https://www.douban.com/search?cat=1002&q=${imdbId}`;
if (consoleLog) console.log("Netflix Douban Rating URL:\n" + url);
$tool.get(url, function (error, response, data) {
if (!error) {
if (consoleLog) console.log("Netflix Douban Rating Data:\n" + data);
if (response.status == 200) {
const rating = get_douban_rating_message(data);
resolve({ rating });
} else {
resolve({ rating: "Douban: " + errorTip().noData });
}
} else {
if (consoleLog) console.log("Netflix Douban Rating Error:\n" + error);
resolve({ rating: "Douban: " + errorTip().error });
}
});
});
}
function requestIMDbRating(title, year, type) {
return new Promise(function (resolve, reject) {
let url = "https://www.omdbapi.com/?t=" + encodeURI(title) + "&apikey=" + IMDbApikey;
if (year) url += "&y=" + year;
if (type) url += "&type=" + type;
if (consoleLog) console.log("Netflix IMDb Rating URL:\n" + url);
$tool.get(url, function (error, response, data) {
if (!error) {
if (consoleLog) console.log("Netflix IMDb Rating Data:\n" + data);
if (response.status == 200) {
const obj = JSON.parse(data);
if (obj.Response != "False") {
const id = obj.imdbID;
const msg = get_IMDb_message(obj);
resolve({ id, msg });
} else {
reject(errorTip().noData);
}
} else if (response.status == 401) {
if (IMDbApikeys.length > 1) {
updateIMDbApikey();
requestIMDbRating(title, year, type);
} else {
reject(errorTip().noData);
}
} else {
reject(errorTip().noData);
}
} else {
if (consoleLog) console.log("Netflix IMDb Rating Error:\n" + error);
reject(errorTip().error);
}
});
});
}
function updateIMDbApikey() {
if (IMDbApikey) IMDbApikeys.splice(IMDbApikeys.indexOf(IMDbApikey), 1);
const index = Math.floor(Math.random() * IMDbApikeys.length);
IMDbApikey = IMDbApikeys[index];
$tool.write(IMDbApikey, imdbApikeyCacheKey);
}
function get_IMDb_message(data) {
let rating_message = "";
let tomatoes_message = "";
let country_message = "";
let ratings = data.Ratings;
let awards_message = "";
if (data.Awards && data.Awards != "N/A") {
awards_message = "🏆" + " " + data.Awards;
}
if (ratings.length > 0) {
const imdb_source = ratings[0]["Source"];
if (imdb_source == "Internet Movie Database") {
const imdb_votes = data.imdbVotes;
const imdb_rating = ratings[0]["Value"];
rating_message = "IMDb ★" + imdb_rating;
if (data.Type == "movie") {
if (ratings.length > 1) {
const source = ratings[1]["Source"];
}
}
}
}
return { rating: rating_message, awards: awards_message }
}
function get_douban_rating_message(data) {
const s = data.replace(/\n| |&#\d{2}/g, '')
.match(/\[(\u7535\u5f71|\u7535\u89c6\u5267)\].+?subject-cast\">.+?<\/span>/g);
const average = s ? s[0].split(/">(\d\.\d)</)[1] || '' : '';
if (!average) {
return '';
}
const rating_message = `Douban ★${average}/10`;
return rating_message;
}
function errorTip() {
return { noData: "", error: "" }
}
function IMDbApikeys() {
const apikeys = [
"f75e0253", "d8bb2d6b",
"ae64ce8d", "7218d678",
"b2650e38", "8c4a29ab",
"9bd135c2", "953dbabe",
"1a66ef12", "3e7ea721",
"457fc4ff", "d2131426",
"9cc1a9b7", "e53c2c11",
"f6dfce0e", "b9db622f",
"e6bde2b9", "d324dbab",
"d7904fa3", "aeaf88b9",
"4e89234e",];
return apikeys;
}
function Tool() {
_node = (() => {
if (typeof require == "function") {
const request = require('request')
return ({ request })
} else {
return (null)
}
})()
_isSurge = typeof $httpClient != "undefined"
_isQuanX = typeof $task != "undefined"
this.isSurge = _isSurge
this.isQuanX = _isQuanX
this.isResponse = typeof $response != "undefined"
this.notify = (title, subtitle, message) => {
if (_isQuanX) $notify(title, subtitle, message)
if (_isSurge) $notification.post(title, subtitle, message)
if (_node) console.log(JSON.stringify({ title, subtitle, message }));
}
this.write = (value, key) => {
if (_isQuanX) return $prefs.setValueForKey(value, key)
if (_isSurge) return $persistentStore.write(value, key)
}
this.read = (key) => {
if (_isQuanX) return $prefs.valueForKey(key)
if (_isSurge) return $persistentStore.read(key)
}
this.get = (options, callback) => {
if (_isQuanX) {
if (typeof options == "string") options = { url: options }
options["method"] = "GET"
$task.fetch(options).then(response => { callback(null, _status(response), response.body) }, reason => callback(reason.error, null, null))
}
if (_isSurge) $httpClient.get(options, (error, response, body) => { callback(error, _status(response), body) })
if (_node) _node.request(options, (error, response, body) => { callback(error, _status(response), body) })
}
this.post = (options, callback) => {
if (_isQuanX) {
if (typeof options == "string") options = { url: options }
options["method"] = "POST"
$task.fetch(options).then(response => { callback(null, _status(response), response.body) }, reason => callback(reason.error, null, null))
}
if (_isSurge) $httpClient.post(options, (error, response, body) => { callback(error, _status(response), body) })
if (_node) _node.request.post(options, (error, response, body) => { callback(error, _status(response), body) })
}
_status = (response) => {
if (response) {
if (response.status) {
response["statusCode"] = response.status
} else if (response.statusCode) {
response["status"] = response.statusCode
}
}
return response
}
}