bandcamp-fetch
Version:
Scrape Bandcamp content
151 lines • 6.81 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 _LimiterFanAPI_limiter;
import { URLS } from '../utils/Constants.js';
import { FetchError, FetchMethod } from '../utils/Fetcher.js';
import FanCollectionParser from './FanCollectionParser.js';
import FanFollowingParser from './FanFollowingParser.js';
import FanInfoParser from './FanInfoParser.js';
import FanWishlistParser from './FanWishlistParser.js';
import BaseAPIWithImageSupport from '../common/BaseAPIWithImageSupport.js';
export default class FanAPI extends BaseAPIWithImageSupport {
async getInfo(params) {
if (!params.username) {
const username = await this.getLoggedInFanUsername();
return this.getInfo({
...params,
username
});
}
const imageConstants = await this.imageAPI.getConstants();
const fanPageUrl = FanAPI.getFanPageUrl(params.username);
const opts = {
imageBaseUrl: imageConstants.baseUrl,
imageFormat: await this.imageAPI.getFormat(params.imageFormat, 20)
};
const html = await this.fetch(fanPageUrl);
return FanInfoParser.parseInfo(html, opts);
}
async getCollection(params) {
return await this.getItems({
...params,
defaultImageFormat: 9,
continuationUrl: URLS.FAN_CONTINUATION.COLLECTION,
parsePageFn: FanCollectionParser.parseCollectionFromPage.bind(FanCollectionParser),
parseContinuationFn: FanCollectionParser.parseCollectionFromContinuation.bind(FanCollectionParser)
});
}
async getWishlist(params) {
return await this.getItems({
...params,
defaultImageFormat: 9,
continuationUrl: URLS.FAN_CONTINUATION.WISHLIST,
parsePageFn: FanWishlistParser.parseWishlistFromPage.bind(FanWishlistParser),
parseContinuationFn: FanWishlistParser.parseWishlistFromContinuation.bind(FanWishlistParser)
});
}
async getFollowingArtistsAndLabels(params) {
return await this.getItems({
...params,
defaultImageFormat: 21,
continuationUrl: URLS.FAN_CONTINUATION.FOLLOWING_BANDS,
parsePageFn: FanFollowingParser.parseFollowingBandsFromPage.bind(FanFollowingParser),
parseContinuationFn: FanFollowingParser.parseFollowingBandsFromContinuation.bind(FanFollowingParser)
});
}
async getFollowingGenres(params) {
return await this.getItems({
...params,
defaultImageFormat: 3,
continuationUrl: URLS.FAN_CONTINUATION.FOLLOWING_GENRES,
parsePageFn: FanFollowingParser.parseFollowingGenresFromPage.bind(FanFollowingParser),
parseContinuationFn: FanFollowingParser.parseFollowingGenresFromContinuation.bind(FanFollowingParser)
});
}
/**
* @internal
*/
async getItems(params) {
const { target, imageFormat, defaultImageFormat, continuationUrl } = params;
if (!target) {
const username = await this.getLoggedInFanUsername();
return this.getItems({
...params,
target: username
});
}
const imageConstants = await this.imageAPI.getConstants();
const opts = {
imageBaseUrl: imageConstants.baseUrl,
imageFormat: await this.imageAPI.getFormat(imageFormat, defaultImageFormat)
};
if (!FanAPI.isContinuation(target)) {
const fanPageUrl = FanAPI.getFanPageUrl(target);
const html = await this.fetch(fanPageUrl);
return params.parsePageFn(html, opts);
}
// Continuation
if (!continuationUrl) {
throw new FetchError('Unable to fetch fan contents: target is continuation token but continuation URL is missing.');
}
const payload = {
fan_id: target.fanId,
older_than_token: target.token,
count: 20
};
const json = await this.fetch(continuationUrl, true, FetchMethod.POST, payload);
return params.parseContinuationFn(json, target, opts);
}
/**
* @internal
*/
static getFanPageUrl(username) {
return `${URLS.SITE_URL}/${username}`;
}
/**
* @internal
*/
static isContinuation(target) {
return typeof target === 'object' && target.fanId && target.token;
}
/**
* @internal
*/
async getLoggedInFanUsername() {
const html = await this.fetch(URLS.SITE_URL);
return FanInfoParser.parseLoggedInFanUsername(html);
}
}
export class LimiterFanAPI extends FanAPI {
constructor(params) {
super(params);
_LimiterFanAPI_limiter.set(this, void 0);
__classPrivateFieldSet(this, _LimiterFanAPI_limiter, params.limiter, "f");
}
async getInfo(params) {
return __classPrivateFieldGet(this, _LimiterFanAPI_limiter, "f").schedule(() => super.getInfo(params));
}
async getCollection(params) {
return __classPrivateFieldGet(this, _LimiterFanAPI_limiter, "f").schedule(() => super.getCollection(params));
}
async getWishlist(params) {
return __classPrivateFieldGet(this, _LimiterFanAPI_limiter, "f").schedule(() => super.getWishlist(params));
}
async getFollowingArtistsAndLabels(params) {
return __classPrivateFieldGet(this, _LimiterFanAPI_limiter, "f").schedule(() => super.getFollowingArtistsAndLabels(params));
}
async getFollowingGenres(params) {
return __classPrivateFieldGet(this, _LimiterFanAPI_limiter, "f").schedule(() => super.getFollowingGenres(params));
}
}
_LimiterFanAPI_limiter = new WeakMap();
//# sourceMappingURL=FanAPI.js.map