forked from yichahucha/surge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNFR-minimal.js
228 lines (218 loc) · 8.15 KB
/
NFR-minimal.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
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-GB&");
}
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 IMDbrating = IMDb.msg.rating;
const message = `\n${IMDbrating}`;
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 : ""}`;
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 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 country_message = "";
let ratings = data.Ratings;
if (ratings.length > 0) {
const imdb_source = ratings[0]["Source"];
if (imdb_source == "Internet Movie Database") {
const imdb_rating = ratings[0]["Value"];
rating_message = "IMDB:" + " " + imdb_rating;
if (data.Type == "movie") {
if (ratings.length > 1) {
const source = ratings[1]["Source"];
}
}
}
}
country_message = get_country_message(data.Country);
return { rating: rating_message, country: country_message }
}
function get_country_message(data) {
const country = data;
const countrys = country.split(", ");
let emoji_country = "";
countrys.forEach(item => {
emoji_country += item + ", ";
});
return emoji_country.slice(0, -2);
}
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
}
}