UNPKG

bandcamp-fetch

Version:
149 lines 6.88 kB
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 _a, _DiscoveryAPI_isContinuation, _LimiterDiscoveryAPI_limiter; import BaseAPIWithImageSupport from '../common/BaseAPIWithImageSupport.js'; import { CacheDataType } from '../utils/Cache.js'; import { URLS } from '../utils/Constants.js'; import { FetchMethod } from '../utils/Fetcher.js'; import DiscoverOptionsParser from './DiscoverOptionsParser.js'; import DiscoverResultParser from './DiscoverResultParser.js'; class DiscoveryAPI extends BaseAPIWithImageSupport { async getAvailableOptions() { return this.cache.getOrSet(CacheDataType.Constants, 'discoverOptions', async () => { const html = await this.fetch(URLS.DISCOVER.SITE); return DiscoverOptionsParser.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(URLS.DISCOVER.API, true, FetchMethod.POST, payload); const result = DiscoverResultParser.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'); }; export default DiscoveryAPI; export 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)); } } _LimiterDiscoveryAPI_limiter = new WeakMap(); //# sourceMappingURL=DiscoveryAPI.js.map