Skip to content

Commit

Permalink
parse sort options from get_album
Browse files Browse the repository at this point in the history
  • Loading branch information
vixalien committed Nov 1, 2023
1 parent 5404a1b commit b788056
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 21 deletions.
30 changes: 26 additions & 4 deletions continuations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export async function get_continuations(
continuation_type: string,
limit: number | null,
request: (additional_params: Record<string, string>) => Promise<any>,
parse: (data: any) => any[],
parse: (data: any, continuation?: any) => any[],
_ctoken_path = "",
reloadable = false,
) {
Expand Down Expand Up @@ -42,6 +42,28 @@ export async function get_continuations(
return { items, continuation };
}

export async function get_sort_continuations<Return extends any>(
results: any,
continuation_type: string,
request: (additional_params: Record<string, string>) => Promise<any>,
parse: (data: any, continuation?: any) => Return,
) {
const get_params = () => get_reloadable_continuation_params(results);

let params = get_params();

const response = await request(params);

if ("continuationContents" in response) {
results = response.continuationContents[continuation_type];
params = get_params();
} else {
return null;
}

return get_continuation_contents(results, parse);
}

export async function get_validated_continuations(
results: any,
continuation_type: string,
Expand Down Expand Up @@ -119,15 +141,15 @@ function get_continuation_object(ctoken: string) {

export function get_continuation_contents<T extends any = any>(
continuation: any,
parse: (data: any) => T[],
parse: (data: any, continuation?: any) => T,
) {
for (const term of ["contents", "items"]) {
if (term in continuation) {
return parse(continuation[term]);
return parse(continuation[term], continuation);
}
}

return [] as T[];
return [] as T;
}

export async function resend_request_until_valid(
Expand Down
82 changes: 65 additions & 17 deletions mixins/browsing.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import CONSTANTS2 from "../constants-ng.json" assert { type: "json" };

import { get_continuations } from "../continuations.ts";
import { get_continuations, get_sort_continuations } from "../continuations.ts";
import {
CAROUSEL,
CONTENT,
DESCRIPTION,
DESCRIPTION_SHELF,
find_object_by_key,
GRID,
GRID_ITEMS,
MRLIR,
MRLITFC,
MUSIC_SHELF,
Expand Down Expand Up @@ -48,7 +49,12 @@ import {
import { ArtistRun, Format, parse_format } from "../parsers/songs.ts";
import { j, jo, sum_total_duration } from "../util.ts";
import { Thumbnail } from "./playlist.ts";
import { AbortOptions, PaginationOptions } from "./utils.ts";
import {
AbortOptions,
get_sort_options,
PaginationOptions,
SortOptions,
} from "./utils.ts";
import { request_json } from "./_request.ts";

export { is_ranked } from "../parsers/browsing.ts";
Expand Down Expand Up @@ -531,41 +537,83 @@ export async function get_lyrics(
}

export interface ArtistAlbums {
artist: string;
artist: string | null;
title: string;
results: ParsedAlbum[];
sort: SortOptions;
}

export async function get_artist_albums(
channelId: string,
params: string,
options: AbortOptions = {},
options: Omit<PaginationOptions, "limit"> = {},
): Promise<ArtistAlbums> {
const data = {
browseId: channelId,
params,
};

function get_chips(renderer: any) {
const header = j(renderer, "header.musicSideAlignedItemRenderer");

const chips = j(header, "startItems.0.chipCloudRenderer.chips");

const selected_chip = j(
chips
.find((chip: any) => chip.chipCloudChipRenderer.isSelected == true),
"chipCloudChipRenderer",
TEXT_RUN_TEXT,
);

return {
selected_chip: selected_chip as string,
sort_options: get_sort_options(header.endItems),
};
}

if (options.continuation) {
return get_sort_continuations(
options.continuation,
"sectionListContinuation",
(params) => {
return request_json("browse", {
data,
params,
signal: options.signal,
});
},
(contents, continuation) => {
const chips = get_chips(continuation);

return {
artist: null,
title: chips.selected_chip,
results: parse_content_list(
j(contents[0], GRID_ITEMS),
parse_album,
),
sort: chips.sort_options,
};
},
)! as Promise<ArtistAlbums>;
}

const json = await request_json("browse", {
data: {
browseId: channelId,
params,
},
data,
signal: options.signal,
});

const columnTab = j(json, SINGLE_COLUMN_TAB);

const grid = j(columnTab, SECTION_LIST_ITEM, GRID);

const chips = j(
columnTab,
"sectionListRenderer.header.musicSideAlignedItemRenderer.startItems.0.chipCloudRenderer.chips",
);

const selected_chip = chips
.filter((chip: any) => chip.chipCloudChipRenderer.isSelected == true)
.map((chip: any) => j(chip.chipCloudChipRenderer, TEXT_RUN_TEXT))[0];
const chips = get_chips(j(columnTab, "sectionListRenderer"));

return {
artist: j(json, "header.musicHeaderRenderer", TITLE_TEXT),
title: selected_chip,
title: chips.selected_chip,
results: parse_content_list(grid.items, parse_album),
sort: chips.sort_options,
};
}

Expand Down
40 changes: 40 additions & 0 deletions mixins/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { ERROR_CODE, MuseError } from "../errors.ts";
import { TITLE_TEXT } from "../nav.ts";
import { LikeStatus } from "../parsers/songs.ts";
import { get_option } from "../setup.ts";
import { j } from "../util.ts";
export { get_option };

export function prepare_like_endpoint(status: LikeStatus) {
Expand Down Expand Up @@ -128,3 +130,41 @@ export interface PaginationOptions extends AbortOptions {
export interface PaginationAndOrderOptions extends PaginationOptions {
order?: Order;
}

export interface SortOptions {
selected: string;
options: { title: string; continuation: string }[];
}

export function get_sort_options(chips: any): SortOptions {
const sort = j(
chips.find((chip: any) => "musicSortFilterButtonRenderer" in chip),
"musicSortFilterButtonRenderer",
);

const selected = j(sort, TITLE_TEXT);
const options = j(sort, "menu.musicMultiSelectMenuRenderer.options")
.map((option: any) => {
const renderer = j(option, "musicMultiSelectMenuItemRenderer");

return {
title: j(renderer, TITLE_TEXT),
continuation: j(
renderer,
"selectedCommand.commandExecutorCommand.commands",
)
.filter((option: any) =>
option.browseSectionListReloadEndpoint != null
)
.map((option: any) =>
option.browseSectionListReloadEndpoint.continuation
.reloadContinuationData.continuation
)[0],
};
});

return {
selected,
options,
};
}

0 comments on commit b788056

Please sign in to comment.