diff --git a/lib/index.js b/lib/index.js index 12d8253..a97f46b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -174,37 +174,56 @@ async function getArtistOrLabelInfo(artistOrLabelUrl, options = {}) { artistOrLabelUrl, imageFormat: await _parseImageFormatArg(options.imageFormat) }; - let url = utils.getUrl('music', artistOrLabelUrl); - if (options.labelId) { - url += '/?label=' + encodeURIComponent(options.labelId); + const _getUrl = (path) => { + let _url = path ? utils.getUrl(path, artistOrLabelUrl) : artistOrLabelUrl; + if (options.labelId) { + _url += '/?label=' + encodeURIComponent(options.labelId); + } + return _url; + }; + const _isInfoComplete = (data) => { + return data.name !== '' && (data.type === 'label' || data.label !== null); } - // 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 _fetchPage(url) - .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 => _fetchPage(url) ) - .then( html => parser.parseArtistOrLabelInfo(html, opts) ); - } - }); + + let url = _getUrl(); + let html = await _fetchPage(url); + let result = parser.parseArtistOrLabelInfo(html, opts); + // Return if result is complete + if (_isInfoComplete(result)) { + return result; + } + + // Info lacking name or label (for artist) - try getting them from music page + url = _getUrl('music'); + html = await _fetchPage(url); + let info = parser.parseArtistOrLabelInfo(html, opts); + if (result.name === '') { + result.name = info.name; + } + if (result.label === null) { + result.label = info.label; + } + // Return if result is complete + if (_isInfoComplete(result)) { + return result; + } + + // Info is still lacking name or label (for artist) - last try with fetching + // from discog's first album or track + let discogItems = await getDiscography(artistOrLabelUrl, options); + if (discogItems[0]) { + url = discogItems[0].url; + html = await _fetchPage(url); + info = parser.parseArtistOrLabelInfo(html, opts); + if (result.name === '') { + result.name = info.name; + } + if (result.label === null) { + result.label = info.label; + } + } + + return result; } async function getLabelArtists(labelUrl, options = {}) {