refactor: simplify channel mapping

This commit is contained in:
ThetaDev 2022-10-29 07:43:10 +02:00
parent 082d1c6c92
commit b64aabb6b6

View file

@ -136,10 +136,6 @@ impl MapResponse<Channel<Paginator<VideoItem>>> for response::Channel {
_deobf: Option<&crate::deobfuscate::Deobfuscator>, _deobf: Option<&crate::deobfuscate::Deobfuscator>,
) -> Result<MapResult<Channel<Paginator<VideoItem>>>, ExtractionError> { ) -> Result<MapResult<Channel<Paginator<VideoItem>>>, ExtractionError> {
let content = map_channel_content(self.contents, self.alerts)?; let content = map_channel_content(self.contents, self.alerts)?;
let grid = match content.content {
response::channel::ChannelContent::GridRenderer { items } => Some(items),
_ => None,
};
let channel_data = map_channel( let channel_data = map_channel(
MapChannelData { MapChannelData {
@ -154,37 +150,22 @@ impl MapResponse<Channel<Paginator<VideoItem>>> for response::Channel {
lang, lang,
)?; )?;
let v_res = grid let v_res = match content.content {
.map(|g| { ChannelContent::GridRenderer { items } => {
let mut mapper = let mut mapper =
response::YouTubeListMapper::<VideoItem>::with_channel(lang, &channel_data); response::YouTubeListMapper::<VideoItem>::with_channel(lang, &channel_data);
mapper.map_response(g); mapper.map_response(items);
MapResult { MapResult {
c: Paginator::new(None, mapper.items, mapper.ctoken), c: Paginator::new(None, mapper.items, mapper.ctoken),
warnings: mapper.warnings, warnings: mapper.warnings,
} }
}) }
.unwrap_or_default(); _ => MapResult::default(),
};
Ok(MapResult { Ok(MapResult {
c: Channel { c: combine_channel_data(channel_data, v_res.c),
id: channel_data.id,
name: channel_data.name,
subscriber_count: channel_data.subscriber_count,
avatar: channel_data.avatar,
verification: channel_data.verification,
description: channel_data.description,
tags: channel_data.tags,
vanity_url: channel_data.vanity_url,
banner: channel_data.banner,
mobile_banner: channel_data.mobile_banner,
tv_banner: channel_data.tv_banner,
has_shorts: channel_data.has_shorts,
has_live: channel_data.has_live,
visitor_data: channel_data.visitor_data,
content: v_res.c,
},
warnings: v_res.warnings, warnings: v_res.warnings,
}) })
} }
@ -198,10 +179,6 @@ impl MapResponse<Channel<Paginator<PlaylistItem>>> for response::Channel {
_deobf: Option<&crate::deobfuscate::Deobfuscator>, _deobf: Option<&crate::deobfuscate::Deobfuscator>,
) -> Result<MapResult<Channel<Paginator<PlaylistItem>>>, ExtractionError> { ) -> Result<MapResult<Channel<Paginator<PlaylistItem>>>, ExtractionError> {
let content = map_channel_content(self.contents, self.alerts)?; let content = map_channel_content(self.contents, self.alerts)?;
let grid = match content.content {
response::channel::ChannelContent::GridRenderer { items } => Some(items),
_ => None,
};
let channel_data = map_channel( let channel_data = map_channel(
MapChannelData { MapChannelData {
@ -216,37 +193,22 @@ impl MapResponse<Channel<Paginator<PlaylistItem>>> for response::Channel {
lang, lang,
)?; )?;
let p_res = grid let p_res = match content.content {
.map(|g| { ChannelContent::GridRenderer { items } => {
let mut mapper = let mut mapper =
response::YouTubeListMapper::<PlaylistItem>::with_channel(lang, &channel_data); response::YouTubeListMapper::<PlaylistItem>::with_channel(lang, &channel_data);
mapper.map_response(g); mapper.map_response(items);
MapResult { MapResult {
c: Paginator::new(None, mapper.items, mapper.ctoken), c: Paginator::new(None, mapper.items, mapper.ctoken),
warnings: mapper.warnings, warnings: mapper.warnings,
} }
}) }
.unwrap_or_default(); _ => MapResult::default(),
};
Ok(MapResult { Ok(MapResult {
c: Channel { c: combine_channel_data(channel_data, p_res.c),
id: channel_data.id,
name: channel_data.name,
subscriber_count: channel_data.subscriber_count,
avatar: channel_data.avatar,
verification: channel_data.verification,
description: channel_data.description,
tags: channel_data.tags,
vanity_url: channel_data.vanity_url,
banner: channel_data.banner,
mobile_banner: channel_data.mobile_banner,
tv_banner: channel_data.tv_banner,
has_shorts: channel_data.has_shorts,
has_live: channel_data.has_live,
visitor_data: channel_data.visitor_data,
content: p_res.c,
},
warnings: p_res.warnings, warnings: p_res.warnings,
}) })
} }
@ -261,10 +223,6 @@ impl MapResponse<Channel<ChannelInfo>> for response::Channel {
) -> Result<MapResult<Channel<ChannelInfo>>, ExtractionError> { ) -> Result<MapResult<Channel<ChannelInfo>>, ExtractionError> {
let content = map_channel_content(self.contents, self.alerts)?; let content = map_channel_content(self.contents, self.alerts)?;
let mut warnings = Vec::new(); let mut warnings = Vec::new();
let meta = match content.content {
response::channel::ChannelContent::ChannelAboutFullMetadataRenderer(meta) => Some(meta),
_ => None,
};
let channel_data = map_channel( let channel_data = map_channel(
MapChannelData { MapChannelData {
@ -279,54 +237,41 @@ impl MapResponse<Channel<ChannelInfo>> for response::Channel {
lang, lang,
)?; )?;
let cinfo = meta let cinfo = match content.content {
.map(|meta| ChannelInfo { response::channel::ChannelContent::ChannelAboutFullMetadataRenderer(meta) => {
create_date: timeago::parse_textual_date_or_warn( ChannelInfo {
lang, create_date: timeago::parse_textual_date_or_warn(
&meta.joined_date_text, lang,
&mut warnings, &meta.joined_date_text,
) &mut warnings,
.map(OffsetDateTime::date), )
view_count: meta .map(OffsetDateTime::date),
.view_count_text view_count: meta
.and_then(|txt| util::parse_numeric_or_warn(&txt, &mut warnings)), .view_count_text
links: meta .and_then(|txt| util::parse_numeric_or_warn(&txt, &mut warnings)),
.primary_links links: meta
.into_iter() .primary_links
.filter_map(|l| { .into_iter()
l.navigation_endpoint .filter_map(|l| {
.url_endpoint l.navigation_endpoint
.map(|url| (l.title, util::sanitize_yt_url(&url.url))) .url_endpoint
}) .map(|url| (l.title, util::sanitize_yt_url(&url.url)))
.collect(), })
}) .collect(),
.unwrap_or_else(|| { }
}
_ => {
warnings.push("no aboutFullMetadata".to_owned()); warnings.push("no aboutFullMetadata".to_owned());
ChannelInfo { ChannelInfo {
create_date: None, create_date: None,
view_count: None, view_count: None,
links: Vec::new(), links: Vec::new(),
} }
}); }
};
Ok(MapResult { Ok(MapResult {
c: Channel { c: combine_channel_data(channel_data, cinfo),
id: channel_data.id,
name: channel_data.name,
subscriber_count: channel_data.subscriber_count,
avatar: channel_data.avatar,
verification: channel_data.verification,
description: channel_data.description,
tags: channel_data.tags,
vanity_url: channel_data.vanity_url,
banner: channel_data.banner,
mobile_banner: channel_data.mobile_banner,
tv_banner: channel_data.tv_banner,
has_shorts: channel_data.has_shorts,
has_live: channel_data.has_live,
visitor_data: channel_data.visitor_data,
content: cinfo,
},
warnings, warnings,
}) })
} }
@ -536,6 +481,26 @@ fn map_channel_content(
} }
} }
fn combine_channel_data<T>(channel_data: Channel<()>, content: T) -> Channel<T> {
Channel {
id: channel_data.id,
name: channel_data.name,
subscriber_count: channel_data.subscriber_count,
avatar: channel_data.avatar,
verification: channel_data.verification,
description: channel_data.description,
tags: channel_data.tags,
vanity_url: channel_data.vanity_url,
banner: channel_data.banner,
mobile_banner: channel_data.mobile_banner,
tv_banner: channel_data.tv_banner,
has_shorts: channel_data.has_shorts,
has_live: channel_data.has_live,
visitor_data: channel_data.visitor_data,
content,
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::{fs::File, io::BufReader, path::Path}; use std::{fs::File, io::BufReader, path::Path};