/
presence.ts
259 lines (236 loc) · 6.52 KB
/
presence.ts
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
const presence = new Presence({
clientId: "879535934977245244",
}),
slugs: { [key: string]: string } = {
home: "Home",
series: "Series",
movies: "Movies",
originals: "Originals",
"just-added": "Just Added",
"last-chance": "Last Chance",
"coming-soon": "Coming Soon",
trending: "Trending Now",
action: "Action",
animation: "Animation",
comedy: "Comedy",
crime: "Crime",
documentaries: "Documentaries",
drama: "Drama",
"fantasy-sci-fi": "Fantasy & Sci-Fi",
horror: "Horror",
international: "International",
kids: "Kids & Family",
latino: "Latino",
music: "Music",
"news-talk": "News/Talk",
reality: "Reality",
romance: "Romance",
shorts: "Shorts",
sports: "Sports",
suspense: "Suspense",
"audio-description": "Audio Description",
hbo: "HBO",
"max-originals": "Max Originals",
dc: "DC",
"classics-curated-by-tcm": "Classics Curated by TCM",
"adult-swim": "Adult Swim",
"studio-ghibli": "Studio Ghibli",
"cartoon-network": "Cartoon Network",
"sesame-workshop": "Sesame Workshop",
"looney-tunes": "Looney Tunes",
crunchyroll: "Crunchyroll Collection",
},
videosInfo: Record<
string,
{
coverArt?: string;
title?: string;
subtitle?: string;
}
> = {};
let isFetching = false;
async function fetchToken(): Promise<string> {
const res = await fetch("https://oauth.api.hbo.com/auth/tokens", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
/* eslint-disable camelcase */
body: JSON.stringify({
client_id: "585b02c8-dbe1-432f-b1bb-11cf670fbeb0",
client_secret: crypto.randomUUID(),
scope: "browse video_playback",
grant_type: "client_credentials",
deviceSerialNumber: crypto.randomUUID(),
clientDeviceData: {
paymentProviderCode: "blackmarket",
},
}),
}),
res_1 = await res.json();
return res_1.access_token;
}
async function fetchClientConfig(
token: string
): Promise<{ routeKey: string; countryCode: string }> {
const res = await fetch(
"https://sessions.api.hbo.com/sessions/v1/clientConfig",
{
method: "POST",
headers: {
authorization: `Bearer ${token}`,
"content-type": "application/json",
},
body: JSON.stringify({
contract: "abc:1.0.0.0",
preferredLanguages: ["en-us"],
}),
}
),
res_1 = await res.json();
return {
routeKey: res_1.routeKeys.contentSubdomain,
countryCode: new URLSearchParams(
res_1.features["express-content"].config.expressContentParams
).get("country-code"),
};
}
const enum Assets {
Logo = "https://cdn.rcd.gg/PreMiD/websites/H/HBO%20Max/assets/logo.png",
}
async function fetchVideoInfo() {
if (isFetching) return { coverArt: Assets.Logo, title: "" };
let output: {
coverArt?: string;
title?: string;
subtitle?: string;
} = {};
const accessToken = await fetchToken(),
{ routeKey, countryCode } = await fetchClientConfig(accessToken);
isFetching = true;
try {
const mediaInfo = (
await fetch(
`https://comet${routeKey}.api.hbo.com/express-content/${
location.pathname.split("/")[2]
}?device-code=desktop&product-code=hboMax&api-version=v9.0&country-code=${countryCode}&language=en-us`,
{
headers: {
authorization: `Bearer ${accessToken}`,
},
}
).then(res => res.json())
)[0].body;
output = {
coverArt: (() => {
if (location.pathname.includes(":episode:")) {
return `https://artist.api.cdn.hbo.com/images/${
mediaInfo.references.series.match(/series:([^:]+)/)[1]
}/tileburnedin?size=1024x1024`;
} else if (location.pathname.includes(":feature:")) {
return `https://artist.api.cdn.hbo.com/images/${
location.pathname.match(/:feature:([^:]+)/)[1]
}/tileburnedin?size=1024x1024`;
} else return Assets.Logo;
})(),
title: (mediaInfo.seriesTitles || mediaInfo.titles).full,
subtitle: mediaInfo.seriesTitles
? `S${mediaInfo.seasonNumber}:E${mediaInfo.numberInSeason} ${mediaInfo.titles.full}`
: "",
};
} catch {
output = { coverArt: Assets.Logo };
presence.error(
"Unable to fetch video info. Please open an issue here https://github.com/PreMiD/Presences/issues"
);
}
isFetching = false;
return output;
}
presence.on("UpdateData", async () => {
const presenceData: PresenceData = {
type: ActivityType.Watching,
largeImageKey: Assets.Logo,
},
video = document.querySelector("video"),
path = document.location.pathname;
switch (true) {
case path === "/profileSelect":
Object.assign(presenceData, {
details: "Selecting a profile",
});
break;
case path === "/search":
Object.assign(presenceData, {
details: "Searching",
smallImageKey: Assets.Search,
smallImageText: "Browsing...",
});
break;
case !!video: {
const timestamps = presence.getTimestampsfromMedia(video),
videoId = location.pathname.match(/:[a-z]+:([^:]+)$/)[1];
videosInfo[videoId] ??= await fetchVideoInfo();
const videoInfo = videosInfo[videoId];
Object.assign(presenceData, {
details: videoInfo.title,
state:
videoInfo.subtitle ||
(location.pathname.includes(":feature:") ? "Movie" : "Extra"),
smallImageKey: video.paused ? Assets.Pause : Assets.Play,
smallImageText: video.paused ? "Paused" : "Playing",
});
if (
[":episode:", ":feature:"].some(x => location.pathname.includes(x)) &&
(await presence.getSetting<boolean>("cover"))
)
presenceData.largeImageKey = videoInfo.coverArt;
if (!video.paused) {
Object.assign(presenceData, {
startTimestamp: timestamps[0],
endTimestamp: timestamps[1],
});
}
break;
}
case /(type:[a-z]+)$/.test(path) && document.title.includes(" • "): {
const title = document.title
.split(" • ")
.shift()
.match(/.+?(?=,)|^(?!,).*$/g)
.join("");
switch (/(type:[a-z]+)$/.exec(path)[0]) {
case "type:episode":
case "type:series":
Object.assign(presenceData, {
details: "Viewing series:",
state: title,
});
break;
case "type:feature":
Object.assign(presenceData, {
details: "Viewing movie:",
state: title,
});
break;
case "type:extra":
Object.assign(presenceData, {
details: "Viewing extra video:",
state: title,
});
break;
}
break;
}
default: {
Object.assign(presenceData, { details: "Browsing" });
const pageSlug = Object.keys(slugs).find(z =>
window.location.href.includes(`:page:${z}`)
);
if (pageSlug) Object.assign(presenceData, { state: slugs[pageSlug] });
break;
}
}
if (presenceData.details) presence.setActivity(presenceData);
else presence.setActivity();
});