fix: improve now playing error handling
This commit is contained in:
parent
157b1c7b67
commit
8d41e1c5b3
3 changed files with 50 additions and 23 deletions
|
@ -69,10 +69,13 @@ export default async () => {
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
.then((data) => {
|
.then((data) => {
|
||||||
if (data.body) return data.json();
|
if (data.ok) return data.json();
|
||||||
return {};
|
throw new Error('Something went wrong with the Trakt endpoint.');
|
||||||
})
|
})
|
||||||
.catch();
|
.catch(err => {
|
||||||
|
console.log(err);
|
||||||
|
return {}
|
||||||
|
});
|
||||||
|
|
||||||
if (Object.keys(traktRes).length) {
|
if (Object.keys(traktRes).length) {
|
||||||
if (traktRes["type"] === "episode") {
|
if (traktRes["type"] === "episode") {
|
||||||
|
@ -97,8 +100,14 @@ export default async () => {
|
||||||
const nbaRes = await fetch(
|
const nbaRes = await fetch(
|
||||||
"https://cdn.nba.com/static/json/liveData/scoreboard/todaysScoreboard_00.json"
|
"https://cdn.nba.com/static/json/liveData/scoreboard/todaysScoreboard_00.json"
|
||||||
)
|
)
|
||||||
.then((data) => data.json())
|
.then((data) => {
|
||||||
.catch();
|
if (data.ok) return data.json()
|
||||||
|
throw new Error('Something went wrong with the NBA endpoint.');
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
console.log(err);
|
||||||
|
return {}
|
||||||
|
});
|
||||||
const games = nbaRes?.scoreboard?.games;
|
const games = nbaRes?.scoreboard?.games;
|
||||||
|
|
||||||
if (games && games.length) {
|
if (games && games.length) {
|
||||||
|
@ -156,20 +165,27 @@ export default async () => {
|
||||||
{
|
{
|
||||||
type: "json",
|
type: "json",
|
||||||
}
|
}
|
||||||
).catch();
|
).then((data) => {
|
||||||
const trackData = await trackRes.json();
|
if (data.ok) return data.json()
|
||||||
|
throw new Error('Something went wrong with the Last.fm endpoint.');
|
||||||
|
}).catch(err => {
|
||||||
|
console.log(err);
|
||||||
|
return {}
|
||||||
|
});
|
||||||
const mbidRes = await fetch("https://coryd.dev/api/mbids", {
|
const mbidRes = await fetch("https://coryd.dev/api/mbids", {
|
||||||
type: "json",
|
type: "json",
|
||||||
}).catch();
|
}).then((data) => {
|
||||||
const mbidData = await mbidRes.json();
|
if (data.ok) return data.json()
|
||||||
const track = trackData["recenttracks"]["track"][0];
|
throw new Error('Something went wrong with the mbid endpoint.');
|
||||||
|
}).catch(err => {
|
||||||
|
console.log(err);
|
||||||
|
return {}
|
||||||
|
});
|
||||||
|
const track = trackRes["recenttracks"]["track"][0];
|
||||||
const artist = track["artist"]["#text"];
|
const artist = track["artist"]["#text"];
|
||||||
let mbid = track["artist"]["mbid"];
|
let mbid = track["artist"]["mbid"];
|
||||||
let genre = "";
|
let genre = "";
|
||||||
|
const mbidMap = (artist) => mbidRes[artist.toLowerCase()] || "";
|
||||||
const mbidMap = (artist) => {
|
|
||||||
return mbidData[artist.toLowerCase()] || "";
|
|
||||||
};
|
|
||||||
|
|
||||||
// mbid mismatches
|
// mbid mismatches
|
||||||
if (mbidMap(artist) !== "") mbid = mbidMap(artist);
|
if (mbidMap(artist) !== "") mbid = mbidMap(artist);
|
||||||
|
@ -188,10 +204,14 @@ export default async () => {
|
||||||
const genreUrl = `https://musicbrainz.org/ws/2/artist/${mbid}?inc=aliases+genres&fmt=json`;
|
const genreUrl = `https://musicbrainz.org/ws/2/artist/${mbid}?inc=aliases+genres&fmt=json`;
|
||||||
const genreRes = await fetch(genreUrl, {
|
const genreRes = await fetch(genreUrl, {
|
||||||
type: "json",
|
type: "json",
|
||||||
}).catch();
|
}).then((data) => {
|
||||||
const genreData = await genreRes.json();
|
if (data.ok) return data.json()
|
||||||
genre =
|
throw new Error('Something went wrong with the MusicBrainz endpoint.');
|
||||||
genreData.genres.sort((a, b) => b.count - a.count)[0]?.["name"] || "";
|
}).catch(err => {
|
||||||
|
console.log(err);
|
||||||
|
return {}
|
||||||
|
});
|
||||||
|
genre = genreRes['genres'].sort((a, b) => b.count - a.count)[0]?.["name"] || "";
|
||||||
}
|
}
|
||||||
|
|
||||||
return Response.json(
|
return Response.json(
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "coryd.dev",
|
"name": "coryd.dev",
|
||||||
"version": "6.5.10",
|
"version": "6.5.11",
|
||||||
"description": "The source for my personal site. Built using 11ty and hosted on Netlify.",
|
"description": "The source for my personal site. Built using 11ty and hosted on Netlify.",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|
|
@ -24,10 +24,17 @@ class NowPlaying extends HTMLElement {
|
||||||
const content = this.querySelector('[data-key="content"]')
|
const content = this.querySelector('[data-key="content"]')
|
||||||
const value = data['content']
|
const value = data['content']
|
||||||
|
|
||||||
loading.style.opacity = '0'
|
if (!value) {
|
||||||
loading.style.display = 'none'
|
loading.style.display = 'none'
|
||||||
content.style.opacity = '1'
|
content.style.display = 'none'
|
||||||
content.innerHTML = value
|
}
|
||||||
|
|
||||||
|
if (value) {
|
||||||
|
loading.style.opacity = '0'
|
||||||
|
loading.style.display = 'none'
|
||||||
|
content.style.opacity = '1'
|
||||||
|
content.innerHTML = value
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
get template() {
|
get template() {
|
||||||
|
|
Reference in a new issue