getArtistOrLabelInfo(): more complete fetching

This commit is contained in:
patrickkfkan 2021-10-20 15:23:28 +08:00
parent 279e86a58e
commit c1590e150f

View File

@ -174,37 +174,56 @@ async function getArtistOrLabelInfo(artistOrLabelUrl, options = {}) {
artistOrLabelUrl,
imageFormat: await _parseImageFormatArg(options.imageFormat)
};
let url = utils.getUrl('music', artistOrLabelUrl);
const _getUrl = (path) => {
let _url = path ? utils.getUrl(path, artistOrLabelUrl) : artistOrLabelUrl;
if (options.labelId) {
url += '/?label=' + encodeURIComponent(options.labelId);
_url += '/?label=' + encodeURIComponent(options.labelId);
}
// 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;
return _url;
};
const _isInfoComplete = (data) => {
return data.name !== '' && (data.type === 'label' || data.label !== null);
}
else {
return getDiscography(artistOrLabelUrl, options)
.then( discographyItems => {
const firstAlbumOrTrack = discographyItems[0];
if (firstAlbumOrTrack) {
return firstAlbumOrTrack.url;
let url = _getUrl();
let html = await _fetchPage(url);
let result = parser.parseArtistOrLabelInfo(html, opts);
// Return if result is complete
if (_isInfoComplete(result)) {
return result;
}
else {
// fallback
return artistOrLabelUrl;
// 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;
}
})
.then( url => _fetchPage(url) )
.then( html => parser.parseArtistOrLabelInfo(html, opts) );
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 = {}) {