From 5674fd1cc0aef1818994db38c64368573dcd8b7e Mon Sep 17 00:00:00 2001 From: patrickkfkan Date: Mon, 25 Jan 2021 23:44:54 +0800 Subject: [PATCH] More robust fetching of artist / label info --- lib/index.js | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/lib/index.js b/lib/index.js index a9c0a19..ad29ba8 100644 --- a/lib/index.js +++ b/lib/index.js @@ -192,11 +192,35 @@ async function getArtistOrLabelInfo(artistOrLabelUrl, options = {}) { artistOrLabelUrl, imageFormat: await _parseImageFormatArg(options.imageFormat) }; - // Some pages don't actually show the 'bio' column. - // The /music page does seem to always show it though, so parse from that. + // The landing page of some artists and labels don't actually + // contain the 'bio' column, so we fetch from the + // 'music' page instead. For artists, if the 'music' page does not + // have the artist info, we shall try with an album or track page + // (this is inefficient...perhaps there is a better way?). return fetch(utils.getUrl('music', artistOrLabelUrl)) .then( res => res.text() ) - .then( html => parser.parseArtistOrLabelInfo(html, opts) ); + .then( html => parser.parseArtistOrLabelInfo(html, opts) ) + .then( info => { + if (info.type === 'label' || info.name !== '') { + return info; + } + else { + return getDiscography(artistOrLabelUrl, options) + .then( discographyItems => { + const firstAlbumOrTrack = discographyItems[0]; + if (firstAlbumOrTrack) { + return firstAlbumOrTrack.url; + } + else { + // fallback + return artistOrLabelUrl; + } + }) + .then( url => fetch(url) ) + .then( res => res.text() ) + .then( html => parser.parseArtistOrLabelInfo(html, opts) ); + } + }); } async function getLabelArtists(labelUrl, options = {}) {