bandcamp-fetch
Version:
Scrape Bandcamp content
126 lines • 5.4 kB
JavaScript
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _LimiterBandAPI_limiter;
import BaseAPIWithImageSupport from '../common/BaseAPIWithImageSupport.js';
import { normalizeUrl } from '../utils/Parse.js';
import BandInfoParser from './BandInfoParser.js';
import DiscographyParser from './DiscographyParser.js';
import LabelArtistsParser from './LabelArtistsParser.js';
export default class BandAPI extends BaseAPIWithImageSupport {
async getDiscography(params) {
const imageConstants = await this.imageAPI.getConstants();
const opts = {
imageBaseUrl: imageConstants.baseUrl,
bandUrl: params.bandUrl,
imageFormat: await this.imageAPI.getFormat(params.imageFormat, 9)
};
const html = await this.fetch(normalizeUrl('music', params.bandUrl));
return DiscographyParser.parseDiscography(html, opts);
}
async getInfo(params) {
const opts = {
bandUrl: params.bandUrl,
imageFormat: await this.imageAPI.getFormat(params.imageFormat, 21)
};
const url = BandAPI.getUrl(params.bandUrl);
const html = await this.fetch(url);
const result = BandInfoParser.parseInfo(html, opts);
// Return if result is complete
if (BandAPI.isInfoComplete(result)) {
return result;
}
// Info lacking name or label (for artist) - try getting them from music page
const musicUrl = BandAPI.getUrl(params.bandUrl, 'music');
const musicHtml = await this.fetch(musicUrl);
const info = BandInfoParser.parseInfo(musicHtml, opts);
BandAPI.fillInfo(result, info);
// Return if result is complete
if (BandAPI.isInfoComplete(result)) {
return result;
}
// Info is still lacking name or label (for artist) - last try with fetching
// From discog's first album or track
const discogItems = await this.getDiscography(params);
if (discogItems[0]) {
const url = discogItems[0]?.url;
if (url) {
const html = await this.fetch(url);
const info = BandInfoParser.parseInfo(html, opts);
BandAPI.fillInfo(result, info);
}
}
if (result.url === null) {
result.url = params.bandUrl;
}
return result;
}
async getLabelArtists(params) {
const opts = {
labelUrl: params.labelUrl,
imageFormat: await this.imageAPI.getFormat(params.imageFormat)
};
const html = await this.fetch(normalizeUrl('artists', params.labelUrl));
return LabelArtistsParser.parseLabelArtists(html, opts);
}
/**
* @internal
*/
static getUrl(artistOrLabelUrl, path, labelId) {
let url = path ? normalizeUrl(path, artistOrLabelUrl) : artistOrLabelUrl;
if (labelId) {
url += `/?label=${encodeURIComponent(labelId)}`;
}
return url;
}
/**
* @internal
*/
static isInfoComplete(data) {
return data.name && data.url && (data.type === 'label' || data.label);
}
/**
* @internal
*/
static fillInfo(target, src) {
if (target.name === null) {
target.name = src.name;
}
if (target.url === null) {
target.url = src.url;
}
if (target.type === 'artist' &&
src.type === 'artist' &&
!target.label &&
src.label) {
target.label = src.label;
}
return target;
}
}
export class LimiterBandAPI extends BandAPI {
constructor(params) {
super(params);
_LimiterBandAPI_limiter.set(this, void 0);
__classPrivateFieldSet(this, _LimiterBandAPI_limiter, params.limiter, "f");
}
async getDiscography(params) {
return __classPrivateFieldGet(this, _LimiterBandAPI_limiter, "f").schedule(() => super.getDiscography(params));
}
async getInfo(params) {
return __classPrivateFieldGet(this, _LimiterBandAPI_limiter, "f").schedule(() => super.getInfo(params));
}
async getLabelArtists(params) {
return __classPrivateFieldGet(this, _LimiterBandAPI_limiter, "f").schedule(() => super.getLabelArtists(params));
}
}
_LimiterBandAPI_limiter = new WeakMap();
//# sourceMappingURL=BandAPI.js.map