mirror of
https://github.com/LuanRT/YouTube.js.git
synced 2026-06-17 19:42:14 +00:00
* refactor: Move transcript logic to `MediaInfo` + Add support for retrieving different languages. * docs: Update and add examples
55 lines
1.7 KiB
TypeScript
55 lines
1.7 KiB
TypeScript
import type Actions from '../../core/Actions.js';
|
|
import { type ApiResponse } from '../../core/Actions.js';
|
|
import type { IGetTranscriptResponse } from '../index.js';
|
|
import Parser from '../index.js';
|
|
import Transcript from '../classes/Transcript.js';
|
|
|
|
export default class TranscriptInfo {
|
|
#page: IGetTranscriptResponse;
|
|
#actions: Actions;
|
|
transcript: Transcript;
|
|
|
|
constructor(actions: Actions, response: ApiResponse) {
|
|
this.#page = Parser.parseResponse(response.data);
|
|
this.#actions = actions;
|
|
this.transcript = this.#page.actions_memo.getType(Transcript).first();
|
|
}
|
|
|
|
/**
|
|
* Selects a language from the language menu and returns the updated transcript.
|
|
* @param language - Language to select.
|
|
*/
|
|
async selectLanguage(language: string): Promise<TranscriptInfo> {
|
|
const target_menu_item = this.transcript.content?.footer?.language_menu?.sub_menu_items?.find((item) => item.title.toString() === language);
|
|
|
|
if (!target_menu_item)
|
|
throw new Error(`Language not found: ${language}`);
|
|
|
|
if (target_menu_item.selected)
|
|
return this;
|
|
|
|
const response = await this.#actions.execute('/get_transcript', {
|
|
params: target_menu_item.continuation
|
|
});
|
|
|
|
return new TranscriptInfo(this.#actions, response);
|
|
}
|
|
|
|
/**
|
|
* Returns available languages.
|
|
*/
|
|
get languages(): string[] {
|
|
return this.transcript.content?.footer?.language_menu?.sub_menu_items?.map((item) => item.title.toString()) || [];
|
|
}
|
|
|
|
/**
|
|
* Returns the currently selected language.
|
|
*/
|
|
get selectedLanguage(): string {
|
|
return this.transcript.content?.footer?.language_menu?.sub_menu_items?.find((item) => item.selected)?.title.toString() || '';
|
|
}
|
|
|
|
get page() {
|
|
return this.#page;
|
|
}
|
|
} |