epg_strip config added

This commit is contained in:
euzu
2025-04-11 10:04:24 +02:00
parent 43a323a9ed
commit b1285c9bad
6 changed files with 176 additions and 108 deletions
+37 -9
View File
@@ -500,6 +500,35 @@ fn flatten_groups(playlistgroups: Vec<PlaylistGroup>) -> Vec<PlaylistGroup> {
sort_order
}
pub struct EpgIdCache<'a > {
pub channel: HashSet<Cow<'a, str>>,
pub normalized: HashMap<Cow<'a, str>, Option<Cow<'a, str>>>,
pub processed: HashSet<String>,
pub strip: Vec<&'a str>,
}
const TERMS_TO_REMOVE: &[&str] = &["3840p", "uhd", "fhd", "hd", "sd", "4k", "plus", "raw"];
impl<'a> EpgIdCache<'a> {
pub fn new(strip: Option<&'a Vec<String>>) -> Self {
let str_refs: Vec<&'a str> = match strip {
Some(vec) => vec.iter().map(std::string::String::as_str).collect(),
None => TERMS_TO_REMOVE.to_vec(),
};
EpgIdCache {
channel: HashSet::new(),
normalized: HashMap::new(),
processed: HashSet::new(),
strip: str_refs,
}
}
pub fn normalize(&self, name: &str) -> String {
normalize_channel_name(name, &self.strip)
}
}
async fn process_playlist_for_target(client: Arc<reqwest::Client>,
playlists: &mut [FetchedPlaylist<'_>],
target: &ConfigTarget,
@@ -535,29 +564,28 @@ async fn process_playlist_for_target(client: Arc<reqwest::Client>,
// we need to process each input epg.
for mut fp in processed_fetched_playlists {
// collect all epg_channel ids
let mut epg_channel_ids: HashSet<Cow<str>> = HashSet::new();
let mut normalized_epg_channel_ids: HashMap<Cow<str>, Option<Cow<str>>> = HashMap::new();
let mut id_cache = EpgIdCache::new(fp.input.epg_strip.as_ref());
for channel in fp.playlistgroups.iter().flat_map(|g| &g.channels) {
match channel.header.epg_channel_id.as_ref() {
None => {
normalized_epg_channel_ids.insert(Cow::Owned(normalize_channel_name(&channel.header.name)), None);
id_cache.normalized.insert(Cow::Owned(id_cache.normalize(&channel.header.name)), None);
},
Some(epg_id) => {
if epg_id.is_empty() {
normalized_epg_channel_ids.insert(Cow::Owned(normalize_channel_name(&channel.header.name)), None);
id_cache.normalized.insert(Cow::Owned(id_cache.normalize(&channel.header.name)), None);
} else {
epg_channel_ids.insert(Cow::Owned(epg_id.to_string()));
id_cache.channel.insert(Cow::Owned(epg_id.to_string()));
}
},
}
}
// let epg_channel_ids: HashSet<_> = fp.playlistgroups.iter().flat_map(|g| &g.channels)
// .filter_map(|c| c.header.epg_channel_id.as_ref()).map(|a| a.as_str()).collect();
if epg_channel_ids.is_empty() && normalized_epg_channel_ids.is_empty() {
if id_cache.channel.is_empty() && id_cache.normalized.is_empty() {
debug!("channel ids are empty");
} else if let Some(tv_guide) = fp.epg {
debug_if_enabled!("found epg information for {}", &target.name);
if let Some(epg) = tv_guide.filter(&mut epg_channel_ids, &mut normalized_epg_channel_ids) {
if let Some(epg) = tv_guide.filter(&mut id_cache) {
let epg_icons: HashMap<&String, &String> = epg.children.iter()
.filter(|tag| tag.icon.is_some() && tag.get_attribute_value(EPG_ATTRIB_ID).is_some())
.map(|t| (t.get_attribute_value(EPG_ATTRIB_ID).unwrap(), t.icon.as_ref().unwrap())).collect();
@@ -567,8 +595,8 @@ async fn process_playlist_for_target(client: Arc<reqwest::Client>,
.filter(|c| c.header.epg_channel_id.is_none() || c.header.logo.is_empty() || c.header.logo_small.is_empty())
.for_each(|c| {
if c.header.epg_channel_id.as_ref().is_none() {
let normalized = normalize_channel_name(&c.header.name);
if let Some(Some(epg_id)) = normalized_epg_channel_ids.get(&Cow::Borrowed(normalized.as_str())) {
let normalized = id_cache.normalize(&c.header.name);
if let Some(Some(epg_id)) = id_cache.normalized.get(&Cow::Borrowed(normalized.as_str())) {
c.header.epg_channel_id = Some(epg_id.to_string());
}
}