bandcamp-fetch
Version:
Scrape Bandcamp content
159 lines • 7.6 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 __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var _LimiterFanAPI_limiter;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LimiterFanAPI = void 0;
const Constants_js_1 = require("../utils/Constants.js");
const Fetcher_js_1 = require("../utils/Fetcher.js");
const FanCollectionParser_js_1 = __importDefault(require("./FanCollectionParser.js"));
const FanFollowingParser_js_1 = __importDefault(require("./FanFollowingParser.js"));
const FanInfoParser_js_1 = __importDefault(require("./FanInfoParser.js"));
const FanWishlistParser_js_1 = __importDefault(require("./FanWishlistParser.js"));
const BaseAPIWithImageSupport_js_1 = __importDefault(require("../common/BaseAPIWithImageSupport.js"));
class FanAPI extends BaseAPIWithImageSupport_js_1.default {
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_js_1.default.parseInfo(html, opts);
}
async getCollection(params) {
return await this.getItems({
...params,
defaultImageFormat: 9,
continuationUrl: Constants_js_1.URLS.FAN_CONTINUATION.COLLECTION,
parsePageFn: FanCollectionParser_js_1.default.parseCollectionFromPage.bind(FanCollectionParser_js_1.default),
parseContinuationFn: FanCollectionParser_js_1.default.parseCollectionFromContinuation.bind(FanCollectionParser_js_1.default)
});
}
async getWishlist(params) {
return await this.getItems({
...params,
defaultImageFormat: 9,
continuationUrl: Constants_js_1.URLS.FAN_CONTINUATION.WISHLIST,
parsePageFn: FanWishlistParser_js_1.default.parseWishlistFromPage.bind(FanWishlistParser_js_1.default),
parseContinuationFn: FanWishlistParser_js_1.default.parseWishlistFromContinuation.bind(FanWishlistParser_js_1.default)
});
}
async getFollowingArtistsAndLabels(params) {
return await this.getItems({
...params,
defaultImageFormat: 21,
continuationUrl: Constants_js_1.URLS.FAN_CONTINUATION.FOLLOWING_BANDS,
parsePageFn: FanFollowingParser_js_1.default.parseFollowingBandsFromPage.bind(FanFollowingParser_js_1.default),
parseContinuationFn: FanFollowingParser_js_1.default.parseFollowingBandsFromContinuation.bind(FanFollowingParser_js_1.default)
});
}
async getFollowingGenres(params) {
return await this.getItems({
...params,
defaultImageFormat: 3,
continuationUrl: Constants_js_1.URLS.FAN_CONTINUATION.FOLLOWING_GENRES,
parsePageFn: FanFollowingParser_js_1.default.parseFollowingGenresFromPage.bind(FanFollowingParser_js_1.default),
parseContinuationFn: FanFollowingParser_js_1.default.parseFollowingGenresFromContinuation.bind(FanFollowingParser_js_1.default)
});
}
/**
* @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 Fetcher_js_1.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, Fetcher_js_1.FetchMethod.POST, payload);
return params.parseContinuationFn(json, target, opts);
}
/**
* @internal
*/
static getFanPageUrl(username) {
return `${Constants_js_1.URLS.SITE_URL}/${username}`;
}
/**
* @internal
*/
static isContinuation(target) {
return typeof target === 'object' && target.fanId && target.token;
}
/**
* @internal
*/
async getLoggedInFanUsername() {
const html = await this.fetch(Constants_js_1.URLS.SITE_URL);
return FanInfoParser_js_1.default.parseLoggedInFanUsername(html);
}
}
exports.default = FanAPI;
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));
}
}
exports.LimiterFanAPI = LimiterFanAPI;
_LimiterFanAPI_limiter = new WeakMap();
//# sourceMappingURL=FanAPI.js.map