-
Notifications
You must be signed in to change notification settings - Fork 480
/
nf_rating_season.js
162 lines (158 loc) · 6.67 KB
/
nf_rating_season.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
/**
* nf_rating_season.js = type=http-response,pattern=^https?://ios\.prod\.ftl\.netflix\.com/iosui/warmer/.+type=show-ath,requires-body=1,max-size=0,script-path=https://raw.githubusercontent.com/yichahucha/surge/master/nf_rating_season.js
*/
const $tool = new Tool()
const url = $request.url
const body = $response.body
const imdbApikeyCacheKey = "ImdbApikeyCacheKey"
const netflixTitleCacheKey = "NetflixTitleCacheKey"
const map = $tool.read(netflixTitleCacheKey)
const id = url.match(/id=(\d+)/)[1]
const title = map ? JSON.parse(map)[id] : null
const obj = JSON.parse(body)
if (title && obj.shows[id].seasons) {
const key = $tool.read(imdbApikeyCacheKey)
const seasons = obj.shows[id].seasons.map(item => item.season)
const promises = []
for (let index = 0; index < seasons.length; index++) {
const url = `https://www.omdbapi.com/?t=${title}&apikey=${key}&type=series&Season=${seasons[index]}`
promises.push(requestSeasonRating(encodeURI(url)))
}
Promise.all(promises).then((values) => {
// console.log(values)
for (let index = 0; index < values.length; index++) {
const imdb = values[index]
if (imdb) {
const imdbSeasonNumber = imdb.Season
const imdbEpisodes = imdb.Episodes
const entities = obj.entities
for (let [key, value] of Object.entries(entities)) {
// console.log(`${key}: ${value}`);
const seasonNumber = value.summary.seasonNumber
const episodeNumber = value.summary.episodeNumber
let title = value.summary.title
if (parseInt(imdbSeasonNumber) == seasonNumber) {
const imdbEpisode = imdbEpisodes.find(element => parseInt(element.Episode) == episodeNumber)
title = `${title}${imdbEpisode && imdbEpisode.imdbRating ? ` ⭐️ ${imdbEpisode.imdbRating}` : " ⭐️ N/A"}`
value.summary.title = title
//console.log(title);
}
}
}
}
// console.log(JSON.stringify(obj))
$tool.done({ body: JSON.stringify(obj) })
})
} else {
$tool.done({})
}
function requestSeasonRating(url) {
return new Promise((resolve) => {
$tool.get(url, (error, response, body) => {
if (!error) {
const code = response.statusCode
if (code == 200) {
const obj = JSON.parse(body)
if (obj.Response != "False") {
//console.log(`successfully: ${url}`)
resolve(obj)
} else {
//console.log(`failed: ${obj.Error} -> ${url}`)
resolve(null)
}
} else {
//console.log(`failed: ${response.statusCode} -> ${url}`)
resolve(null)
}
} else {
//console.log(`faile: ${error}`)
resolve(null)
}
})
})
}
function Tool() {
_node = (() => {
if (typeof require == "function") {
const request = require('request')
return ({ request })
} else {
return (null)
}
})()
_isSurge = typeof $httpClient != "undefined"
_isQuanX = typeof $task != "undefined"
_isTask = typeof $request == "undefined"
_isResponse = typeof $response != "undefined"
_isRequestBody = typeof $request != "undefined" && typeof $request.body != "undefined"
this.isSurge = _isSurge
this.isQuanX = _isQuanX
this.isTask = _isTask
this.isResponse = _isResponse
this.isRequestBody = _isRequestBody
this.method = (() => {
if (!_isTask && (_isSurge || _isQuanX)) {
return $request.method
}
})()
this.scriptType = (() => {
if (_isResponse) {
return "response"
} else {
return "request"
}
})()
this.done = (obj) => {
if (_isQuanX) $done(obj)
if (_isSurge) $done(obj)
if (_node) console.log("script done.");
}
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)
if (_node) console.log(`write success: ${key}`);
}
this.read = (key) => {
if (_isQuanX) return $prefs.valueForKey(key)
if (_isSurge) return $persistentStore.read(key)
if (_node) console.log(`read success: ${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.put = (options, callback) => {
if (_isQuanX) $task.fetch(options).then(response => { callback(null, _status(response), response.body) }, reason => callback(reason.error, null, null))
if (_isSurge) $httpClient.put(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
}
}