bandcamp-fetch
Version:
Scrape Bandcamp content
156 lines • 7.37 kB
JavaScript
;
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 __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 __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var _a, _DiscoveryAPI_isContinuation, _LimiterDiscoveryAPI_limiter;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LimiterDiscoveryAPI = void 0;
const BaseAPIWithImageSupport_js_1 = __importDefault(require("../common/BaseAPIWithImageSupport.js"));
const Cache_js_1 = require("../utils/Cache.js");
const Constants_js_1 = require("../utils/Constants.js");
const Fetcher_js_1 = require("../utils/Fetcher.js");
const DiscoverOptionsParser_js_1 = __importDefault(require("./DiscoverOptionsParser.js"));
const DiscoverResultParser_js_1 = __importDefault(require("./DiscoverResultParser.js"));
class DiscoveryAPI extends BaseAPIWithImageSupport_js_1.default {
async getAvailableOptions() {
return this.cache.getOrSet(Cache_js_1.CacheDataType.Constants, 'discoverOptions', async () => {
const html = await this.fetch(Constants_js_1.URLS.DISCOVER.SITE);
return DiscoverOptionsParser_js_1.default.parseOptions(html);
});
}
async sanitizeDiscoverParams(params) {
const options = await this.getAvailableOptions();
const _getOptionValue = (optArr, value, defaultIndex = 0) => {
if (value !== undefined && optArr) {
const opt = optArr.find((o) => o.value == value);
if (opt) {
return opt.value;
}
}
if (optArr) {
return optArr[defaultIndex]?.value;
}
return undefined;
};
const category = _getOptionValue(options.categories, params?.category) || 0;
const genre = _getOptionValue(options.genres, params?.genre, -1);
const sortBy = _getOptionValue(options.sortBys, params?.sortBy, -1) || 'top';
const location = _getOptionValue(options.locations, params?.location) || 0;
const time = _getOptionValue(options.times, params?.time) || -1;
const sanitized = {
category,
sortBy,
location,
time,
size: params?.size || 60
};
if (genre) {
sanitized.genre = genre;
const subgenreOptions = options.subgenres[genre];
if (subgenreOptions) {
const subgenre = _getOptionValue(subgenreOptions, params?.subgenre, -1);
if (subgenre) {
sanitized.subgenre = subgenre;
}
}
}
if (params?.customTags) {
sanitized.customTags = params.customTags;
}
return sanitized;
}
async discover(params) {
const imageConstants = await this.imageAPI.getConstants();
const options = await this.getAvailableOptions();
const opts = {
imageBaseUrl: imageConstants.baseUrl,
albumImageFormat: await this.imageAPI.getFormat(params?.albumImageFormat, 9),
artistImageFormat: await this.imageAPI.getFormat(params?.artistImageFormat, 21),
merchImageFormat: await this.imageAPI.getFormat(params?.merchImageFormat, 9)
};
let payload;
let sanitizedParams;
if (params && __classPrivateFieldGet(_a, _a, "m", _DiscoveryAPI_isContinuation).call(_a, params)) {
sanitizedParams = { ...params };
Reflect.deleteProperty(sanitizedParams, 'cursor');
payload = _a.getDiscoverRequestPayload(params);
}
else {
sanitizedParams = await this.sanitizeDiscoverParams(params);
payload = _a.getDiscoverRequestPayload(sanitizedParams);
}
const json = await this.fetch(Constants_js_1.URLS.DISCOVER.API, true, Fetcher_js_1.FetchMethod.POST, payload);
const result = DiscoverResultParser_js_1.default.parseDiscoverResult(json, opts, sanitizedParams, options);
if (result.continuation) {
if (!params?.albumImageFormat) {
delete result.continuation.albumImageFormat;
}
if (!params?.artistImageFormat) {
delete result.continuation.artistImageFormat;
}
if (!params?.merchImageFormat) {
delete result.continuation.merchImageFormat;
}
}
return result;
}
/**
* @internal
*/
static getDiscoverRequestPayload(params) {
const tagNames = [];
if (params.genre) {
tagNames.push(params.genre);
}
if (params.subgenre) {
tagNames.push(params.subgenre);
}
if (params.customTags) {
tagNames.push(...params.customTags);
}
return {
category_id: params.category,
cursor: __classPrivateFieldGet(this, _a, "m", _DiscoveryAPI_isContinuation).call(this, params) ? params.cursor : '*',
geoname_id: params.location,
include_result_types: ['a', 's'],
size: params.size,
slice: params.sortBy,
tag_norm_names: tagNames,
time_facet_id: params.time === -1 ? null : params.time
};
}
}
_a = DiscoveryAPI, _DiscoveryAPI_isContinuation = function _DiscoveryAPI_isContinuation(params) {
return Reflect.has(params, 'cursor');
};
exports.default = DiscoveryAPI;
class LimiterDiscoveryAPI extends DiscoveryAPI {
constructor(params) {
super(params);
_LimiterDiscoveryAPI_limiter.set(this, void 0);
__classPrivateFieldSet(this, _LimiterDiscoveryAPI_limiter, params.limiter, "f");
}
async getAvailableOptions() {
return __classPrivateFieldGet(this, _LimiterDiscoveryAPI_limiter, "f").schedule(() => super.getAvailableOptions());
}
async sanitizeDiscoverParams(params) {
return __classPrivateFieldGet(this, _LimiterDiscoveryAPI_limiter, "f").schedule(() => super.sanitizeDiscoverParams(params));
}
async discover(params) {
return __classPrivateFieldGet(this, _LimiterDiscoveryAPI_limiter, "f").schedule(() => super.discover(params));
}
}
exports.LimiterDiscoveryAPI = LimiterDiscoveryAPI;
_LimiterDiscoveryAPI_limiter = new WeakMap();
//# sourceMappingURL=DiscoveryAPI.js.map