bandcamp-fetch
Version:
Scrape Bandcamp content
101 lines • 4.64 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 _LimiterSearchAPI_limiter;
import { URL } from 'url';
import { URLS } from '../utils/Constants.js';
import SearchResultsParser from './SearchResultsParser.js';
import BaseAPIWithImageSupport from '../common/BaseAPIWithImageSupport.js';
export var SearchItemType;
(function (SearchItemType) {
SearchItemType["All"] = "All";
SearchItemType["ArtistsAndLabels"] = "ArtistsAndLabels";
SearchItemType["Albums"] = "Albums";
SearchItemType["Tracks"] = "Tracks";
SearchItemType["Fans"] = "Fans";
})(SearchItemType || (SearchItemType = {}));
export default class SearchAPI extends BaseAPIWithImageSupport {
async all(params) {
return this.search({ ...params, itemType: SearchItemType.All });
}
async artistsAndLabels(params) {
return this.search({
...params,
itemType: SearchItemType.ArtistsAndLabels
});
}
async albums(params) {
return this.search({ ...params, itemType: SearchItemType.Albums });
}
async tracks(params) {
return this.search({ ...params, itemType: SearchItemType.Tracks });
}
async fans(params) {
return this.search({ ...params, itemType: SearchItemType.Fans });
}
async search(params) {
const opts = {
itemType: params.itemType || SearchItemType.All,
albumImageFormat: await this.imageAPI.getFormat(params.albumImageFormat, 9),
artistImageFormat: await this.imageAPI.getFormat(params.artistImageFormat, 21)
};
const html = await this.fetch(SearchAPI.getSearchUrl(params));
return SearchResultsParser.parseResults(html, opts);
}
/**
* @internal
*/
static getSearchUrl(params) {
const urlObj = new URL(URLS.SEARCH);
urlObj.searchParams.set('q', params.query);
urlObj.searchParams.set('page', (params.page || 1).toString());
switch (params.itemType) {
case SearchItemType.ArtistsAndLabels:
urlObj.searchParams.set('item_type', 'b');
break;
case SearchItemType.Albums:
urlObj.searchParams.set('item_type', 'a');
break;
case SearchItemType.Tracks:
urlObj.searchParams.set('item_type', 't');
break;
case SearchItemType.Fans:
urlObj.searchParams.set('item_type', 'f');
break;
default:
}
return urlObj.toString();
}
}
export class LimiterSearchAPI extends SearchAPI {
constructor(params) {
super(params);
_LimiterSearchAPI_limiter.set(this, void 0);
__classPrivateFieldSet(this, _LimiterSearchAPI_limiter, params.limiter, "f");
}
async all(params) {
return __classPrivateFieldGet(this, _LimiterSearchAPI_limiter, "f").schedule(() => super.all(params));
}
async artistsAndLabels(params) {
return __classPrivateFieldGet(this, _LimiterSearchAPI_limiter, "f").schedule(() => super.artistsAndLabels(params));
}
async albums(params) {
return __classPrivateFieldGet(this, _LimiterSearchAPI_limiter, "f").schedule(() => super.albums(params));
}
async tracks(params) {
return __classPrivateFieldGet(this, _LimiterSearchAPI_limiter, "f").schedule(() => super.tracks(params));
}
async fans(params) {
return __classPrivateFieldGet(this, _LimiterSearchAPI_limiter, "f").schedule(() => super.fans(params));
}
}
_LimiterSearchAPI_limiter = new WeakMap();
//# sourceMappingURL=SearchAPI.js.map