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

Handle case where top search result is a 'radio' playlist with only 1 subtitle. #163

Merged
merged 2 commits into from
Sep 4, 2024
Merged
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
28 changes: 22 additions & 6 deletions ytmapi-rs/src/parse/search.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use super::{parse_flex_column_item, ParseFrom, ProcessedResult, DISPLAY_POLICY};
use super::{
parse_flex_column_item, ParseFrom, ProcessedResult, DISPLAY_POLICY, RESPONSIVE_HEADER,
};
use crate::common::{
AlbumID, AlbumType, ChannelID, Explicit, PlaylistID, PodcastID, ProfileID, SearchSuggestion,
SuggestionType, TextRun, Thumbnail, VideoID,
Expand Down Expand Up @@ -74,6 +76,7 @@ enum SearchResultType {
/// Dynamically defined top result.
/// Some fields are optional as they are not defined for all result types.
// In future, may be possible to make this type safe.
// TODO: Add endpoint id.
pub struct TopResult {
pub result_name: String,
/// Both Videos and Songs can have this left out.
Expand All @@ -87,7 +90,8 @@ pub struct TopResult {
pub plays: Option<String>,
/// Podcast publisher.
pub publisher: Option<String>,
// TODO: Add endpoint id.
/// Generic tagline that can appear on top results
pub byline: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
Expand Down Expand Up @@ -326,17 +330,27 @@ fn parse_basic_search_result_from_section_list_contents(
profiles,
})
}

fn parse_top_results_from_music_card_shelf_contents(
mut music_shelf_contents: JsonCrawlerBorrowed<'_>,
) -> Result<Vec<TopResult>> {
let mut results = Vec::new();
// Begin - first result parsing
let result_name = music_shelf_contents.take_value_pointer(TITLE_TEXT)?;
let result_type = music_shelf_contents
.take_value_pointer::<TopResultType>(SUBTITLE)
.ok();
let subtitle: String = music_shelf_contents.take_value_pointer(SUBTITLE)?;
let subtitle_2: Option<String> = music_shelf_contents.take_value_pointer(SUBTITLE2).ok();
// Deserialize without taking ownership of subtitle - not possible with
// JsonCrawler::take_value_pointer().
// TODO: add methods like borrow_value_pointer() to JsonCrawler.
let result_type_result: std::result::Result<_, serde::de::value::Error> =
TopResultType::deserialize(subtitle.as_str().into_deserializer());
let result_type = result_type_result.ok();
// Possibly artists only.
let subscribers = music_shelf_contents.take_value_pointer(SUBTITLE2)?;
let subscribers = subtitle_2;
let byline = match result_type {
Some(_) => None,
None => Some(subtitle),
};
// Imperative solution, may be able to make more functional.
let publisher = None;
let artist = None;
Expand All @@ -357,6 +371,7 @@ fn parse_top_results_from_music_card_shelf_contents(
duration,
year,
plays,
byline,
};
// End - first result parsing.
results.push(first_result);
Expand Down Expand Up @@ -442,6 +457,7 @@ fn parse_top_result_from_music_shelf_contents(
duration,
year,
plays,
byline: None,
}))
}
// TODO: Type safety
Expand Down
12 changes: 12 additions & 0 deletions ytmapi-rs/src/parse/search/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ async fn test_search_basic_top_result_no_type() {
);
}
#[tokio::test]
async fn test_search_basic_radio() {
// Case where topmost result is a special 'radio' playlist. Doesn't contain a
// type and only has a single subtitle. Seems to show up when searching for
// genres like classical and metal.
parse_test!(
"./test_json/search_basic_radio_20240830.json",
"./test_json/search_basic_radio_20240830_output.txt",
SearchQuery::new(""),
BrowserToken
);
}
#[tokio::test]
async fn test_search_basic_top_result_card() {
// Case where there is only a 'card' top result, with no children.
parse_test!(
Expand Down
1 change: 1 addition & 0 deletions ytmapi-rs/test_json/search_basic_radio_20240830.json

Large diffs are not rendered by default.

Loading