Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 16 additions & 29 deletions scripts/locales/strings.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// This module gets all translated strings for all languages
import LOCALES from "../../locales/locales.json" with { type: "json" };
import { MuseError } from "../../mod.ts";

import { request_json } from "../../src/mixins/_request.ts";
import { set_option } from "../../src/mod.ts";
import { ERROR_CODE, set_option } from "../../src/mod.ts";
import { setup } from "../../src/setup.ts";
import { DenoFileStore } from "../../src/store.ts";
import { jo, jom } from "../../src/util.ts";
import { jm, jo } from "../../src/util.ts";
import { cache_fetch } from "../../src/util/cache-fetch.ts";

setup({
Expand Down Expand Up @@ -154,15 +155,6 @@ async function get_uri_response(
return null;
}

export interface PathDeclaration {
path: string;
value: string;
parent: any;
parentProperty: string;
hasArrExpr: boolean;
pointer: string;
}

export interface FetchMapItem {
uri: string;
paths: string[];
Expand All @@ -172,14 +164,14 @@ export const fetch_map: FetchMapItem[] = [
{
uri: "browse:FEmusic_explore",
paths: [
"json.contents.singleColumnBrowseResultsRenderer.tabs.0.tabRenderer.content.sectionListRenderer.contents[1:].musicCarouselShelfRenderer.header.musicCarouselShelfBasicHeaderRenderer.title.runs[0].text",
"json.contents.singleColumnBrowseResultsRenderer.tabs.0.tabRenderer.content.sectionListRenderer.contents[1:].musicCarouselShelfRenderer.header.musicCarouselShelfBasicHeaderRenderer.title.runs.0.text",
],
},
{
// We select the US because they are the ones who can see Genres
uri: "browse:FEmusic_charts?formData.selectedValues=[US]",
paths: [
"json.contents.singleColumnBrowseResultsRenderer.tabs.0.tabRenderer.content.sectionListRenderer.contents.*.musicCarouselShelfRenderer.header.musicCarouselShelfBasicHeaderRenderer.title.runs[0].text",
"json.contents.singleColumnBrowseResultsRenderer.tabs.0.tabRenderer.content.sectionListRenderer.contents.*.musicCarouselShelfRenderer.header.musicCarouselShelfBasicHeaderRenderer.title.runs.0.text",
],
},
{
Expand All @@ -188,8 +180,8 @@ export const fetch_map: FetchMapItem[] = [
// and make sure the item you add is NOT the first item in any category
uri: "browse:UC9vrvNSL3xcWGSkV86REBSg",
paths: [
"json.contents.singleColumnBrowseResultsRenderer.tabs.0.tabRenderer.content.sectionListRenderer.contents.*.musicShelfRenderer.title.runs[0].text",
"json.contents.singleColumnBrowseResultsRenderer.tabs.0.tabRenderer.content.sectionListRenderer.contents.*.musicCarouselShelfRenderer.header.musicCarouselShelfBasicHeaderRenderer.title.runs[0].text",
"json.contents.singleColumnBrowseResultsRenderer.tabs.0.tabRenderer.content.sectionListRenderer.contents.*.musicShelfRenderer.title.runs.0.text",
"json.contents.singleColumnBrowseResultsRenderer.tabs.0.tabRenderer.content.sectionListRenderer.contents.*.musicCarouselShelfRenderer.header.musicCarouselShelfBasicHeaderRenderer.title.runs.0.text",
],
},
{
Expand All @@ -204,13 +196,13 @@ export const fetch_map: FetchMapItem[] = [
uri: "search:get+lucky",
paths: [
"json.contents.tabbedSearchResultsRenderer.tabs.0.tabRenderer.content.sectionListRenderer.header.chipCloudRenderer.chips.*.chipCloudChipRenderer.text.runs.0.text",
"json.contents.tabbedSearchResultsRenderer.tabs.0.tabRenderer.content.sectionListRenderer.contents.*.musicShelfRenderer.contents.0.musicResponsiveListItemRenderer.flexColumns.1.musicResponsiveListItemFlexColumnRenderer.text.runs[0].text",
"json.contents.tabbedSearchResultsRenderer.tabs.0.tabRenderer.content.sectionListRenderer.contents.*.musicShelfRenderer.contents.0.musicResponsiveListItemRenderer.flexColumns.1.musicResponsiveListItemFlexColumnRenderer.text.runs.0.text",
"json.contents.tabbedSearchResultsRenderer.tabs.0.tabRenderer.content.sectionListRenderer.contents.0.musicCardShelfRenderer.subtitle.runs.0.text",
],
},
];

const base_strings = new Map();
const base_strings = new Map<string, string>();

async function get_language_map() {
const map = new Map<string, [uri: string, path: string]>();
Expand All @@ -219,13 +211,18 @@ async function get_language_map() {
const data = await get_uri_response(item.uri);

for (const path of item.paths) {
const items = jom(data, path, "all") as PathDeclaration[] ??
[];
const items = jm(data, path, "all");

english_strings.forEach((string, key) => {
const match = items.find((item) => item.value === string);

if (!match) return;
if (typeof match.value !== "string") {
throw new MuseError(
ERROR_CODE.PARSING_INVALID_JSON,
`Expected string value for key "${key}": ${JSON.stringify(match)}`,
);
}

map.set(key, [item.uri, match.path]);
base_strings.set(key, match.value);
Expand All @@ -238,16 +235,6 @@ async function get_language_map() {

const base_map = await get_language_map();

async function _test_fetch_map_item(item: FetchMapItem) {
const data = await get_uri_response(item.uri);

const items =
item.paths.map((path) => jom(data, path)).flat() as PathDeclaration[] ??
[];

console.log("items", items);
}

// console.log("base map", base_strings);

async function get_strings_for_lang(lang: string) {
Expand Down
43 changes: 24 additions & 19 deletions src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ import { wait } from "./util.ts";
import { Store } from "./store.ts";
import { ERROR_CODE, MuseError } from "./errors.ts";

interface TokenResponseFromLoginCode {
access_token: string;
refresh_token: string;
token_type: string;
expires_in: number;
scope: string;
}

type TokenResponseFromRefreshToken = Omit<
TokenResponseFromLoginCode,
"refresh_token"
>;

export interface Token {
access_token: string;
refresh_token: string;
Expand Down Expand Up @@ -95,10 +108,10 @@ export class Authenticator extends EventTarget {
let fn: () => Promise<void> = () => Promise.resolve();
this.dispatchEvent(
new CustomEvent("requires-login", {
detail: (_fn: () => Promise<void>) => {
detail: (_fn) => {
fn = _fn;
},
}) as RequiresLoginEvent,
}) satisfies RequiresLoginEvent,
);

await fn();
Expand Down Expand Up @@ -130,13 +143,11 @@ export class Authenticator extends EventTarget {
throw new MuseError(
ERROR_CODE.AUTH_CANT_GET_LOGIN_CODE,
"Can't get login code",
{
cause: text,
},
{ cause: text },
);
}

const data = response.json() as Promise<LoginCode>;
const data = await response.json() as LoginCode;

return data;
}
Expand All @@ -159,25 +170,19 @@ export class Authenticator extends EventTarget {
): Promise<Token>;

async load_token_with_code(...args: any[]): Promise<Token> {
let res: Token | null = null;
let res: TokenResponseFromLoginCode | null = null;
let tries = 0;

let code: string, interval: number, signal: AbortSignal | undefined;

if (typeof args[0] === "string") {
code = args[0];
interval = args[1] ?? 5;
signal = args[2];
[code, interval = 5, signal] = args;
} else {
code = args[0].device_code;
interval = args[0].interval;
signal = args[1];
[{ device_code: code, interval }, signal] = args;
}

while (!res || !res.refresh_token) {
if (signal?.aborted) {
throw new DOMException("Aborted", "AbortError");
}
signal?.throwIfAborted();

const response = await this.client.request(
"https://oauth2.googleapis.com/token",
Expand All @@ -193,7 +198,7 @@ export class Authenticator extends EventTarget {
},
);

res = await response.json() as Token;
res = await response.json() as TokenResponseFromLoginCode;

if (!response.ok) await wait(interval * 1000);

Expand Down Expand Up @@ -245,15 +250,15 @@ export class Authenticator extends EventTarget {
);
}

const new_token = await res.json() as Token;
const new_token = await res.json() as TokenResponseFromRefreshToken;

this.token = {
...token,
...new_token,
expires_date: new Date(Date.now() + new_token.expires_in * 1000),
};

return this.token!;
return this.token;
}

return token;
Expand Down
1 change: 0 additions & 1 deletion src/deps.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export { JSONPath, type JSONPathOptions } from "npm:jsonpath-plus@10.1.0";
export { omit } from "npm:lodash-es@4.17.21";
export { basename, extname } from "jsr:@std/path";
2 changes: 1 addition & 1 deletion src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export enum ERROR_CODE {
}

export class MuseError extends Error {
name = "MuseError";
override name = "MuseError";
code: ERROR_CODE;
constructor(code: ERROR_CODE, message: string, options?: ErrorOptions) {
super(message, options);
Expand Down
2 changes: 1 addition & 1 deletion src/mixins/_request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export async function request(endpoint: string, options: FetchData) {
...CONSTANTS2.HEADERS,
...auth_headers,
"Content-Type": "application/json",
"X-Goog-Request-Time": (new Date()).getTime().toString(),
"X-Goog-Request-Time": Date.now().toString(),
...options.headers,
},
params: {
Expand Down
2 changes: 1 addition & 1 deletion src/mixins/browsing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ export async function get_song(
parse_format,
),
expires: new Date(
new Date().getTime() +
Date.now() +
(Number(response.streamingData.expiresInSeconds) * 1000),
),
videoDetails: {
Expand Down
3 changes: 1 addition & 2 deletions src/mixins/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ export function prepare_like_endpoint(status: LikeStatus) {
export function get_timestamp() {
const one_day = 24 * 60 * 60 * 1000;

return Math.round((new Date().getTime() - new Date(0).getTime()) / one_day) -
7;
return Math.round(Date.now() / one_day) - 7;
}

export async function check_auth() {
Expand Down
57 changes: 56 additions & 1 deletion src/nav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,46 @@ interface ObjectWithNested {
[key: number]: any;
}

type ObjectList = Array<{ [key: string]: any } | ObjectWithNested>;
type ObjectList = ReadonlyArray<{ [key: string]: any } | ObjectWithNested>;

export function find_object_by_key<
const K extends string,
T extends { [key in K]?: any },
>(
objectList: ReadonlyArray<T>,
key: K,
nested?: undefined,
isKey?: false,
): T | null;
export function find_object_by_key<
const K extends string,
T extends { [key in K]?: any },
>(
objectList: ReadonlyArray<T>,
key: K,
nested: undefined,
isKey: true,
): T[K] | null;
export function find_object_by_key<
const K extends string,
T extends { [key in K]?: any },
const K2 extends string,
>(
objectList: ReadonlyArray<{ [_key in K2]: T }>,
key: K,
nested: K2,
isKey?: false,
): T | null;
export function find_object_by_key<
const K extends string,
T extends { [key in K]?: any },
const K2 extends string,
>(
objectList: ReadonlyArray<{ [_key in K2]: T }>,
key: K,
nested: K2,
isKey: true,
): T[K] | null;
export function find_object_by_key(
objectList: ObjectList,
key: string,
Expand All @@ -113,6 +151,23 @@ export function find_object_by_key(
return null;
}

export function find_objects_by_key<
const K extends string,
T extends { [_key in K]?: any },
>(
object_list: ReadonlyArray<T>,
key: K,
nested?: undefined,
): T[];
export function find_objects_by_key<
const K extends string,
T extends { [_key in K]?: any },
const K2 extends string,
>(
object_list: ReadonlyArray<{ [_key in K2]: T }>,
key: K,
nested: K2,
): T[];
export function find_objects_by_key(
object_list: any,
key: string,
Expand Down
15 changes: 8 additions & 7 deletions src/parsers/browsing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -828,19 +828,20 @@ export function parse_watch_playlist(data: any): WatchPlaylist {
};
}

type Translatable = keyof typeof STRINGS[keyof typeof STRINGS];
type Language = keyof typeof STRINGS;
type Translatable = keyof typeof STRINGS[Language];

export function _(id: Translatable) {
const result = STRINGS[get_option("language") as keyof typeof STRINGS];
export function _(id: Translatable): string {
const result = STRINGS[get_option("language") as Language];

return result?.[id] ?? id;
}

export function __(value: string) {
export function __(value: string): Translatable | null {
// does the reverse of _()
// it returns the key of the string or null if it doesn't exist

const result = STRINGS[get_option("language") as keyof typeof STRINGS];
const result = STRINGS[get_option("language") as Language];

if (result) {
for (const key in result) {
Expand Down Expand Up @@ -874,8 +875,8 @@ export function parse_two_columns(json: any) {
};
}

export function parse_description_runs(runs: any) {
return runs.map((run: any) => {
export function parse_description_runs(runs: any[]) {
return runs.map((run) => {
return jo(run, "navigationEndpoint.urlEndpoint.url") ?? run.text;
}).join("");
}
Loading