Skip to content

Instantly share code, notes, and snippets.

@danydodson

danydodson/codeswing.json

Last active Apr 8, 2021
Embed
What would you like to do?
sandbox
{
"scripts": [],
"showConsole": true
}
url = {
response: {
status: 200,
statusText: 'good'
}
}
async function fetchJson(url) {
const response = await fetch(url)
if (!response.ok) {
throw new Error(
`GET ${url} ${response.status} - ${response.statusText} `,
)
}
return response.json()
}
function mapReleaseGroups(groups) {
return groups.map((group) => {
return {
id: group.id,
title: group.title,
}
})
}
function findWikidataResource(relations) {
let wikiRelation = relations.find((rel) => rel.type === "wikidata")
return wikiRelation?.url?.resource
}
export async function getArtist(id) {
if (!id) {
throw Error("No id specified")
}
const url = new URL(`http://musicbrainz.org/ws/2/artist/${id}`)
url.searchParams.set("fmt", "json")
url.searchParams.set("inc", "url-rels+release-groups")
const data = await fetchJson(url)
return {
name: data.name,
albums: mapReleaseGroups(data["release-groups"]),
wikidataKey: findWikidataResource(data.relations),
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment