-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrakt.js
109 lines (103 loc) · 3.08 KB
/
trakt.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
const getToken = async (code) => {
const address = `https://api.trakt.tv/oauth/token`
const response = await fetch(address,
{
method: 'POST',
body: JSON.stringify({
"code": code,
"client_id": apiKey,
"client_secret": apiSecret,
"redirect_uri": "https://www.amazon.co.uk/",
"grant_type": "authorization_code"
}),
headers: {
'Content-Type': 'application/json'
}
});
console.log(response)
const myJson = await response.json(); //extract JSON from the http response
console.log(myJson)
return myJson
}
const getMedia = async (name, isFilm) => {
const query = "\"" + name + "\""
const type = isFilm ? "movie" : "show"
const address = `https://api.trakt.tv/search/${type}?query=${query}&field=title`
const response = await fetch(address,
{
method: 'GET',
//body: myBody, // string or object
headers: {
'Content-Type': 'application/json',
'trakt-api-version': '2',
'trakt-api-key': apiKey
}
});
const myJson = await response.json(); //extract JSON from the http response
console.log("GET MEDIA RESPONSE");
console.log(myJson);
if (isFilm) {
return myJson[0].movie
} else {
return myJson[0].show
}
}
const getEpisode = async(trakt_id, series, episode) => {
const response = await fetch(`https://api.trakt.tv/shows/${trakt_id}/seasons/${series}/episodes/${episode}`,
{
method: 'GET',
//body: myBody, // string or object
headers: {
'Content-Type': 'application/json',
'trakt-api-version': '2',
'trakt-api-key': apiKey
}
});
const myJson = await response.json(); //extract JSON from the http response
return myJson
}
const States = {
STOP: "stop",
START: "start",
PAUSE: "pause"
}
const scrobble = async(media, isMovie, state, percent, token) => {
var body = {
"progress": percent,
//"app_version": "1.0",
//"app_date": "2014-09-22"
}
if (isMovie) {
body["movie"] = media;
} else {
body["episode"] = media;
}
const response = await fetch(`https://api.trakt.tv/scrobble/${state}`,
{
method: 'POST',
body: JSON.stringify(body),
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
'trakt-api-version': '2',
'trakt-api-key': apiKey
}
});
const myJson = await response.json(); //extract JSON from the http response
if (response.status == 409) {
myJson.alreadyScrobbled = true;
}
return myJson
}
const doLookup = async(name, isFilm, series, episode) => {
const media_promise = getMedia(name, isFilm)
if (series && episode) {
return media_promise.then(async show => {
const media_id = show.ids.slug
const episode = await getEpisode(media_id, 2, 13)
return episode
})
} else {
return media_promise
}
}