Skip to content

Commit 74bdae8

Browse files
committed
fix: podcast episode name resolving
1 parent ea14062 commit 74bdae8

File tree

2 files changed

+226
-102
lines changed

2 files changed

+226
-102
lines changed

src/names.ts

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { MetadataResponse, Snapshot } from "./types";
1+
import type { MetadataResponse, PodcastEpisode, Snapshot } from "./types";
22
import { formatDateTime } from "./utils";
33
import { t } from "./i18n";
44

@@ -80,27 +80,31 @@ export async function resolveItemNames(uris: string[]): Promise<string[]> {
8080
}
8181

8282
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];
102106
}
103-
}
107+
});
104108
}
105109

106110
return names;

0 commit comments

Comments
 (0)