-
-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
touch lookup logic, extract extra metadata
- Loading branch information
Showing
2 changed files
with
88 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,57 @@ | ||
async function gatherMusicBrainzMetadata(track, trackLogger) | ||
{ | ||
var musicBrainz = []; | ||
if (track.isrc !== "") { | ||
trackLogger.print('| [\u2022] Obtaining MusicBrainz metadata...'); | ||
|
||
const got = require('got'); | ||
var parser = require('xml2js'); | ||
|
||
await got(`https://musicbrainz.org/ws/2/isrc/${track.isrc}?inc=artist-credits+releases`).then(response => { | ||
trackLogger.write('[done]\n'); | ||
try { | ||
// Should 'explicitArray: false' be used ? | ||
parser.parseString(response.body, { trim: true, mergeAttrs: true }, function (err, result) { | ||
const recording = result.metadata.isrc[0]['recording-list'][0]['recording'][0]; | ||
|
||
try { | ||
musicBrainz.trackId = recording['id'][0]; | ||
} catch { }; | ||
trackLogger.log(`| \u27a4 TrackId: ${musicBrainz.trackId}`); | ||
try { | ||
musicBrainz.artistId = recording['artist-credit'][0]['name-credit'][0]['artist'][0]['id'][0]; | ||
} catch { }; | ||
trackLogger.log(`| \u27a4 ArtistId: ${musicBrainz.artistId}`); | ||
|
||
// Searching for a matching album | ||
const releases = recording['release-list'][0]['release'].filter(obj => { | ||
const title = obj.title[0].replace(/[\u2018\u2019]/g, "'").replace(/[\u201C\u201D]/g, '"'); // Removing weird characters that can cause fails | ||
return track.album.localeCompare(title) == 0; | ||
}); | ||
|
||
try { | ||
musicBrainz.albumId = releases[0]['id'][0]; | ||
bim.hello = true; | ||
} | ||
catch { }; | ||
trackLogger.log(`| \u27a4 AlbumId: ${musicBrainz.albumId}`); | ||
try { | ||
musicBrainz.albumArtistId = releases[0]['artist-credit'][0]['name-credit'][0]['artist'][0]['id'][0]; | ||
} catch { }; | ||
trackLogger.log(`| \u27a4 AlbumArtistId: ${musicBrainz.albumArtistId}`); | ||
}); | ||
} catch (error) { | ||
trackLogger.log(error); | ||
} | ||
// | ||
}).catch(error => { | ||
trackLogger.write(`[failed, ${error.message}]\n`); | ||
}); | ||
} | ||
return musicBrainz; | ||
const got = require('got'); | ||
const {parseStringPromise: xml2js} = require('xml2js'); | ||
|
||
class MusicBrainzError extends Error { | ||
constructor(message, statusCode) { | ||
super(message); | ||
if (statusCode) this.statusCode = statusCode; | ||
} | ||
} | ||
|
||
async function query(entity_type, entity, args) { | ||
let response = await got(`https://musicbrainz.org/ws/2/${entity_type}/${entity}?inc=artists+releases+discids`, { | ||
searchParams: {...args, ...('inc' in args ? {inc: args.inc.join('+')} : {}), ...(args.json ? {fmt: 'json'} : {})}, | ||
}); | ||
let body; | ||
try { | ||
body = response.body.startsWith('<?xml') | ||
? await xml2js(response.body, {trim: true, mergeAttrs: true, explicitRoot: false, explicitArray: false}) | ||
: JSON.parse(response.body); | ||
} catch { | ||
throw new MusicBrainzError('Invalid Server Response'); | ||
} | ||
if (response.statusCode !== 200) { | ||
throw new MusicBrainzError(body.error || 'An error occurred', response.statusCode); | ||
} | ||
return body; | ||
} | ||
|
||
module.exports = { | ||
gatherMusicBrainzMetadata, | ||
async function lookupISRC(isrc, storefront) { | ||
let { | ||
recording: { | ||
id: trackId, | ||
'release-list': {release: releases}, | ||
}, | ||
} = (await query('isrc', isrc, {inc: ['releases']})).isrc['recording-list']; | ||
releases = Array.isArray(releases) ? releases : [releases]; | ||
|
||
let {id: releaseId} = releases.find(release => release.country === storefront) || releases[0]; | ||
|
||
let release = await query('release', releaseId, {inc: ['artists', 'release-groups', 'media'], json: true}); | ||
|
||
let {artist: artistMeta} = release['artist-credit'][0]; | ||
return { | ||
trackId, | ||
releaseId, | ||
artistId: artistMeta.id, | ||
artistSortOrder: artistMeta['sort-name'], | ||
releaseGroupId: release['release-group'].id, | ||
releaseType: release['release-group']['primary-type'], | ||
barcode: release.barcode, | ||
releaseStatus: release.status.toLowerCase(), | ||
script: release['text-representation'].script, | ||
media: release.media[0].format, | ||
}; | ||
|
||
} | ||
|
||
module.exports = {lookupISRC}; |