Newer
Older
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
return this;
}
hasMorePagers() {
return this.hasMore;
}
}
function format_discography(discography_response, artist) {
return discography_response.data.artistUnion.discography.all.items.map(function (album) {
const first_release = album.releases.items[0];
if (first_release === undefined) {
throw new ScriptException("unreachable");
}
const thumbnail = first_release.coverArt.sources[0]?.url;
if (thumbnail === undefined) {
throw new ScriptException("unreachable");
}
return new PlatformPlaylist({
id: new PlatformID(PLATFORM, first_release.id, plugin.config.id),
name: first_release.name,
author: artist,
datetime: new Date(first_release.date.isoString).getTime() / 1000,
url: `${ALBUM_URL_PREFIX}${first_release.id}`,
videoCount: first_release.tracks.totalCount,
thumbnail
});
});
}
function discography_args(artist_uri_id, offset, limit) {
const variables = JSON.stringify({
uri: `spotify:artist:${artist_uri_id}`,
offset,
limit
});
const extensions = JSON.stringify({
persistedQuery: {
version: 1,
sha256Hash: "9380995a9d4663cbcb5113fef3c6aabf70ae6d407ba61793fd01e2a1dd6929b0"
}
});
const url = new URL(QUERY_URL);
url.searchParams.set("operationName", "queryArtistDiscographyAll");
url.searchParams.set("variables", variables);
url.searchParams.set("extensions", extensions);
return { url: url.toString(), headers: { Authorization: `Bearer ${local_state.bearer_token}` } };
}
function format_section_item(section, section_as_author) {
switch (section.__typename) {
case "Album":
{
const album_artist = section.artists.items[0];
if (album_artist === undefined) {
throw new ScriptException("missing album artist");
}
const cover_art_url = section.coverArt.sources[0]?.url;
if (cover_art_url === undefined) {
throw new ScriptException("missing album cover art");
}
return new PlatformPlaylist({
id: new PlatformID(PLATFORM, id_from_uri(section.uri), plugin.config.id),
name: section.name,
author: new PlatformAuthorLink(new PlatformID(PLATFORM, id_from_uri(album_artist.uri), plugin.config.id), album_artist.profile.name, `${ARTIST_URL_PREFIX}${id_from_uri(album_artist.uri)}`),
// TODO load datetime another way datetime: ,
url: `${ALBUM_URL_PREFIX}${id_from_uri(section.uri)}`,
// TODO load video count some other way videoCount?: number
thumbnail: cover_art_url
});
}
case "Playlist": {
const created_iso = section.attributes.find(function (attribute) {
return attribute.key === "created";
})?.value;
const image_url = section.images.items[0]?.sources[0]?.url;
if (image_url === undefined) {
throw new ScriptException("missing playlist thumbnail");
}
let author = section_as_author;
// TODO we might want to look up the username of the playlist if it is missing instead of using the section/page/genre as the channel
if (section.ownerV2.data.username) {
if (!section.ownerV2.data.username) {
throw new ScriptException(`missing username for owner ${section.ownerV2}`);
}
author = new PlatformAuthorLink(new PlatformID(PLATFORM, section.ownerV2.data.username, plugin.config.id), section.ownerV2.data.name, `${USER_URL_PREFIX}${section.ownerV2.data.username}`, section.ownerV2.data.avatar?.sources[0]?.url);
}
const platform_playlist = {
id: new PlatformID(PLATFORM, id_from_uri(section.uri), plugin.config.id),
url: `${PLAYLIST_URL_PREFIX}${id_from_uri(section.uri)}`,
name: section.name,
// TODO load some other way videoCount:
thumbnail: image_url
};
if (created_iso !== undefined) {
return new PlatformPlaylist({
...platform_playlist,
datetime: new Date(created_iso).getTime() / 1000
});
}
return new PlatformPlaylist(platform_playlist);
}
case "Episode": {
if (section.podcastV2.data.__typename === "NotFound" || section.releaseDate === null) {
throw new ScriptException("unreachable");
}
return new PlatformVideo({
id: new PlatformID(PLATFORM, section.id, plugin.config.id),
name: section.name,
author: new PlatformAuthorLink(new PlatformID(PLATFORM, id_from_uri(section.podcastV2.data.uri), plugin.config.id), section.podcastV2.data.name, `${SHOW_URL_PREFIX}${id_from_uri(section.podcastV2.data.uri)}`, section.podcastV2.data.coverArt?.sources[0]?.url),
url: `${EPISODE_URL_PREFIX}${section.id}`,
thumbnails: new Thumbnails(section.coverArt.sources.map(function (source) {
return new Thumbnail(source.url, source.height);
})),
duration: section.duration.totalMilliseconds / 1000,
viewCount: HARDCODED_ZERO,
isLive: false,
shareUrl: `${EPISODE_URL_PREFIX}${section.id}`,
/** unix time */
datetime: new Date(section.releaseDate.isoString).getTime() / 1000
});
}
case "PseudoPlaylist": {
const image_url = section.image.sources[0]?.url;
if (image_url === undefined) {
throw new ScriptException("missing playlist thumbnail");
}
const author = section_as_author;
const platform_playlist = {
id: new PlatformID(PLATFORM, id_from_uri(section.uri), plugin.config.id),
url: `${COLLECTION_UR_PREFIX}${id_from_uri(section.uri)}`,
name: section.name,
author,
// TODO load some other way videoCount:
thumbnail: image_url
};
return new PlatformPlaylist(platform_playlist);
}
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
default:
throw assert_exhaustive(section, "unreachable");
}
}
class SectionPager extends ContentPager {
section_uri_id;
section_as_author;
limit;
offset;
constructor(section_uri_id, section_items, offset, limit, section_as_author, has_more) {
const playlists = section_items.map(function (section_item) {
return format_section_item(section_item, section_as_author);
});
super(playlists, has_more);
this.section_uri_id = section_uri_id;
this.section_as_author = section_as_author;
this.offset = offset + limit;
this.limit = limit;
}
nextPage() {
const { url, headers } = browse_section_args(this.section_uri_id, this.offset, this.limit);
const browse_section_response = JSON.parse(local_http.GET(url, headers, false).body);
const section_items = browse_section_response.data.browseSection.sectionItems.items.flatMap(function (section_item) {
const section_item_content = section_item.content.data;
if (section_item_content.__typename === "Album" || section_item_content.__typename === "Playlist") {
return [section_item_content];
}
return [];
});
const author = this.section_as_author;
if (section_items.length === 0) {
this.results = [];
}
else {
this.results = section_items.map(function (section_item) {
return format_section_item(section_item, author);
});
}
const next_offset = browse_section_response.data.browseSection.sectionItems.pagingInfo.nextOffset;
if (next_offset !== null) {
this.offset = next_offset;
}
this.hasMore = next_offset !== null;
return this;
}
hasMorePagers() {
return this.hasMore;
}
}
function browse_section_args(page_uri_id, offset, limit) {
const variables = JSON.stringify({
uri: `spotify:section:${page_uri_id}`,
pagination: {
offset,
limit
}
});
const extensions = JSON.stringify({
persistedQuery: {
version: 1,
sha256Hash: "8cb45a0fea4341b810e6f16ed2832c7ef9d3099aaf0034ee2a0ce49afbe42748"
}
});
const url = new URL(QUERY_URL);
url.searchParams.set("operationName", "browseSection");
url.searchParams.set("variables", variables);
url.searchParams.set("extensions", extensions);
return { url: url.toString(), headers: { Authorization: `Bearer ${local_state.bearer_token}` } };
}
function book_chapters_args(audiobook_uri_id, offset, limit) {
const variables = JSON.stringify({
uri: `spotify:show:${audiobook_uri_id}`,
offset,
limit
});
const extensions = JSON.stringify({
persistedQuery: {
version: 1,
sha256Hash: "9879e364e7cee8e656be5f003ac7956b45c5cc7dea1fd3c8039e6b5b2e1f40b4"
}
});
const url = new URL(QUERY_URL);
url.searchParams.set("operationName", "queryBookChapters");
url.searchParams.set("variables", variables);
url.searchParams.set("extensions", extensions);
return { url: url.toString(), headers: { Authorization: `Bearer ${local_state.bearer_token}` } };
}
function podcast_episodes_args(podcast_uri_id, offset, limit) {
const variables = JSON.stringify({
uri: `spotify:show:${podcast_uri_id}`,
offset,
limit
});
const extensions = JSON.stringify({
persistedQuery: {
version: 1,
sha256Hash: "108deda91e2701403d95dc39bdade6741c2331be85737b804a00de22cc0acabf"
}
});
const url = new URL(QUERY_URL);
url.searchParams.set("operationName", "queryPodcastEpisodes");
url.searchParams.set("variables", variables);
url.searchParams.set("extensions", extensions);
return { url: url.toString(), headers: { Authorization: `Bearer ${local_state.bearer_token}` } };
}
class ChapterPager extends VideoPager {
audiobook_uri_id;
limit;
author;
publish_date_time;
offset;
constructor(audiobook_uri_id, chapters_response, offset, limit, author, publish_date_time) {
const chapters = format_chapters(chapters_response, author, publish_date_time);
const next_offset = chapters_response.data.podcastUnionV2.chaptersV2.pagingInfo.nextOffset;
super(chapters, next_offset !== null);
this.audiobook_uri_id = audiobook_uri_id;
this.limit = limit;
this.author = author;
this.publish_date_time = publish_date_time;
this.offset = next_offset === null ? offset : next_offset;
}
nextPage() {
const { url, headers } = book_chapters_args(this.audiobook_uri_id, this.offset, this.limit);
const chapters_response = JSON.parse(local_http.GET(url, headers, false).body);
const chapters = format_chapters(chapters_response, this.author, this.publish_date_time);
const next_offset = chapters_response.data.podcastUnionV2.chaptersV2.pagingInfo.nextOffset;
this.hasMore = next_offset !== null;
this.results = chapters;
this.offset = next_offset === null ? this.offset : next_offset;
return this;
}
hasMorePagers() {
return this.hasMore;
}
}
function format_chapters(chapters_response, author, publish_date_time) {
return chapters_response.data.podcastUnionV2.chaptersV2.items.map(function (chapter_container) {
const chapter_data = chapter_container.entity.data;
const thumbnails = new Thumbnails(chapter_data.coverArt.sources.map(function (source) {
return new Thumbnail(source.url, source.height);
}));
return new PlatformVideo({
id: new PlatformID(PLATFORM, id_from_uri(chapter_data.uri), plugin.config.id),
name: chapter_data.name,
author,
datetime: publish_date_time,
url: `${EPISODE_URL_PREFIX}${id_from_uri(chapter_data.uri)}`,
thumbnails,
duration: chapter_data.duration.totalMilliseconds / 1000,
viewCount: HARDCODED_ZERO,
isLive: false,
shareUrl: `${EPISODE_URL_PREFIX}${id_from_uri(chapter_data.uri)}`
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
});
});
}
class EpisodePager extends VideoPager {
podcast_uri_id;
limit;
author;
offset;
constructor(podcast_uri_id, episodes_response, offset, limit, author) {
const chapters = format_episodes(episodes_response, author);
const next_offset = episodes_response.data.podcastUnionV2.episodesV2.pagingInfo.nextOffset;
super(chapters, next_offset !== null);
this.podcast_uri_id = podcast_uri_id;
this.limit = limit;
this.author = author;
this.offset = next_offset === null ? offset : next_offset;
}
nextPage() {
const { url, headers } = podcast_episodes_args(this.podcast_uri_id, this.offset, this.limit);
const chapters_response = JSON.parse(local_http.GET(url, headers, false).body);
const chapters = format_episodes(chapters_response, this.author);
const next_offset = chapters_response.data.podcastUnionV2.episodesV2.pagingInfo.nextOffset;
this.hasMore = next_offset !== null;
this.results = chapters;
this.offset = next_offset === null ? this.offset : next_offset;
return this;
}
hasMorePagers() {
return this.hasMore;
}
}
function format_episodes(episodes_response, author) {
return episodes_response.data.podcastUnionV2.episodesV2.items.map(function (chapter_container) {
const episode_data = chapter_container.entity.data;
const thumbnails = new Thumbnails(episode_data.coverArt.sources.map(function (source) {
return new Thumbnail(source.url, source.height);
}));
return new PlatformVideo({
id: new PlatformID(PLATFORM, id_from_uri(episode_data.uri), plugin.config.id),
name: episode_data.name,
author,
datetime: new Date(episode_data.releaseDate.isoString).getTime() / 1000,
url: `${EPISODE_URL_PREFIX}${id_from_uri(episode_data.uri)}`,
thumbnails,
duration: episode_data.duration.totalMilliseconds / 1000,
viewCount: HARDCODED_ZERO,
isLive: false,
shareUrl: `${EPISODE_URL_PREFIX}${id_from_uri(episode_data.uri)}`
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
});
});
}
class UserPlaylistPager extends PlaylistPager {
username;
limit;
offset;
total_playlists;
constructor(username, offset, limit) {
const { url, headers } = user_playlists_args(username, offset, limit);
const playlists_response = JSON.parse(local_http.GET(url, headers, false).body);
const playlists = format_user_playlists(playlists_response);
const total_playlists = playlists_response.total_public_playlists_count;
super(playlists, offset + limit < total_playlists);
this.username = username;
this.limit = limit;
this.offset = offset + limit;
this.total_playlists = total_playlists;
}
nextPage() {
const { url, headers } = user_playlists_args(this.username, this.offset, this.limit);
const playlists_response = JSON.parse(local_http.GET(url, headers, false).body);
const playlists = format_user_playlists(playlists_response);
this.hasMore = this.offset + this.limit < this.total_playlists;
this.results = playlists;
this.offset = this.offset + this.limit;
return this;
}
hasMorePagers() {
return this.hasMore;
}
}
function user_playlists_args(username, offset, limit) {
const url = new URL(`https://spclient.wg.spotify.com/user-profile-view/v3/profile/${username}/playlists`);
url.searchParams.set("offset", offset.toString());
url.searchParams.set("limit", limit.toString());
return { url: url.toString(), headers: { Authorization: `Bearer ${local_state.bearer_token}` } };
}
function format_user_playlists(playlists_response) {
return playlists_response.public_playlists.map(function (playlist) {
const image_uri = playlist.image_url;
return new PlatformPlaylist({
id: new PlatformID(PLATFORM, id_from_uri(playlist.uri), plugin.config.id),
name: playlist.name,
author: new PlatformAuthorLink(new PlatformID(PLATFORM, id_from_uri(playlist.owner_uri), plugin.config.id), playlist.owner_name, `${USER_URL_PREFIX}${id_from_uri(playlist.owner_uri)}`),
// TODO load the playlist creation or modificiation date somehow datetime?: number
url: `${PLAYLIST_URL_PREFIX}${id_from_uri(playlist.uri)}`,
// TODO load the video count somehow videoCount?: number
thumbnail: url_from_image_uri(image_uri)
});
});
}
//#endregion
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
//#region other
function getUserPlaylists() {
let playlists = [];
let more = true;
let offset = 0;
const limit = 50;
while (more) {
const { url, headers } = library_args(offset, limit);
const library_response = JSON.parse(local_http.GET(url, headers, false).body);
playlists = [
...playlists,
...library_response.data.me.libraryV3.items.flatMap(function (library_item) {
const item = library_item.item.data;
switch (item.__typename) {
case "Album":
return `${ALBUM_URL_PREFIX}${id_from_uri(item.uri)}`;
case "Playlist":
return `${PLAYLIST_URL_PREFIX}${id_from_uri(item.uri)}`;
case "PseudoPlaylist":
return `${COLLECTION_UR_PREFIX}${id_from_uri(item.uri)}`;
case "Audiobook":
return [];
case "Podcast":
return [];
case "Artist":
return [];
default:
throw assert_exhaustive(item, "unreachable");
}
})
];
if (library_response.data.me.libraryV3.totalCount <= offset + limit) {
more = false;
}
offset += limit;
}
return playlists;
}
function library_args(offset, limit) {
const variables = JSON.stringify({
filters: [],
order: null,
textFilter: "",
features: ["LIKED_SONGS", "YOUR_EPISODES", "PRERELEASES"],
limit,
offset,
flatten: false,
expandedFolders: [],
folderUri: null,
includeFoldersWhenFlattening: true,
withCuration: false
});
const extensions = JSON.stringify({
persistedQuery: {
version: 1,
sha256Hash: "cb996f38c4e0f98c53e46546e0b58f1ed34ab6c31cd00d17698af6ce2ac0f3af"
}
});
const url = new URL(QUERY_URL);
url.searchParams.set("operationName", "libraryV3");
url.searchParams.set("variables", variables);
url.searchParams.set("extensions", extensions);
return { url: url.toString(), headers: { Authorization: `Bearer ${local_state.bearer_token}` } };
}
function liked_songs_args(offset, limit) {
const variables = JSON.stringify({
limit,
offset
});
const extensions = JSON.stringify({
persistedQuery: {
version: 1,
sha256Hash: "f6cdd87d7fc8598e4e7500fbacd4f661b0c4aea382fe28540aeb4cb7ea4d76c8"
}
});
const url = new URL(QUERY_URL);
url.searchParams.set("operationName", "fetchLibraryTracks");
url.searchParams.set("variables", variables);
url.searchParams.set("extensions", extensions);
return { url: url.toString(), headers: { Authorization: `Bearer ${local_state.bearer_token}` } };
}
function liked_episodes_args(offset, limit) {
const variables = JSON.stringify({
limit,
offset
});
const extensions = JSON.stringify({
persistedQuery: {
version: 1,
sha256Hash: "f6cdd87d7fc8598e4e7500fbacd4f661b0c4aea382fe28540aeb4cb7ea4d76c8"
}
});
const url = new URL(QUERY_URL);
url.searchParams.set("operationName", "fetchLibraryEpisodes");
url.searchParams.set("variables", variables);
url.searchParams.set("extensions", extensions);
return { url: url.toString(), headers: { Authorization: `Bearer ${local_state.bearer_token}` } };
}
function following_args() {
const url = `https://spclient.wg.spotify.com/user-profile-view/v3/profile/${local_state.username}/following`;
return { url, headers: { Authorization: `Bearer ${local_state.bearer_token}` } };
}
function getUserSubscriptions() {
const { url, headers } = following_args();
const following_response = JSON.parse(local_http.GET(url, headers, false).body);
let following = following_response.profiles.map(function (profile) {
const { uri_id, uri_type } = parse_uri(profile.uri);
if (uri_type === "artist") {
return `${ARTIST_URL_PREFIX}${uri_id}`;
}
else if (uri_type === "user") {
return `${USER_URL_PREFIX}${uri_id}`;
}
throw new ScriptException("unreachable");
});
let more = true;
let offset = 0;
const limit = 50;
while (more) {
const { url, headers } = library_args(offset, limit);
const library_response = JSON.parse(local_http.GET(url, headers, false).body);
following = [
...following,
...library_response.data.me.libraryV3.items.flatMap(function (library_item) {
const item = library_item.item.data;
switch (item.__typename) {
case "Album":
return [];
case "Playlist":
return [];
case "PseudoPlaylist":
return [];
case "Audiobook":
return `${SHOW_URL_PREFIX}${id_from_uri(item.uri)}`;
case "Podcast":
return `${SHOW_URL_PREFIX}${id_from_uri(item.uri)}`;
case "Artist":
return `${ARTIST_URL_PREFIX}${id_from_uri(item.uri)}`;
default:
throw assert_exhaustive(item, "unreachable");
}
})
];
if (library_response.data.me.libraryV3.totalCount <= offset + limit) {
more = false;
}
offset += limit;
}
return following;
}
function getPlaybackTracker(url) {
const { content_uri_id, content_type } = parse_content_url(url);
return new SpotifyPlaybackTracker(content_uri_id, content_type);
}
class SpotifyPlaybackTracker extends PlaybackTracker {
device_id;
context_url;
context_uri;
skip_to_data;
duration;
interval_seconds;
constructor(uri_id, content_type) {
const interval_seconds = 2;
this.interval_seconds = interval_seconds;
// generate device id
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
const ht = "undefined" != typeof crypto && "function" == typeof crypto.getRandomValues;
const gt = (e) => ht ? function (e) {
return crypto.getRandomValues(new Uint8Array(e));
}(e) : function (e) {
const t = [];
for (; t.length < e;)
t.push(Math.floor(256 * Math.random()));
return t;
}(e);
const ft = (e) => {
const t = Math.ceil(e / 2);
return function (e) {
let t = "";
for (let n = 0; n < e.length; n++) {
const i = e[n];
if (i === undefined) {
throw new ScriptException("issue generating device id");
}
i < 16 && (t += "0"),
t += i.toString(16);
}
return t;
}(gt(t));
};
const vt = () => ft(40);
this.device_id = vt();
// load track info
switch (content_type) {
case "episode": {
const { url, headers } = episode_metadata_args(uri_id);
const response = JSON.parse(local_http.GET(url, headers, false).body);
switch (response.data.episodeUnionV2.__typename) {
case "Chapter":
this.context_uri = response.data.episodeUnionV2.audiobookV2.data.uri;
break;
case "Episode":
this.context_uri = response.data.episodeUnionV2.podcastV2.data.uri;
break;
default:
throw assert_exhaustive(response.data.episodeUnionV2, "unreachable");
}
this.context_url = `context://${this.context_uri}`;
this.skip_to_data = {
content_type: "episode",
track_uri: response.data.episodeUnionV2.uri
};
this.duration = response.data.episodeUnionV2.duration.totalMilliseconds;
break;
}
case "track":
const { url, headers } = track_metadata_args(uri_id);
const response = JSON.parse(local_http.GET(url, headers, false).body);
const track_album_index = response.data.trackUnion.trackNumber - 1;
const { url: tracks_url, headers: tracks_headers } = album_tracks_args(id_from_uri(response.data.trackUnion.albumOfTrack.uri), track_album_index, 1);
const tracks_response = JSON.parse(local_http.GET(tracks_url, tracks_headers, false).body);
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
this.context_uri = response.data.trackUnion.albumOfTrack.uri;
this.context_url = `context://${this.context_uri}`;
this.duration = response.data.trackUnion.duration.totalMilliseconds;
const uid = tracks_response.data.albumUnion.tracks.items[0]?.uid;
if (uid === undefined) {
throw new ScriptException("can't find song uid");
}
this.skip_to_data = {
content_type: "track",
uid,
track_uri: response.data.trackUnion.uri,
track_album_index
};
break;
default:
throw assert_exhaustive(content_type, "unreachable");
}
}
onInit(_seconds) {
}
onProgress(_seconds, is_playing) {
if (is_playing) {
// this ends up lagging behind.
this.total_seconds_played += this.interval_seconds;
}
if (is_playing && !this.recording_play && this.total_seconds_played > 30) {
this.recording_play = true;
log("creating WebSocket connection");
// setup WebSocket connection
const url = `wss://gue1-dealer.spotify.com/?access_token=${local_state.bearer_token}`;
const socket = http.socket(url, {}, false);
socket.connect({
open: () => {
},
closed: (code, reason) => {
console.log(code.toString());
console.log(reason);
},
closing: (code, reason) => {
console.log(code.toString());
console.log(reason);
},
message: (msg) => {
// ignore queued messages
if (this.play_recorded) {
log("ignoring queued message");
return;
}
const message = JSON.parse(msg);
// this is the initial connection message
if ("method" in message) {
const connection_id = message.headers["Spotify-Connection-Id"];
const track_playback_url = "https://gue1-spclient.spotify.com/track-playback/v1/devices";
local_http.POST(track_playback_url, JSON.stringify({
device: {
brand: "spotify",
capabilities: {
change_volume: true,
enable_play_token: true,
supports_file_media_type: true,
play_token_lost_behavior: "pause",
disable_connect: false,
audio_podcasts: true,
video_playback: true,
manifest_formats: ["file_ids_mp3", "file_urls_mp3", "manifest_urls_audio_ad", "manifest_ids_video", "file_urls_external", "file_ids_mp4", "file_ids_mp4_dual", "manifest_urls_audio_ad"]
},
device_id: this.device_id,
device_type: "computer",
metadata: {},
model: "web_player",
name: SPOTIFY_CONNECT_NAME,
platform_identifier: PLATFORM_IDENTIFIER,
is_group: false
},
outro_endcontent_snooping: false,
connection_id: connection_id,
volume: 65535
}), { Authorization: `Bearer ${local_state.bearer_token}` }, false);
const connect_state_url = `https://gue1-spclient.spotify.com/connect-state/v1/devices/hobs_${this.device_id.slice(0, 35)}`;
local_http.requestWithBody("PUT", connect_state_url, JSON.stringify({
member_type: "CONNECT_STATE",
device: {
device_info: {
capabilities: {
can_be_player: false,
hidden: true,
needs_full_player_state: true
}
}
}
}), {
Authorization: `Bearer ${local_state.bearer_token}`,
"X-Spotify-Connection-Id": connection_id
}, false);
const transfer_url = `https://gue1-spclient.spotify.com/connect-state/v1/player/command/from/${this.device_id}/to/${this.device_id}`;
local_http.POST(transfer_url, JSON.stringify({
uri: this.context_uri,
url: this.context_url,
play_origin: {
feature_identifier: this.feature_identifier,
feature_version: local_state.feature_version,
options: {
license: "on-demand",
skip_to: this.skip_to_data.content_type === "track" ? {
track_index: this.skip_to_data.track_album_index,
track_uid: this.skip_to_data.uid,
track_uri: this.skip_to_data.track_uri
} : {
track_uri: this.skip_to_data.track_uri
},
"54d854fb-fcb4-4e1f-a600-4fd9cbfaac2e"
],
"d3697919-e8be-425d-98bc-1ea70e28963a"
],
command_id: "46b1903536f6eda76783840368982c5e"
}
}), { Authorization: `Bearer ${local_state.bearer_token}` }, false);
return;
}
if (message.uri === "hm://track-playback/v1/command") {
if (message.payloads[0]?.state_machine.states.length === 0) {
log("ignored WS message that was informing of the active device");
const state_machine = message.payloads[0]?.state_machine;
const playback_id = (() => {
const data = this.skip_to_data;
switch (data.content_type) {
case "episode": {
return state_machine?.states.find((state) => {
return state_machine.tracks[state.track]?.metadata.uri === data.track_uri;
})?.state_id;
}
case "track": {
return message.payloads[0]?.state_machine.states.find((state) => {
return state.track_uid === data.uid;
})?.state_id;
}
default:
throw assert_exhaustive(data);
}
})();
if (playback_id === undefined) {
log("error missing playback_id");
log(msg);
return;
}
let state_machine_id = state_machine?.state_machine_id;
if (state_machine_id === undefined) {
log("error missing state_machine_id");
log(msg);
return;
}
let seq_num = 3;
const initial_state_machine_id = state_machine_id;
const state_update_url = `https://gue1-spclient.spotify.com/track-playback/v1/devices/${this.device_id}/state`;
const logged_in = bridge.isLoggedIn();
const bitrate = logged_in ? 256000 : 128000;
const format = logged_in ? 11 : 10;
const audio_quality = logged_in ? "VERY_HIGH" : "HIGH";
// simulate song play
const before_track_load = JSON.parse(local_http.requestWithBody("PUT", state_update_url, JSON.stringify({
seq_num: seq_num,
state_ref: { state_machine_id: state_machine_id, state_id: playback_id, paused: false },
sub_state: { playback_speed: 1, position: 0, duration: this.duration, media_type: "AUDIO", bitrate, audio_quality, format },
debug_source: "before_track_load"
}), { Authorization: `Bearer ${local_state.bearer_token}` }, false).body);
state_machine_id = before_track_load.state_machine.state_machine_id;
seq_num += 1;
local_http.requestWithBody("PUT", state_update_url, JSON.stringify({
seq_num: seq_num,
state_ref: { state_machine_id: initial_state_machine_id, state_id: playback_id, paused: false },
sub_state: { playback_speed: 0, position: 0, duration: this.duration, media_type: "AUDIO", bitrate, audio_quality, format },
debug_source: "speed_changed"
}), { Authorization: `Bearer ${local_state.bearer_token}` }, false);
seq_num += 1;
const speed_change = JSON.parse(local_http.requestWithBody("PUT", state_update_url, JSON.stringify({
seq_num: seq_num,
state_ref: { state_machine_id: state_machine_id, state_id: playback_id, paused: false },
sub_state: { playback_speed: 1, position: 0, duration: this.duration, media_type: "AUDIO", bitrate, audio_quality, format },
previous_position: 0,
debug_source: "speed_changed"
}), { Authorization: `Bearer ${local_state.bearer_token}` }, false).body);
state_machine_id = speed_change.state_machine.state_machine_id;
seq_num += 1;
const started_playing = JSON.parse(local_http.requestWithBody("PUT", state_update_url, JSON.stringify({
seq_num: seq_num,
state_ref: { state_machine_id: state_machine_id, state_id: playback_id, paused: false },
sub_state: { playback_speed: 1, position: 1360, duration: this.duration, media_type: "AUDIO", bitrate, audio_quality, format },
previous_position: 1360,
debug_source: "started_playing"
}), { Authorization: `Bearer ${local_state.bearer_token}` }, false).body);
state_machine_id = started_playing.state_machine.state_machine_id;
seq_num += 1;
const played_threshold_reached = JSON.parse(local_http.requestWithBody("PUT", state_update_url, JSON.stringify({
seq_num: seq_num,
state_ref: { state_machine_id: state_machine_id, state_id: playback_id, paused: false },
sub_state: { playback_speed: 1, position: 30786, duration: this.duration, media_type: "AUDIO", bitrate, audio_quality, format },
previous_position: 30786,
debug_source: "played_threshold_reached"
}), { Authorization: `Bearer ${local_state.bearer_token}` }, false).body);
state_machine_id = played_threshold_reached.state_machine.state_machine_id;
seq_num += 1;
// delete the device
const url = `https://gue1-spclient.spotify.com/connect-state/v1/devices/hobs_${this.device_id.slice(0, 35)}`;
local_http.request("DELETE", url, { Authorization: `Bearer ${local_state.bearer_token}` }, false);
const deregister = `https://gue1-spclient.spotify.com/track-playback/v1/devices/${this.device_id}`;
local_http.requestWithBody("DELETE", deregister, JSON.stringify({
seq_num: seq_num,
state_ref: { state_machine_id: state_machine_id, state_id: playback_id, paused: false },
sub_state: { playback_speed: 1, position: 40786, duration: this.duration, media_type: "AUDIO", bitrate, audio_quality, format },
previous_position: 40786,
debug_source: "deregister"
}), { Authorization: `Bearer ${local_state.bearer_token}` }, false);
socket.close();
this.play_recorded = true;
log("closing WebSocket connection");
return;
}
log("ignored WS message");
log(msg);
return;
},
failure: (exception) => {
log("failure");
console.log(exception);
function url_from_image_uri(image_uri) {
const match_result = image_uri.match(/^spotify:(image|mosaic):([0-9a-zA-Z:]*)$/);
if (match_result === null) {
if (/^https:\/\//.test(image_uri)) {
return image_uri;
}
throw new ScriptException("regex error");
}
const image_type = match_result[1];
if (image_type === undefined) {
throw new ScriptException("regex error");
}
const uri_id = match_result[2];
if (uri_id === undefined) {
throw new ScriptException("regex error");
}
switch (image_type) {
case "image":
return `https://i.scdn.co/image/${uri_id}`;
case "mosaic":
return `https://mosaic.scdn.co/300/${uri_id.split(":").join("")}`;
default:
throw assert_exhaustive(image_type);
}
}
return parse_uri(uri).uri_id;
}
function parse_uri(uri) {
const match_result = uri.match(/^spotify:(show|album|track|artist|playlist|section|episode|user|genre|collection):([0-9a-zA-Z]*|tracks|your-episodes)$/);
throw new ScriptException("regex error");
}
const maybe_type = match_result[1];
if (maybe_type === undefined) {
throw new ScriptException("regex error");
}
const uri_type = maybe_type;
const uri_id = match_result[2];
if (uri_id === undefined) {
throw new ScriptException("regex error");
}
/**
* Converts seconds to the timestamp format used in WebVTT
* @param seconds
* @returns
*/
function milliseconds_to_WebVTT_timestamp(milliseconds) {
return new Date(milliseconds).toISOString().substring(11, 23);
}
function assert_never(value) {
log(value);
}
function log_passthrough(value) {
log(value);
return value;
}
function assert_exhaustive(value, exception_message) {
log(["Spotify log:", value]);
if (exception_message !== undefined) {
return new ScriptException(exception_message);
}
return;
}
//#endregion
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
// https://open.spotifycdn.com/cdn/build/web-player/vendor~web-player.391a2438.js
const Z = "0123456789abcdef";
const Q = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
const ee = [];
ee.length = 256;
for (let ke = 0; ke < 256; ke++)
// @ts-expect-error
ee[ke] = Z[ke >> 4] + Z[15 & ke];
const te = [];
te.length = 128;
for (let ke = 0; ke < Q.length; ++ke)
te[Q.charCodeAt(ke)] = ke;
function get_gid(song_uri_id) {
return 22 === song_uri_id.length ? function (e) {
if (22 !== e.length)
return null;
const t = 2.3283064365386963e-10, n = 4294967296, i = 238328;
let o, r, a, s, c;
// @ts-expect-error
return o = 56800235584 * te[e.charCodeAt(0)] + 916132832 * te[e.charCodeAt(1)] + 14776336 * te[e.charCodeAt(2)] + 238328 * te[e.charCodeAt(3)] + 3844 * te[e.charCodeAt(4)] + 62 * te[e.charCodeAt(5)] + te[e.charCodeAt(6)],
r = o * t | 0,
o -= r * n,
// @ts-expect-error
c = 3844 * te[e.charCodeAt(7)] + 62 * te[e.charCodeAt(8)] + te[e.charCodeAt(9)],
o = o * i + c,
o -= (c = o * t | 0) * n,
r = r * i + c,
// @ts-expect-error
c = 3844 * te[e.charCodeAt(10)] + 62 * te[e.charCodeAt(11)] + te[e.charCodeAt(12)],
o = o * i + c,
o -= (c = o * t | 0) * n,
r = r * i + c,
r -= (c = r * t | 0) * n,
a = c,
// @ts-expect-error
c = 3844 * te[e.charCodeAt(13)] + 62 * te[e.charCodeAt(14)] + te[e.charCodeAt(15)],
o = o * i + c,
o -= (c = o * t | 0) * n,
r = r * i + c,
r -= (c = r * t | 0) * n,
a = a * i + c,
// @ts-expect-error
c = 3844 * te[e.charCodeAt(16)] + 62 * te[e.charCodeAt(17)] + te[e.charCodeAt(18)],
o = o * i + c,
o -= (c = o * t | 0) * n,
r = r * i + c,
r -= (c = r * t | 0) * n,
a = a * i + c,
a -= (c = a * t | 0) * n,
s = c,
// @ts-expect-error
c = 3844 * te[e.charCodeAt(19)] + 62 * te[e.charCodeAt(20)] + te[e.charCodeAt(21)],
o = o * i + c,
o -= (c = o * t | 0) * n,
r = r * i + c,
r -= (c = r * t | 0) * n,
a = a * i + c,
a -= (c = a * t | 0) * n,
s = s * i + c,
s -= (c = s * t | 0) * n,
// @ts-expect-error
c ? null : ee[s >>> 24] + ee[s >>> 16 & 255] + ee[s >>> 8 & 255] + ee[255 & s] + ee[a >>> 24] + ee[a >>> 16 & 255] + ee[a >>> 8 & 255] + ee[255 & a] + ee[r >>> 24] + ee[r >>> 16 & 255] + ee[r >>> 8 & 255] + ee[255 & r] + ee[o >>> 24] + ee[o >>> 16 & 255] + ee[o >>> 8 & 255] + ee[255 & o];
}(song_uri_id) : song_uri_id;
}