refactor: split music item mapping into multiple fns

This commit is contained in:
ThetaDev 2023-07-22 16:36:20 +02:00
parent 1d94d0241b
commit 68926b9ca2
3 changed files with 423 additions and 436 deletions

View file

@ -482,10 +482,35 @@ impl MusicListMapper {
}
}
/// Map a MusicResponseItem (list item or tile)
fn map_item(&mut self, item: MusicResponseItem) -> Result<Option<MusicItemType>, String> {
match item {
// List item
MusicResponseItem::MusicResponsiveListItemRenderer(item) => {
MusicResponseItem::MusicResponsiveListItemRenderer(item) => self.map_list_item(item),
// Tile
MusicResponseItem::MusicTwoRowItemRenderer(item) => self.map_tile(item),
MusicResponseItem::MessageRenderer(_) => Ok(None),
}
}
pub fn map_response(
&mut self,
mut res: MapResult<Vec<MusicResponseItem>>,
) -> Option<MusicItemType> {
let mut etype = None;
self.warnings.append(&mut res.warnings);
res.c.into_iter().for_each(|item| {
if let Some(et) = self.add_response_item(item) {
if etype.is_none() {
etype = Some(et);
}
}
});
etype
}
/// Map a ListMusicItem (album/playlist tile)
fn map_list_item(&mut self, item: ListMusicItem) -> Result<Option<MusicItemType>, String> {
let mut columns = item.flex_columns.into_iter();
let c1 = columns.next();
let c2 = columns.next();
@ -525,9 +550,7 @@ impl MusicListMapper {
(
MusicPageType::Track {
is_video: self.album.is_none()
&& !first_tn
.map(|tn| tn.height == tn.width)
.unwrap_or_default(),
&& !first_tn.map(|tn| tn.height == tn.width).unwrap_or_default(),
},
d.video_id,
)
@ -549,11 +572,9 @@ impl MusicListMapper {
match pt_id {
// Track
Some((MusicPageType::Track { is_video }, id)) => {
let title =
title.ok_or_else(|| format!("track {id}: could not get title"))?;
let title = title.ok_or_else(|| format!("track {id}: could not get title"))?;
let (artists_p, album_p, duration_p) = match item.flex_column_display_style
{
let (artists_p, album_p, duration_p) = match item.flex_column_display_style {
// Search result
FlexColumnDisplayStyle::TwoLines => {
// Is this a related track?
@ -565,9 +586,7 @@ impl MusicListMapper {
)
} else {
let mut subtitle_parts = c2
.ok_or_else(|| {
format!("track {id}: could not get subtitle")
})?
.ok_or_else(|| format!("track {id}: could not get subtitle"))?
.renderer
.text
.split(util::DOT_SEPARATOR)
@ -607,8 +626,7 @@ impl MusicListMapper {
),
};
let duration =
duration_p.and_then(|p| util::parse_video_length(p.first_str()));
let duration = duration_p.and_then(|p| util::parse_video_length(p.first_str()));
let (album, view_count) = match (item.flex_column_display_style, is_video) {
// The album field contains the view count for search videos
@ -623,9 +641,8 @@ impl MusicListMapper {
}),
),
(_, false) => (
album_p.and_then(|p| {
p.0.into_iter().find_map(|c| AlbumId::try_from(c).ok())
}),
album_p
.and_then(|p| p.0.into_iter().find_map(|c| AlbumId::try_from(c).ok())),
None,
),
(FlexColumnDisplayStyle::Default, true) => (None, None),
@ -683,8 +700,7 @@ impl MusicListMapper {
.split(util::DOT_SEPARATOR)
.into_iter();
let title =
title.ok_or_else(|| format!("track {id}: could not get title"))?;
let title = title.ok_or_else(|| format!("track {id}: could not get title"))?;
let subtitle_p1 = subtitle_parts.next();
let subtitle_p2 = subtitle_parts.next();
@ -717,8 +733,8 @@ impl MusicListMapper {
let artist_id = map_artist_id_fallback(item.menu, artists.first());
let year = subtitle_p3
.and_then(|st| util::parse_numeric(st.first_str()).ok());
let year =
subtitle_p3.and_then(|st| util::parse_numeric(st.first_str()).ok());
self.items.push(MusicItem::Album(AlbumItem {
id,
@ -782,8 +798,9 @@ impl MusicListMapper {
}
}
}
// Tile
MusicResponseItem::MusicTwoRowItemRenderer(item) => {
/// Map a CoverMusicItem (album/playlist tile)
fn map_tile(&mut self, item: CoverMusicItem) -> Result<Option<MusicItemType>, String> {
let mut subtitle_parts = item.subtitle.split(util::DOT_SEPARATOR).into_iter();
let subtitle_p1 = subtitle_parts.next();
let subtitle_p2 = subtitle_parts.next();
@ -845,28 +862,23 @@ impl MusicListMapper {
// "Album", "2022" (Artist albums)
(Some(atype_txt), Some(year_txt), Some(artists), true) => {
year = util::parse_numeric(year_txt.first_str()).ok();
album_type =
map_album_type(atype_txt.first_str(), self.lang);
album_type = map_album_type(atype_txt.first_str(), self.lang);
artists.clone()
}
// Album on artist page with unknown year
(None, None, Some(artists), true) => artists.clone(),
// "Album", <"Oonagh"> (Album variants, new releases)
(Some(atype_txt), Some(p2), _, false) => {
album_type =
map_album_type(atype_txt.first_str(), self.lang);
album_type = map_album_type(atype_txt.first_str(), self.lang);
map_artists(Some(p2))
}
// "Album" (Album variants, no artist)
(Some(atype_txt), None, _, false) => {
album_type =
map_album_type(atype_txt.first_str(), self.lang);
album_type = map_album_type(atype_txt.first_str(), self.lang);
(Vec::new(), true)
}
_ => {
return Err(format!(
"could not parse subtitle of album {id}"
));
return Err(format!("could not parse subtitle of album {id}"));
}
};
@ -889,9 +901,8 @@ impl MusicListMapper {
.as_ref()
.and_then(|p| p.0.first())
.map_or(true, util::is_ytm);
let channel = subtitle_p2.and_then(|p| {
p.0.into_iter().find_map(|c| ChannelId::try_from(c).ok())
});
let channel = subtitle_p2
.and_then(|p| p.0.into_iter().find_map(|c| ChannelId::try_from(c).ok()));
self.items.push(MusicItem::Playlist(MusicPlaylistItem {
id,
@ -912,26 +923,8 @@ impl MusicListMapper {
None => Err("could not determine item type".to_owned()),
}
}
MusicResponseItem::MessageRenderer(_) => Ok(None),
}
}
pub fn map_response(
&mut self,
mut res: MapResult<Vec<MusicResponseItem>>,
) -> Option<MusicItemType> {
let mut etype = None;
self.warnings.append(&mut res.warnings);
res.c.into_iter().for_each(|item| {
if let Some(et) = self.add_response_item(item) {
if etype.is_none() {
etype = Some(et);
}
}
});
etype
}
/// Map a MusicCardShelf (used for the top search result)
pub fn map_card(&mut self, card: MusicCardShelf) -> Option<MusicItemType> {
/*
"Artist" "" "<subscriber count>"

View file

@ -297,7 +297,7 @@ impl<'de> DeserializeAs<'de, TextComponents> for AttributedText {
}
impl TryFrom<TextComponent> for crate::model::ChannelId {
type Error = util::MappingError;
type Error = ();
fn try_from(value: TextComponent) -> Result<Self, Self::Error> {
match value {
@ -310,9 +310,9 @@ impl TryFrom<TextComponent> for crate::model::ChannelId {
id: browse_id,
name: text,
}),
_ => Err(util::MappingError("invalid channel link type".into())),
_ => Err(()),
},
_ => Err(util::MappingError("invalid channel link".into())),
_ => Err(()),
}
}
}

View file

@ -8,7 +8,6 @@ pub use date::{now_sec, shift_months, shift_years};
pub use protobuf::{string_from_pb, ProtoBuilder};
use std::{
borrow::{Borrow, Cow},
collections::BTreeMap,
str::{FromStr, SplitWhitespace},
};
@ -42,11 +41,6 @@ pub const ARTIST_DISCOGRAPHY_PREFIX: &str = "MPAD";
const CONTENT_PLAYBACK_NONCE_ALPHABET: &[u8; 64] =
b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
/// Internal error
#[derive(thiserror::Error, Debug)]
#[error("mapping error: {0}")]
pub struct MappingError(pub(crate) Cow<'static, str>);
/// Return the given capture group that matches first in a list of regexes
pub fn get_cg_from_regexes<'a, I>(mut regexes: I, text: &str, cg: usize) -> Option<String>
where
@ -249,7 +243,7 @@ pub fn sanitize_yt_url(url: &str) -> String {
if parsed_url.query().is_some() {
let params = parsed_url
.query_pairs()
.filter_map(|(k, v)| match k.borrow() {
.filter_map(|(k, v)| match k.as_ref() {
"utm_source" | "utm_medium" | "utm_campaign" | "utm_content" => None,
_ => Some((k.to_string(), v.to_string())),
})