Skip to content

Commit

Permalink
don't cut "subscribers" and "views" text
Browse files Browse the repository at this point in the history
this may play out badly in RTL languages
  • Loading branch information
vixalien committed Jul 14, 2023
1 parent 568b35e commit f173cc9
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 22 deletions.
2 changes: 1 addition & 1 deletion mixins/playlist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export async function get_playlist(

const song_count = header.secondSubtitle.runs
? Number(
header.secondSubtitle.runs[0].text.normalize("NFKD").split(" ")[0],
header.secondSubtitle.runs[0].text.normalize("NFKD"),
)
: null;

Expand Down
2 changes: 1 addition & 1 deletion parsers/albums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function parse_album_header(response: any) {
}

if (header.secondSubtitle.runs.length > 1) {
album.trackCount = Number(header.secondSubtitle.runs[0].text.split(" ")[0]);
album.trackCount = Number(header.secondSubtitle.runs[0].text);
album.duration = header.secondSubtitle.runs[2].text;
} else {
album.duration = header.secondSubtitle.runs[0].text;
Expand Down
25 changes: 10 additions & 15 deletions parsers/browsing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ export function parse_song_flat(data: any) {
id: j(columns[2], TEXT_RUN, NAVIGATION_BROWSE_ID),
};
} else {
song.views = jo(columns[1], `text.runs[-1].text`)?.split(" ")[0];
song.views = jo(columns[1], `text.runs[-1].text`);
}

return song;
Expand Down Expand Up @@ -617,7 +617,7 @@ export function parse_top_video(result: any): TopVideo {
artists: parse_song_artists_runs(runs.slice(0, artists_len)),
playlistId: jo(result, NAVIGATION_PLAYLIST_ID),
thumbnails: j(result, THUMBNAIL_RENDERER),
views: runs[runs.length - 1].text.split(" ")[0],
views: runs[runs.length - 1],
rank: Number(j(rank, TEXT_RUN_TEXT)),
change: jo(rank, "icon.iconType")?.split("_")[2].toLowerCase() || null,
};
Expand All @@ -638,8 +638,7 @@ export function parse_top_artist(result: any): TopArtist {
return {
name: j(get_flex_column_item(result, 0), TEXT_RUN_TEXT),
browseId: j(result, NAVIGATION_BROWSE_ID),
subscribers:
j(get_flex_column_item(result, 1), TEXT_RUN_TEXT)?.split(" ")[0],
subscribers: j(get_flex_column_item(result, 1), TEXT_RUN_TEXT),
thumbnails: j(result, THUMBNAILS),
rank: Number(j(rank, TEXT_RUN_TEXT)),
change: jo(rank, "icon.iconType")?.split("_")[2].toLowerCase() || null,
Expand All @@ -665,8 +664,6 @@ export function parse_trending(result: any): TrendingSong {
const title = get_flex_column_item(result, 0);
const title_run = j(title, TEXT_RUN);

const rank = j(result, "customIndexColumn.musicCustomIndexColumnRenderer");

const last_flex =
get_flex_column_item(result, result.flexColumns.length - 1) ??
get_flex_column_item(result, result.flexColumns.length - 2);
Expand All @@ -680,9 +677,7 @@ export function parse_trending(result: any): TrendingSong {
TEXT_RUNS,
);

const views = last_runs[last_runs.length - 1];

const has_views = views?.text.endsWith(_("views")) ?? false;
const rank = j(result, "customIndexColumn.musicCustomIndexColumnRenderer");

const album_flex = get_flex_column_item(
result,
Expand All @@ -692,18 +687,18 @@ export function parse_trending(result: any): TrendingSong {
return {
title: j(title_run, "text"),
videoId: jo(title_run, NAVIGATION_VIDEO_ID),
artists: parse_song_artists(result, 1, has_views ? -1 : undefined),
artists: parse_song_artists(result, 1, undefined),
playlistId: jo(title_run, NAVIGATION_PLAYLIST_ID),
thumbnails: j(result, THUMBNAILS),
rank: Number(j(rank, TEXT_RUN_TEXT)),
change: jo(rank, "icon.iconType")?.split("_")[2].toLowerCase() || null,
album: (!has_views && album_flex)
album: album_flex
? {
title: j(album_flex, TEXT_RUN_TEXT),
browseId: j(album_flex, TEXT_RUN, NAVIGATION_BROWSE_ID),
}
: null,
views: has_views ? views.text.split(" ")[0] : null,
views: album_flex ? null : last_runs[last_runs.length - 1]?.text ?? null,
};
}

Expand Down Expand Up @@ -731,7 +726,7 @@ export function parse_playlist(data: any) {
playlistId: j(data, TITLE, NAVIGATION_BROWSE_ID).slice(2),
thumbnails: j(data, THUMBNAIL_RENDERER),
songs: has_data && has_songs
? j(subtitles[subtitles.length - 1], "text")?.split(" ")[0]
? j(subtitles[subtitles.length - 1], "text")
: null,
authors: has_data
? parse_song_artists_runs(
Expand All @@ -752,7 +747,7 @@ export function parse_playlist(data: any) {
subtitle.runs.length == 3 &&
j(data, SUBTITLE2).match(/\d+ /)
) {
playlist.count = j(data, SUBTITLE2).split(" ")[0];
playlist.count = j(data, SUBTITLE2);
// TODO: why are we getting "author" 2 times?
playlist.author = parse_song_artists_runs(subtitle.runs.slice(0, 1));
}
Expand All @@ -770,7 +765,7 @@ export interface RelatedArtist {
}

export function parse_related_artist(data: any): RelatedArtist {
const subscribers = jo(data, SUBTITLE2)?.split(" ")[0];
const subscribers = jo(data, SUBTITLE2);

return {
type: "artist",
Expand Down
8 changes: 4 additions & 4 deletions parsers/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ export function parse_search_artist(result: any): SearchArtist {
return {
type: "artist",
name: j(title, "text"),
subscribers: (flex1 && flex1.text.runs[2]?.text.split(" ")[0]) ?? null,
subscribers: (flex1 && flex1.text.runs[2]?.text) ?? null,
browseId: j(result, NAVIGATION_BROWSE_ID),
thumbnails: j(result, THUMBNAILS),
...get_menu_playlists(result),
Expand All @@ -267,7 +267,7 @@ export function parse_search_profile(result: any): SearchProfile {
return {
type: "profile",
name: j(title, "text"),
username: (flex1 && flex1.text.runs[2]?.text.split(" ")[0]) ?? null,
username: (flex1 && flex1.text.runs[2]?.text) ?? null,
browseId: jo(result, NAVIGATION_BROWSE_ID),
thumbnails: j(result, THUMBNAILS),
};
Expand Down Expand Up @@ -298,7 +298,7 @@ export function parse_search_playlist(
return {
type: "playlist",
title: j(title, "text"),
songs: flex1.text.runs[2]?.text.split(" ")[0] ?? null,
songs: flex1.text.runs[2]?.text[0] ?? null,
authors,
browseId: j(result, NAVIGATION_BROWSE_ID),
thumbnails: j(result, THUMBNAILS),
Expand Down Expand Up @@ -488,7 +488,7 @@ export interface TopResultArtist extends SearchArtist {
}

export function parse_top_result_artist(result: any): TopResultArtist {
const subscribers = jo(result, SUBTITLE2)?.split(" ")[0];
const subscribers = jo(result, SUBTITLE2);

const buttons = j(result, "buttons").map((button: any) =>
button.buttonRenderer
Expand Down
2 changes: 1 addition & 1 deletion parsers/songs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export function parse_song_runs(runs: any[], slice_start = 0) {
} else {
// note: YT uses non-breaking space \xa0 to separate number and magnitude
if (text.match(/\d([^ ])* [^ ]*$/) && Number(i) > 0) {
parsed.views = text.split(" ")[0];
parsed.views = text;
} else if (text.match(/^(\d+:)*\d+:\d+$/)) {
parsed.duration = text;
parsed.duration_seconds = parse_duration(text);
Expand Down

0 comments on commit f173cc9

Please sign in to comment.