-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
65 lines (58 loc) · 1.96 KB
/
utils.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
function get_imdb_tt_id(url) {
const res = url.match(/(tt[0-9]{5,9})/);
return res === null ? null : res[0];
}
async function tmdb_type(imdb_tt_id = "tt2661044"/*test_value*/,tmdb_api_key) {
if(imdb_tt_id === null)
return {raw:null};
const test_url = `https://api.themoviedb.org/3/find/${imdb_tt_id}?api_key=${tmdb_api_key}&language=en-US&external_source=imdb_id`
let tmdb_response = await JSON_request(test_url);
const r = {
is_tv: false,
raw: tmdb_response
}
if (tmdb_response.tv_results.length != 0) {
r.is_tv = true;
r.tv_raw = tmdb_response.tv_results[0];
}
return r;
}
async function JSON_request(url) {
let promise = new Promise((resolve, reject) => {
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.onload = function () {
if (this.status >= 200 && this.status < 400) {
// Success!
resolve(JSON.parse(this.response));
} else {
// We reached our target server, but it returned an error
console.log(this);
reject(this.status);
}
};
request.onerror = function () {
// There was a connection error of some sort
throw "Error. :( TODO";
};
request.send();
});
return promise;
}
async function estimate_full_size(imdb_tt_id,tmdb_api_key) {
const data = await tmdb_type(imdb_tt_id,tmdb_api_key);
if (data.is_tv) {
const detail = await JSON_request(`https://api.themoviedb.org/3/tv/${data.tv_raw.id}?api_key=${tmdb_api_key}&language=en-US`);
const t = detail.episode_run_time[0];
let full = 0;
if (detail.episode_run_time.length != 1)
console.log(detail);
detail.seasons.forEach((season) => {
full += season.episode_count * t;
});
return full;
}
else {
return data.raw;
}
}