Fix limiter throwing private access errors

This commit is contained in:
patrickkfkan 2023-06-14 04:08:08 +08:00
parent 59a78fd5ed
commit eace49cb14
6 changed files with 76 additions and 40 deletions

View File

@ -22,13 +22,16 @@ export default class AutocompleteAPI {
static getSuggestions(params: AutocompleteAPIGetSuggestionsParams): Promise<AutoCompleteTag[] | AutocompleteLocation[]>;
static getSuggestions(params: AutocompleteAPIGetSuggestionsParams): Promise<any> {
if (params.itemType === AutocompleteItemType.Tag) {
return this.#getAutocompleteTags(params);
return this.getAutocompleteTags(params);
}
return this.#getAutocompleteLocations(params);
return this.getAutocompleteLocations(params);
}
static async #getAutocompleteTags(params: AutocompleteAPIGetSuggestionsParams) {
/**
* @internal
*/
protected static async getAutocompleteTags(params: AutocompleteAPIGetSuggestionsParams) {
const payload = {
search_term: params.query,
count: params.limit || 5
@ -38,7 +41,10 @@ export default class AutocompleteAPI {
return AutocompleteResultsParser.parseTags(json);
}
static async #getAutocompleteLocations(params: AutocompleteAPIGetSuggestionsParams) {
/**
* @internal
*/
protected static async getAutocompleteLocations(params: AutocompleteAPIGetSuggestionsParams) {
const payload = {
q: params.query,
n: params.limit || 5,

View File

@ -46,21 +46,21 @@ export default class BandAPI {
imageFormat: await ImageAPI.getFormat(params.imageFormat, 21)
};
const url = this.#getUrl(params.bandUrl);
const url = this.getUrl(params.bandUrl);
const html = await fetchPage(url);
const result = BandInfoParser.parseInfo(html, opts);
// Return if result is complete
if (this.#isInfoComplete(result)) {
if (this.isInfoComplete(result)) {
return result;
}
// Info lacking name or label (for artist) - try getting them from music page
const musicUrl = this.#getUrl(params.bandUrl, 'music');
const musicUrl = this.getUrl(params.bandUrl, 'music');
const musicHtml = await fetchPage(musicUrl);
const info = BandInfoParser.parseInfo(musicHtml, opts);
this.#fillInfo(result, info);
this.fillInfo(result, info);
// Return if result is complete
if (this.#isInfoComplete(result)) {
if (this.isInfoComplete(result)) {
return result;
}
@ -72,7 +72,7 @@ export default class BandAPI {
if (url) {
const html = await fetchPage(url);
const info = BandInfoParser.parseInfo(html, opts);
this.#fillInfo(result, info);
this.fillInfo(result, info);
}
}
@ -93,7 +93,10 @@ export default class BandAPI {
return LabelArtistsParser.parseLabelArtists(html, opts);
}
static #getUrl(artistOrLabelUrl: string, path?: string, labelId?: string): string {
/**
* @internal
*/
protected static getUrl(artistOrLabelUrl: string, path?: string, labelId?: string): string {
let url = path ? normalizeUrl(path, artistOrLabelUrl) : artistOrLabelUrl;
if (labelId) {
url += `/?label=${encodeURIComponent(labelId)}`;
@ -101,12 +104,18 @@ export default class BandAPI {
return url;
}
static #isInfoComplete(data: Artist | Label) {
/**
* @internal
*/
protected static isInfoComplete(data: Artist | Label) {
return data.name && data.url &&
(data.type === 'label' || data.label);
}
static #fillInfo<T extends Artist | Label>(target: T, src: T): T {
/**
* @internal
*/
protected static fillInfo<T extends Artist | Label>(target: T, src: T): T {
if (target.name === null) {
target.name = src.name;
}

View File

@ -92,12 +92,15 @@ export default class DiscoveryAPI {
delete sanitizedParams.subgenre;
}
const payload = this.#getDiscoverRequestPayload(sanitizedParams);
const payload = this.getDiscoverRequestPayload(sanitizedParams);
const json = await fetchPage(URLS.DISCOVER_URL, true, FetchMethod.GET, payload);
return DiscoverResultParser.parseDiscoverResult(json, opts, resultParams);
}
static #getDiscoverRequestPayload(params: DiscoverParams): DiscoverRequestPayload {
/**
* @internal
*/
protected static getDiscoverRequestPayload(params: DiscoverParams): DiscoverRequestPayload {
const result: DiscoverRequestPayload = {
s: params.sortBy || 'top',
p: params.page || 0

View File

@ -40,7 +40,7 @@ export default class FanAPI {
static async getInfo(params: FanAPIGetInfoParams): Promise<Fan> {
const imageConstants = await ImageAPI.getConstants();
const fanPageUrl = this.#getFanPageUrl(params.username);
const fanPageUrl = this.getFanPageUrl(params.username);
const opts = {
imageBaseUrl: imageConstants.baseUrl,
imageFormat: await ImageAPI.getFormat(params.imageFormat, 20)
@ -50,7 +50,7 @@ export default class FanAPI {
}
static async getCollection(params: FanAPIGetItemsParams) {
return await this.#getItems({
return await this.getItems({
...params,
defaultImageFormat: 9,
continuationUrl: URLS.FAN_CONTINUATION.COLLECTION,
@ -60,7 +60,7 @@ export default class FanAPI {
}
static async getWishlist(params: FanAPIGetItemsParams) {
return await this.#getItems({
return await this.getItems({
...params,
defaultImageFormat: 9,
continuationUrl: URLS.FAN_CONTINUATION.WISHLIST,
@ -70,7 +70,7 @@ export default class FanAPI {
}
static async getFollowingArtistsAndLabels(params: FanAPIGetItemsParams) {
return await this.#getItems({
return await this.getItems({
...params,
defaultImageFormat: 21,
continuationUrl: URLS.FAN_CONTINUATION.FOLLOWING_BANDS,
@ -80,7 +80,7 @@ export default class FanAPI {
}
static async getFollowingGenres(params: FanAPIGetItemsParams) {
return await this.#getItems({
return await this.getItems({
...params,
defaultImageFormat: 3,
continuationUrl: URLS.FAN_CONTINUATION.FOLLOWING_GENRES,
@ -89,7 +89,10 @@ export default class FanAPI {
});
}
static async #getItems<T>(params: FanAPIGetItemsFullParams<T>): Promise<FanPageItemsResult<T> | FanContinuationItemsResult<T>> {
/**
* @internal
*/
protected static async getItems<T>(params: FanAPIGetItemsFullParams<T>): Promise<FanPageItemsResult<T> | FanContinuationItemsResult<T>> {
const { target, imageFormat, defaultImageFormat, continuationUrl } = params;
const imageConstants = await ImageAPI.getConstants();
const opts = {
@ -97,8 +100,8 @@ export default class FanAPI {
imageFormat: await ImageAPI.getFormat(imageFormat, defaultImageFormat)
};
if (!this.#isContinuation(target)) {
const fanPageUrl = this.#getFanPageUrl(target as string);
if (!this.isContinuation(target)) {
const fanPageUrl = this.getFanPageUrl(target as string);
const html = await fetchPage(fanPageUrl);
return params.parsePageFn(html, opts);
}
@ -118,11 +121,17 @@ export default class FanAPI {
return params.parseContinuationFn(json, continuation, opts);
}
static #getFanPageUrl(username: string) {
/**
* @internal
*/
protected static getFanPageUrl(username: string) {
return `${URLS.SITE_URL}/${username}`;
}
static #isContinuation(target: any) {
/**
* @internal
*/
protected static isContinuation(target: any) {
return typeof target === 'object' && target.fanId && target.token;
}
}

View File

@ -25,41 +25,47 @@ export interface SearchAPISearchParams {
export default class SearchAPI {
static async all(params: SearchAPISearchParams) {
return this.#search({ ...params, itemType: SearchItemType.All });
return this.search({ ...params, itemType: SearchItemType.All });
}
static async artistsAndLabels(params: SearchAPISearchParams) {
return this.#search({ ...params, itemType: SearchItemType.ArtistsAndLabels });
return this.search({ ...params, itemType: SearchItemType.ArtistsAndLabels });
}
static async albums(params: SearchAPISearchParams) {
return this.#search({ ...params, itemType: SearchItemType.Albums });
return this.search({ ...params, itemType: SearchItemType.Albums });
}
static async tracks(params: SearchAPISearchParams) {
return this.#search({ ...params, itemType: SearchItemType.Tracks });
return this.search({ ...params, itemType: SearchItemType.Tracks });
}
static async fans(params: SearchAPISearchParams) {
return this.#search({ ...params, itemType: SearchItemType.Fans });
return this.search({ ...params, itemType: SearchItemType.Fans });
}
static async #search(params: SearchAPISearchParams & { itemType: SearchItemType.ArtistsAndLabels }): Promise<SearchResults<SearchResultArtist | SearchResultLabel>>;
static async #search(params: SearchAPISearchParams & { itemType: SearchItemType.Albums }): Promise<SearchResults<SearchResultAlbum>>;
static async #search(params: SearchAPISearchParams & { itemType: SearchItemType.Tracks }): Promise<SearchResults<SearchResultTrack>>;
static async #search(params: SearchAPISearchParams & { itemType: SearchItemType.Fans }): Promise<SearchResults<SearchResultFan>>;
static async #search(params: SearchAPISearchParams & { itemType: SearchItemType.All }): Promise<SearchResults<SearchResultAny>>;
static async #search(params: SearchAPISearchParams & { itemType: SearchItemType }): Promise<any> {
/**
* @internal
*/
protected static async search(params: SearchAPISearchParams & { itemType: SearchItemType.ArtistsAndLabels }): Promise<SearchResults<SearchResultArtist | SearchResultLabel>>;
protected static async search(params: SearchAPISearchParams & { itemType: SearchItemType.Albums }): Promise<SearchResults<SearchResultAlbum>>;
protected static async search(params: SearchAPISearchParams & { itemType: SearchItemType.Tracks }): Promise<SearchResults<SearchResultTrack>>;
protected static async search(params: SearchAPISearchParams & { itemType: SearchItemType.Fans }): Promise<SearchResults<SearchResultFan>>;
protected static async search(params: SearchAPISearchParams & { itemType: SearchItemType.All }): Promise<SearchResults<SearchResultAny>>;
protected static async search(params: SearchAPISearchParams & { itemType: SearchItemType }): Promise<any> {
const opts = {
itemType: params.itemType || SearchItemType.All,
albumImageFormat: await ImageAPI.getFormat(params.albumImageFormat, 9),
artistImageFormat: await ImageAPI.getFormat(params.artistImageFormat, 21)
};
const html = await fetchPage(this.#getSearchUrl(params));
const html = await fetchPage(this.getSearchUrl(params));
return SearchResultsParser.parseResults(html, opts);
}
static #getSearchUrl(params: SearchAPISearchParams & { itemType: SearchItemType }) {
/**
* @internal
*/
protected static getSearchUrl(params: SearchAPISearchParams & { itemType: SearchItemType }) {
const urlObj = new URL(URLS.SEARCH);
urlObj.searchParams.set('q', params.query);
urlObj.searchParams.set('page', (params.page || 1).toString());

View File

@ -47,7 +47,7 @@ export default class TagAPI {
}
static async getReleasesAvailableFilters(tagUrl: string): Promise<ReleasesByTag.Filter[]> {
const filterValueNames = await this.#getReleaseFilterValueNames(tagUrl);
const filterValueNames = await this.getReleaseFilterValueNames(tagUrl);
const html = await fetchPage(tagUrl);
return ReleasesByTagParser.parseFilters(html, filterValueNames);
}
@ -121,7 +121,10 @@ export default class TagAPI {
return ReleasesByTagParser.parseReleases(json, opts);
}
static async #getReleaseFilterValueNames(tagUrl: string) {
/**
* @internal
*/
protected static async getReleaseFilterValueNames(tagUrl: string) {
const url = `${tagUrl}?tab=all_releases`;
const html = await fetchPage(url);
const path = ReleasesByTagParser.parseHubJSPath(html);