79369493

Date: 2025-01-19 18:09:30
Score: 2
Natty:
Report link

The following code uses the MusicBrainz API to find all artist relations of Queen - Made in Heaven. It uses musicbrainz-api, a module I wrote, to ease communication with the MusicBrainz API.

async function findReleaseGroup(mbApi, artist, title) {
  const query = `artist:"${artist}" AND release:"${title}"`;
  console.log('Resolving release group: ' + query);
  const result = await mbApi.search('release-group', {query});
  return result.count > 0 ? result["release-groups"][0] : undefined;
}

async function findAllRelatedArtist(mbApi, mbidRelease) {
  // Lookup metadata Queen: "Made In Heaven" release group
  const mbidMadeInHeaven = '780e6a16-9384-307d-ae65-02e1d6313753';
  const relInfoGrp = await mbApi.lookup('release-group', mbidMadeInHeaven, ['releases', '']);
  let release = relInfoGrp.releases[0]; // Pick the first (some) release from the release-group
  console.log(`Using Release MBID=${release.id}`);
  release = await mbApi.lookup('release', release.id, ['artists', 'recordings']);
  
  const seenRelations = new Set(); // Set to track unique relations

  for(const media of release.media) {
    for(const track of media.tracks) {
      const recording = await mbApi.lookup('recording', track.recording.id, ['artists', 'artist-rels']);
      for(const relation of recording.relations) {
        const relationKey = `${relation.type}-${relation.artist.name}`; // Create a unique key
        if (!seenRelations.has(relationKey)) {
          seenRelations.add(relationKey); // Add the key to the set
          console.log(`relation ${relation.type}: ${relation.artist.name}`); // Print unique relation
        }
      }
    }
  }
}

async function run() {
  console.log('Loading musicbrainz-api...');
  const {MusicBrainzApi} = await import('https://cdn.jsdelivr.net/npm/[email protected]/+esm');
  const mbApi = new MusicBrainzApi({
    appName: 'stackoverflow.com/questions/74498924',
    appVersion: '0.1.0',
    appContactInfo: 'Borewit',
  });
  const releaseGroup = await findReleaseGroup(mbApi, "Queen", "Made in Heaven");
  console.log(`Using Release Group MBID=${releaseGroup.id}`);
  await findAllRelatedArtist(mbApi, releaseGroup.id);
}

run().then(() => {
  console.log('The End.');
}, err => {
  console.error(err);
});

Reasons:
  • Blacklisted phrase (1): stackoverflow
  • Probably link only (1):
  • Contains signature (1):
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: Borewit