diff --git a/lib/index.js b/lib/index.js index afc1e13..ffefe07 100644 --- a/lib/index.js +++ b/lib/index.js @@ -108,7 +108,9 @@ async function getTrackInfo(trackUrl, options = {}) { } async function getDiscography(artistOrLabelUrl, options = {}) { + const imageConstants = await _getImageConstants(); const opts = { + imageBaseUrl: imageConstants.baseUrl, artistOrLabelUrl, imageFormat: await _parseImageFormatArg(options.imageFormat) }; diff --git a/lib/parser.js b/lib/parser.js index 5bddc27..6502b18 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -248,9 +248,45 @@ function parseTrackInfo(html, opts) { function parseDiscography(html, opts) { const $ = cheerio.load(html); + + // One-album / one-track artists don't have a discography page. + // The page for the album or track will be loaded instead. + // Check if this is the case and handle accordingly + const currentAlbumOrTrack = $('script[type="application/ld+json"]'); + let isOneTrack = false, + isOneAlbum = false; + if (currentAlbumOrTrack.length) { + currentAlbumOrTrackData = JSON.parse(currentAlbumOrTrack.html()); + if (typeof currentAlbumOrTrackData === 'object') { + // Check if there is a 'discography' element and, if there is, whether + // it is hidden or has only one track / album child + const discographyEl = $('#discography'); + if (discographyEl.length === 0 || discographyEl.css('display') === 'none' || discographyEl.find('li').length === 1) { + currentAlbumOrTrackUrl = utils.splitUrl(currentAlbumOrTrackData['@id']); + isOneTrack = currentAlbumOrTrackUrl.path.startsWith('/track/'); + isOneAlbum = currentAlbumOrTrackUrl.path.startsWith('/album/'); + } + } + } + if (isOneTrack || isOneAlbum) { + const newOpts = { + imageBaseUrl: opts.imageBaseUrl, + albumImageFormat: opts.imageFormat, + artistImageFormat: null, + includeRawData: false + }; + let info = isOneTrack ? parseTrackInfo(html, newOpts) : parseAlbumInfo(html, newOpts); + return [{ + url: info.url, + type: info.type, + name: info.name || '', + imageUrl: info.imageUrl || null, + artist: info.artist.name + }]; + } + const allLinks = $('a'); const items = {}; - const isLabel = $('a[href="/artists"]').length; const defaultArtistName = $('#band-name-location').find('.title').text(); allLinks.each( (index, link) => { link = $(link);