Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: get timestamp lyrics, custom clientName & clientVersion for constructRequest function #50

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
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
31 changes: 26 additions & 5 deletions src/YTMusic.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios, { AxiosInstance } from "axios"
import { Cookie, CookieJar } from "tough-cookie"

import { FE_MUSIC_HOME } from "./constants"
import { FE_MUSIC_HOME, ANDROID_CLIENTNAME, ANDROID_CLIENTVERSION } from "./constants"
import AlbumParser from "./parsers/AlbumParser"
import ArtistParser from "./parsers/ArtistParser"
import Parser from "./parsers/Parser"
Expand All @@ -20,6 +20,7 @@ import {
SearchResult,
SongDetailed,
SongFull,
TimedLyricsRes,
UpNextsDetails,
VideoDetailed,
VideoFull,
Expand Down Expand Up @@ -139,6 +140,10 @@ export default class YTMusic {
endpoint: string,
body: Record<string, any> = {},
query: Record<string, string> = {},
options?: {
clientName?: string;
clientVersion?: string;
}
) {
if (!this.config) {
throw new Error("API not initialized. Make sure to call the initialize() method first")
Expand Down Expand Up @@ -169,8 +174,8 @@ export default class YTMusic {
context: {
capabilities: {},
client: {
clientName: this.config.INNERTUBE_CLIENT_NAME,
clientVersion: this.config.INNERTUBE_CLIENT_VERSION,
clientName: options?.clientName || this.config.INNERTUBE_CLIENT_NAME,
clientVersion: options?.clientVersion || this.config.INNERTUBE_CLIENT_VERSION,
experimentIds: [],
experimentsToken: "",
gl: this.config.GL,
Expand Down Expand Up @@ -396,14 +401,30 @@ export default class YTMusic {
* Get lyrics of a specific Song
*
* @param videoId Video ID
* @param timestamp isTimestampLyrics
* @returns Lyrics
*/
public async getLyrics(videoId: string) {
public async getLyrics(videoId: string): Promise<string[] | null>;
public async getLyrics(videoId: string, timestamp: boolean): Promise<TimedLyricsRes | null>;
public async getLyrics(
videoId: string,
timestamp?: boolean
): Promise<string[] | TimedLyricsRes | null> {
if (!videoId.match(/^[a-zA-Z0-9-_]{11}$/)) throw new Error("Invalid videoId")
const data = await this.constructRequest("next", { videoId })
const browseId = traverse(traverseList(data, "tabs", "tabRenderer")[1], "browseId")

const lyricsData = await this.constructRequest("browse", { browseId })
if ( timestamp ) {
const lyricsData = await this.constructRequest("browse", { browseId }, undefined, { clientName: ANDROID_CLIENTNAME, clientVersion: ANDROID_CLIENTVERSION });
const timedLyrics = traverse(lyricsData, "contents", "type", "lyricsData")
if ( !timedLyrics || !timedLyrics.timedLyricsData || !timedLyrics.sourceMessage ) return null;
return {
timedLyricsData: timedLyrics.timedLyricsData,
sourceMessage: timedLyrics.sourceMessage,
} as TimedLyricsRes;
}

const lyricsData = await this.constructRequest("browse", { browseId });
const lyrics = traverseString(lyricsData, "description", "runs", "text")

return lyrics
Expand Down
3 changes: 3 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ export enum PageType {
}

export const FE_MUSIC_HOME = "FEmusic_home"

export const ANDROID_CLIENTNAME = "ANDROID_MUSIC"
export const ANDROID_CLIENTVERSION = "8.05.50"
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export type {
VideoDetailed,
VideoFull,
HomeSection,
TimedLyricsData,
TimedLyricsRes
} from "./types"

export default YTMusic
20 changes: 20 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,26 @@ export const SearchResult = z.discriminatedUnion("type", [
PlaylistDetailed,
])

export type TimedLyricsData = z.infer<typeof TimedLyricsData>
export const TimedLyricsData = z.object({
lyricLine: z.string(),
cueRange: z.object({
startTimeMilliseconds: z.string(),
endTimeMilliseconds: z.string(),
metadata: z.object({
id: z.number()
})
})
})
.strict()

export type TimedLyricsRes = z.infer<typeof TimedLyricsRes>
export const TimedLyricsRes = z.object({
timedLyricsData: z.array(TimedLyricsData),
sourceMessage: z.string()
})
.strict()

export type HomeSection = z.infer<typeof HomeSection>
export const HomeSection = z
.object({
Expand Down