|
1 | | -import type { MetadataResponse, Snapshot } from "./types"; |
| 1 | +import type { MetadataResponse, PodcastEpisode, Snapshot } from "./types"; |
2 | 2 | import { formatDateTime } from "./utils"; |
3 | 3 | import { t } from "./i18n"; |
4 | 4 |
|
@@ -80,27 +80,31 @@ export async function resolveItemNames(uris: string[]): Promise<string[]> { |
80 | 80 | } |
81 | 81 |
|
82 | 82 | if (episodeIds.length) { |
83 | | - for (let i = 0; i < episodeIds.length; i += 50) { |
84 | | - const chunk = episodeIds.slice(i, i + 50); |
85 | | - const ids = chunk.map((c) => c.id).join(","); |
86 | | - try { |
87 | | - // TODO: this needs a new endpoint because it will get 429 too many requests errors |
88 | | - // TODO: I tried the same method we use for tracks now but it didn't work because it returned a 404 |
89 | | - const res = await Spicetify.CosmosAsync.get(`https://api.spotify.com/v1/episodes?ids=${ids}`); |
90 | | - const arr = Array.isArray(res?.episodes) ? res.episodes : []; |
91 | | - const idToDisplay = new Map<string, string>(); |
92 | | - for (const t of arr) { |
93 | | - if (t?.id && t?.name) { |
94 | | - const showName = t?.show?.name; |
95 | | - const display = showName ? `${showName} - ${t.name}` : t.name; |
96 | | - idToDisplay.set(t.id, display); |
97 | | - } |
98 | | - } |
99 | | - for (const c of chunk) names[c.idx] = idToDisplay.get(c.id) || uris[c.idx]; |
100 | | - } catch { |
101 | | - for (const c of chunk) names[c.idx] = uris[c.idx]; |
| 83 | + const promises = episodeIds.map(async (c) => { |
| 84 | + const episodeUri = uris[c.idx]; |
| 85 | + const data: PodcastEpisode = await Spicetify.Platform.ShowAPI.getEpisodeOrChapter(episodeUri); |
| 86 | + const episodeName = data?.name; |
| 87 | + const showName = data?.podcast?.name; |
| 88 | + const display = episodeName |
| 89 | + ? showName |
| 90 | + ? `${showName} - ${episodeName}` |
| 91 | + : episodeName |
| 92 | + : null; |
| 93 | + return { idx: c.idx, id: c.id, display }; |
| 94 | + }); |
| 95 | + |
| 96 | + const results = await Promise.allSettled(promises); |
| 97 | + |
| 98 | + results.forEach((result, index) => { |
| 99 | + const { idx } = episodeIds[index]; |
| 100 | + |
| 101 | + if (result.status === "fulfilled") { |
| 102 | + const value = result.value; |
| 103 | + names[value.idx] = value.display || uris[value.idx]; |
| 104 | + } else { |
| 105 | + names[idx] = uris[idx]; |
102 | 106 | } |
103 | | - } |
| 107 | + }); |
104 | 108 | } |
105 | 109 |
|
106 | 110 | return names; |
|
0 commit comments