nick42d / youtui

TUI and API for YouTube Music written in Rust
MIT License
46 stars 4 forks source link

ytmapi-rs: Wrong value in album, duration if song has multiple artists (with my crappy solution) #171

Closed lebenoa closed 1 month ago

lebenoa commented 1 month ago

Hello. I'm apologizing in advance for my bad English since I'm not a native speaker.

During the development I saw that SearchResultSong.duration return an artist or album name instead of actual duration and this is only happening to song with multiple artists (Example: Sleeping In The Cold Below by Keith Power & Alan Doyle AND All Falls Down by Alan Walker, Noah Cyrus & Digital Farm Animals)

Solution

ytmapi-rs > src > parse > parse_song_search_result_from_music_shelf_contents


// TODO: Type safety
// TODO: Tests
fn parse_song_search_result_from_music_shelf_contents(
music_shelf_contents: JsonCrawlerBorrowed<'_>,
) -> Result<SearchResultSong> {
let mut mrlir = music_shelf_contents.navigate_pointer("/musicResponsiveListItemRenderer")?;
let title = parse_flex_column_item(&mut mrlir, 0, 0)?;
let mut artist: String = parse_flex_column_item(&mut mrlir, 1, 0)?;

let mut run_idx = 1;
loop {
    let cont: String = parse_flex_column_item(&mut mrlir, 1, run_idx)?;
    run_idx += 1;

    // Since next section seperator is always " • "
    // We can check if it's not equal to that instead of ", " and " & "
    if cont != " • " {
        artist.push_str(&cont);
        artist.push_str(&parse_flex_column_item::<String>(&mut mrlir, 1, run_idx)?);

        run_idx += 1;
    } else {
        break;
    }
}

let album = parse_flex_column_item(&mut mrlir, 1, run_idx)?;
let duration = parse_flex_column_item(&mut mrlir, 1, run_idx + 2)?;
let plays = parse_flex_column_item(&mut mrlir, 2, 0)?;
let explicit = if mrlir.path_exists(BADGE_LABEL) {
    Explicit::IsExplicit
} else {
    Explicit::NotExplicit
};
let video_id = mrlir.take_value_pointer(PLAYLIST_ITEM_VIDEO_ID)?;
let thumbnails: Vec<Thumbnail> = mrlir.take_value_pointer(THUMBNAILS)?;
Ok(SearchResultSong {
    artist,
    thumbnails,
    title,
    explicit,
    plays,
    album,
    video_id,
    duration,
})

}



### PSA: I'm happy to create a pull request if you are happy with my solution but since I'm a bad programmer I decided to create an issue instead
nick42d commented 1 month ago

G'day there,

Thanks for the report - I'm glad to see somebody using this library! I can replicate this locally, it's a faulty assumption from my behalf.

Let me see what I can do, there are a couple of things I'd like to investigate before committing to a solution, ie can this be done declaratively and have I made this same mistake elsewhere.

UPDATE: Seems to affect albums search as well. everything else seems OK.

lebenoa commented 1 month ago

Yes. Thank you for your hard work. Have a good day.

nick42d commented 1 month ago

I have resolved this in #171 and will merge and release as a new version shortly. I couldn't find a better solution than yours, so that's what I've gone with, the only change is to abstract it out to a function. Well done tracking this down and finding the solution :).