Files
tuliprox/backend/src/processing/processor/playlist.rs
T

957 lines
40 KiB
Rust
Raw Normal View History

use crate::model::{AppConfig, Config, ConfigFavourites, ConfigInput, ConfigRename, TVGuide};
2025-05-04 10:59:08 +02:00
use crate::utils::m3u;
use crate::utils::xtream;
2025-12-30 15:06:16 +01:00
use crate::utils::{epg, StepMeasureCallback};
2023-10-27 19:03:29 +02:00
use std::collections::{HashMap, HashSet};
2025-01-06 09:17:05 +01:00
use std::path::PathBuf;
use std::sync::{Arc, Weak};
use tokio::sync::{Mutex, OwnedRwLockWriteGuard, RwLock};
2025-12-16 09:54:59 +01:00
use tokio::task::JoinSet;
2023-10-19 18:56:08 +02:00
2025-12-16 09:54:59 +01:00
use crate::api::model::{EventManager, EventMessage, PlaylistStorageState, UpdateGuard};
use crate::messaging::send_message_json;
2025-06-24 01:06:25 +02:00
use crate::model::Epg;
2025-06-24 01:06:25 +02:00
use crate::model::FetchedPlaylist;
use crate::model::Mapping;
use crate::model::{ConfigTarget, ProcessTargets};
2025-05-04 10:59:08 +02:00
use crate::model::{InputStats, PlaylistStats, SourceStats, TargetStats};
2026-01-02 16:29:05 +01:00
use crate::processing::input_cache;
use crate::processing::input_cache::ClusterState;
2025-06-24 01:06:25 +02:00
use crate::processing::parser::xmltv::flatten_tvguide;
use crate::processing::playlist_watch::process_group_watch;
2025-06-24 01:06:25 +02:00
use crate::processing::processor::epg::process_playlist_epg;
2025-12-22 17:25:11 +01:00
use crate::processing::processor::library;
2025-06-24 01:06:25 +02:00
use crate::processing::processor::sort::sort_playlist;
2025-06-16 17:53:03 +02:00
use crate::processing::processor::trakt::process_trakt_categories_for_target;
2025-06-24 01:06:25 +02:00
use crate::processing::processor::xtream_series::playlist_resolve_series;
use crate::processing::processor::xtream_vod::playlist_resolve_vod;
2026-01-02 16:29:05 +01:00
use crate::repository::playlist_repository::{load_input_playlist, persist_input_playlist, persist_playlist};
use crate::repository::xtream_repository::CategoryKey;
use crate::repository::{MemoryPlaylistSource, PlaylistSource};
2025-06-24 01:06:25 +02:00
use crate::utils::StepMeasure;
2025-12-16 09:54:59 +01:00
use crate::utils::{debug_if_enabled, trace_if_enabled};
2025-10-28 20:21:36 +01:00
use futures::StreamExt;
2025-12-30 15:06:16 +01:00
use indexmap::IndexMap;
use log::{debug, error, info, log_enabled, warn, Level};
2025-08-13 23:07:21 +02:00
use shared::error::{get_errors_notify_message, notify_err, TuliproxError};
2025-10-20 15:55:12 +02:00
use shared::foundation::filter::{get_field_value, set_field_value, Filter, ValueAccessor, ValueProvider};
2026-01-02 16:29:05 +01:00
use shared::model::xtream_const::XTREAM_CLUSTER;
use shared::model::{CounterModifier, FieldGetAccessor, FieldSetAccessor, InputType, ItemField, MsgKind,
PlaylistGroup, PlaylistItem, PlaylistItemType, PlaylistUpdateState,
ProcessingOrder, UUIDType};
use shared::utils::{create_alias_uuid, default_as_default, StringInterner};
2025-06-24 01:06:25 +02:00
use std::time::Instant;
2025-04-03 18:03:59 +03:00
2025-12-29 15:55:50 +01:00
fn is_valid(pli: &PlaylistItem, filter: &Filter, match_as_ascii: bool) -> bool {
let provider = ValueProvider { pli, match_as_ascii };
2025-10-20 15:55:12 +02:00
filter.filter(&provider)
}
2021-10-15 16:59:20 +02:00
pub fn apply_filter_to_source(source: &mut dyn PlaylistSource, filter: &Filter) -> Option<Vec<PlaylistGroup>> {
let mut groups: IndexMap<Arc<str>, PlaylistGroup> = IndexMap::new();
for pli in source.into_items() {
if is_valid(&pli, filter, false) {
let group_title = pli.header.group.clone();
let cluster = pli.header.xtream_cluster;
let cat_id = pli.header.category_id;
groups.entry(group_title.clone())
.or_insert_with(|| PlaylistGroup {
id: cat_id,
title: group_title,
channels: vec![],
xtream_cluster: cluster,
})
.channels.push(pli);
}
}
if groups.is_empty() { None } else { Some(groups.into_values().collect()) }
}
fn filter_playlist(source: &mut dyn PlaylistSource, target: &ConfigTarget, _interner: &mut StringInterner) -> Option<Vec<PlaylistGroup>> {
apply_filter_to_source(source, &target.filter)
}
2025-10-20 15:55:12 +02:00
pub fn apply_filter_to_playlist(playlist: &mut [PlaylistGroup], filter: &Filter) -> Option<Vec<PlaylistGroup>> {
2025-01-09 13:31:05 +01:00
let mut new_playlist = Vec::with_capacity(128);
2024-05-10 12:01:48 +02:00
for pg in playlist.iter_mut() {
let channels = pg.channels.iter()
2025-12-29 15:55:50 +01:00
.filter(|&pli| is_valid(pli, filter, false)).cloned().collect::<Vec<PlaylistItem>>();
2023-05-03 17:46:39 +02:00
if !channels.is_empty() {
2023-01-13 15:56:03 +01:00
new_playlist.push(PlaylistGroup {
id: pg.id,
2023-01-13 15:56:03 +01:00
title: pg.title.clone(),
channels,
xtream_cluster: pg.xtream_cluster,
2023-01-13 15:56:03 +01:00
});
}
2024-05-10 12:01:48 +02:00
}
if new_playlist.is_empty() { None } else { Some(new_playlist) }
2025-10-20 15:55:12 +02:00
}
2024-05-05 19:15:54 +02:00
2025-05-04 10:59:08 +02:00
fn assign_channel_no_playlist(new_playlist: &mut [PlaylistGroup]) {
2025-04-12 18:20:46 +02:00
let assigned_chnos: HashSet<u32> = new_playlist.iter().flat_map(|g| &g.channels)
2025-12-26 16:03:03 +01:00
.filter(|c| c.header.chno != 0)
2025-12-22 17:25:11 +01:00
.map(|c| c.header.chno)
.collect();
let mut chno = 1;
for group in new_playlist {
2025-03-11 14:56:10 +01:00
for chan in &mut group.channels {
2025-12-22 17:25:11 +01:00
if chan.header.chno == 0 {
2025-04-12 18:20:46 +02:00
while assigned_chnos.contains(&chno) {
chno += 1;
}
2025-12-22 17:25:11 +01:00
chan.header.chno = chno;
2025-04-12 18:20:46 +02:00
chno += 1;
}
}
}
}
fn exec_rename(pli: &mut PlaylistItem, rename: Option<&Vec<ConfigRename>>, interner: &mut StringInterner) {
2023-05-03 17:46:39 +02:00
if let Some(renames) = rename {
if !renames.is_empty() {
let result = pli;
for r in renames {
2025-05-26 13:57:29 +02:00
let value = get_field_value(result, r.field);
let cap = r.pattern.replace_all(&*value, &r.new_name);
if log_enabled!(log::Level::Debug) && value != cap {
2025-12-12 14:35:00 +01:00
trace_if_enabled!("Renamed {}={value} to {cap}", &r.field);
}
2023-05-03 17:46:39 +02:00
let value = cap.into_owned();
set_field_value(result, r.field, value, interner);
2023-01-13 15:56:03 +01:00
}
}
}
}
fn rename_playlist(source: &mut dyn PlaylistSource, target: &ConfigTarget, interner: &mut StringInterner) -> Option<Vec<PlaylistGroup>> {
2023-01-13 15:56:03 +01:00
match &target.rename {
Some(renames) if !renames.is_empty() => {
let mut groups: IndexMap<Arc<str>, PlaylistGroup> = IndexMap::new();
for mut pli in source.into_items() {
// Handle group rename first if it's in the renames
for r in renames {
if matches!(r.field, ItemField::Group) {
let value = &*pli.header.group;
let cap = r.pattern.replace_all(value, &r.new_name);
if *value != cap {
pli.header.group = interner.intern(&cap);
2023-01-12 15:59:40 +01:00
}
}
}
exec_rename(&mut pli, Some(renames), interner);
let group_title = pli.header.group.clone();
let cluster = pli.header.xtream_cluster;
let cat_id = pli.header.category_id;
groups.entry(group_title.clone())
.or_insert_with(|| PlaylistGroup {
id: cat_id,
title: group_title,
channels: vec![],
xtream_cluster: cluster,
})
.channels.push(pli);
2023-01-12 15:59:40 +01:00
}
Some(groups.into_values().collect())
2023-01-12 15:59:40 +01:00
}
2023-01-13 15:56:03 +01:00
_ => None
2023-01-12 15:59:40 +01:00
}
}
2025-12-29 15:55:50 +01:00
fn map_channel(mut channel: PlaylistItem, mapping: &Mapping) -> (PlaylistItem, Vec<PlaylistItem>, bool) {
let mut matched = false;
2025-12-29 15:55:50 +01:00
let mut virtual_items = vec![];
2025-05-14 18:23:22 +02:00
if let Some(mapper) = &mapping.mapper {
if !mapper.is_empty() {
let ref_chan = &mut channel;
2025-06-12 16:20:15 +02:00
let templates = mapping.templates.as_ref();
2025-05-14 18:23:22 +02:00
for m in mapper {
2025-05-22 13:34:45 +02:00
if let Some(script) = m.t_script.as_ref() {
if let Some(filter) = &m.t_filter {
2025-12-29 15:55:50 +01:00
let provider = ValueProvider { pli: ref_chan, match_as_ascii: mapping.match_as_ascii };
2025-05-22 13:34:45 +02:00
if filter.filter(&provider) {
matched = true;
2025-12-29 15:55:50 +01:00
let mut accessor = ValueAccessor { pli: ref_chan, virtual_items: vec![], match_as_ascii: mapping.match_as_ascii };
2025-06-12 16:20:15 +02:00
script.eval(&mut accessor, templates);
2025-12-29 15:55:50 +01:00
virtual_items.extend(accessor.virtual_items.into_iter().map(|(_, pli)| pli));
2025-05-14 18:23:22 +02:00
}
}
2022-04-05 12:23:30 +02:00
}
2025-04-10 00:06:48 +02:00
}
2023-01-12 15:59:40 +01:00
}
}
2025-12-29 15:55:50 +01:00
(channel, virtual_items, matched)
}
2025-12-29 16:43:33 +01:00
fn map_channel_and_flatten(channel: PlaylistItem, mapping: &Mapping) -> Vec<PlaylistItem> {
2025-12-29 15:55:50 +01:00
let (mapped_channel, mut virtual_items, _matched) = map_channel(channel, mapping);
let mut result = Vec::with_capacity(1 + virtual_items.len());
result.push(mapped_channel);
result.append(&mut virtual_items);
result
2023-01-12 15:59:40 +01:00
}
fn map_playlist(source: &mut dyn PlaylistSource, target: &ConfigTarget, _interner: &mut StringInterner) -> Option<Vec<PlaylistGroup>> {
let mapping_binding = target.mapping.load();
let mappings = mapping_binding.as_ref()?;
let valid_mappings = mappings.iter().filter(|m| m.mapper.as_ref().is_some_and(|v| !v.is_empty()));
let iter: Box<dyn Iterator<Item=PlaylistItem>> = Box::new(source.into_items());
let mapped_iter = valid_mappings.fold(iter, |iter, mapping| {
Box::new(iter.flat_map(move |chan| map_channel_and_flatten(chan, mapping)))
as Box<dyn Iterator<Item=PlaylistItem>>
});
let mut next_groups: IndexMap<Arc<str>, PlaylistGroup> = IndexMap::new();
let mut grp_id: u32 = 0;
for channel in mapped_iter {
let group_title = channel.header.group.clone();
let cluster = channel.header.xtream_cluster;
next_groups.entry(group_title.clone())
.or_insert_with(|| {
grp_id += 1;
PlaylistGroup {
id: grp_id,
title: group_title,
channels: Vec::new(),
xtream_cluster: cluster,
2023-04-26 14:30:43 +02:00
}
})
.channels.push(channel);
2022-04-05 12:23:30 +02:00
}
Some(next_groups.into_values().collect())
2022-04-05 12:23:30 +02:00
}
2025-03-11 14:56:10 +01:00
fn map_playlist_counter(target: &ConfigTarget, playlist: &mut [PlaylistGroup]) {
2025-06-24 01:06:25 +02:00
if let Some(guard) = &*target.mapping.load() {
let mappings = guard.as_ref();
for mapping in mappings {
2024-09-11 19:41:50 +02:00
if let Some(counter_list) = &mapping.t_counter {
for counter in counter_list {
2025-03-11 14:56:10 +01:00
for plg in &mut *playlist {
for channel in &mut plg.channels {
2025-12-29 15:55:50 +01:00
let provider = ValueProvider { pli: channel, match_as_ascii: mapping.match_as_ascii };
2025-05-22 13:34:45 +02:00
if counter.filter.filter(&provider) {
let cntval = counter.value.fetch_add(1, core::sync::atomic::Ordering::AcqRel);
2025-04-28 08:30:48 +02:00
let padded_cntval = if counter.padding > 0 {
format!("{:0width$}", cntval, width = counter.padding as usize)
} else {
2024-09-11 19:41:50 +02:00
cntval.to_string()
2025-04-28 08:30:48 +02:00
};
let new_value = if counter.modifier == CounterModifier::Assign {
padded_cntval
2024-09-11 19:41:50 +02:00
} else {
2025-03-11 14:56:10 +01:00
let value = channel.header.get_field(&counter.field).map_or_else(String::new, |field_value| field_value.to_string());
2024-09-11 19:41:50 +02:00
if counter.modifier == CounterModifier::Suffix {
2025-04-28 08:30:48 +02:00
format!("{value}{}{padded_cntval}", counter.concat)
2024-10-25 00:39:22 +02:00
} else {
2025-04-28 08:30:48 +02:00
format!("{padded_cntval}{}{value}", counter.concat)
2024-09-11 19:41:50 +02:00
}
};
2025-03-11 14:56:10 +01:00
channel.header.set_field(&counter.field, new_value.as_str());
2024-09-11 19:41:50 +02:00
}
}
}
}
}
}
}
}
2026-01-02 13:18:08 +01:00
// Inputs disabled in the config are always disabled.
// Command-line targets can only restrict enabled inputs, never enable them.
fn is_input_enabled(input: &ConfigInput, user_targets: &ProcessTargets) -> bool {
2026-01-02 13:18:08 +01:00
input.enabled && (!user_targets.enabled || user_targets.has_input(input.id))
}
fn is_target_enabled(target: &ConfigTarget, user_targets: &ProcessTargets) -> bool {
(!user_targets.enabled && target.enabled) || (user_targets.enabled && user_targets.has_target(target.id))
}
async fn playlist_download_from_input(client: &reqwest::Client, app_config: &Arc<AppConfig>, input: &ConfigInput) -> (Vec<PlaylistGroup>, Vec<TuliproxError>, bool, bool) {
2025-12-16 16:23:15 +01:00
let config = &*app_config.config.load();
2026-01-02 16:29:05 +01:00
let working_dir = &config.working_dir;
// Check Status
let storage_path = input_cache::resolve_input_storage_path(working_dir, &input.name);
let mut status = input_cache::load_input_status(&storage_path);
let cache_duration = input.cache_duration_seconds;
// Ensure data directory exists
if !storage_path.exists() {
let _ = std::fs::create_dir_all(&storage_path);
}
let (clusters_to_download, fully_cached) = match input.input_type {
InputType::Xtream => {
let mut to_download = vec![];
for c in XTREAM_CLUSTER {
if !input_cache::is_cache_valid(&status, &c.to_string(), cache_duration) {
to_download.push(c);
}
}
if to_download.is_empty() {
(None, true) // Everything cached
} else {
(Some(to_download), false)
}
}
_ => {
// M3U / Library
if input_cache::is_cache_valid(&status, "default", cache_duration) {
(None, true)
} else {
(None, false) // Download all
}
}
};
if fully_cached {
return (vec![], vec![], true, false);
2026-01-02 16:29:05 +01:00
}
let (playlist, errors, persisted) = match input.input_type {
InputType::M3u => {
let (p, e) = m3u::download_m3u_playlist(client, config, input).await;
(p, e, false)
}
InputType::Xtream => xtream::download_xtream_playlist(app_config, client, input, clusters_to_download.as_deref()).await,
InputType::M3uBatch | InputType::XtreamBatch => (vec![], vec![], false),
InputType::Library => {
let (p, e) = library::download_library_playlist(client, app_config, input).await;
(p, e, false)
}
2026-01-02 16:29:05 +01:00
};
// Update Status
if errors.is_empty() {
if let InputType::Xtream = input.input_type {
if let Some(clusters) = clusters_to_download {
for c in clusters {
input_cache::update_cluster_status(&mut status, &c.to_string(), ClusterState::Ok);
}
} else {
// All clusters logic if None passed (implies all were invalid or first run)
for c in XTREAM_CLUSTER {
input_cache::update_cluster_status(&mut status, &c.to_string(), ClusterState::Ok);
}
}
} else {
input_cache::update_cluster_status(&mut status, "default", ClusterState::Ok);
}
input_cache::save_input_status(&storage_path, &status);
} else {
// Mark failed?
// We could mark specific clusters as failed if we knew which one failed.
// For simplicity, if error, we don't update the timestamp (so it stays expired/invalid).
// Or we mark as Failed.
if let InputType::Xtream = input.input_type {
if let Some(clusters) = clusters_to_download {
for c in clusters {
// Optimistic: Only mark failed if we are sure?
// Currently just don't update the status to OK.
input_cache::update_cluster_status(&mut status, &c.to_string(), ClusterState::Failed);
}
}
input_cache::save_input_status(&storage_path, &status);
}
2025-08-13 13:40:41 +02:00
}
2026-01-02 16:29:05 +01:00
(playlist, errors, false, persisted)
2025-08-13 13:40:41 +02:00
}
async fn process_source(source_idx: usize, ctx: &PlaylistProcessingContext) -> (Vec<InputStats>, Vec<TargetStats>, Vec<TuliproxError>) {
let sources = ctx.config.sources.load();
2023-10-13 13:59:22 +02:00
let mut errors = vec![];
2025-01-27 15:58:57 +01:00
let mut input_stats = HashMap::<String, InputStats>::new();
2025-01-05 13:29:28 +01:00
let mut target_stats = Vec::<TargetStats>::new();
if let Some(source) = sources.get_source_at(source_idx) {
let mut interner = StringInterner::default();
let mut source_playlists = Vec::with_capacity(128);
let broadcast_step = create_broadcast_callback(ctx.event_manager.as_ref());
// Download the sources
let mut source_downloaded = false;
2026-01-02 16:29:05 +01:00
for input_name in &source.inputs {
let Some(input) = sources.get_input_by_name(input_name) else {
error!("Input {input_name} referenced by source {source_idx} does not exist");
continue;
};
if is_input_enabled(input, &ctx.user_targets) {
source_downloaded = true;
2026-01-02 16:29:05 +01:00
let start_time = Instant::now();
2026-01-02 16:29:05 +01:00
// Download the playlist for input
let (mut playlist_groups, mut error_list) = {
2025-12-30 12:31:55 +01:00
broadcast_step("Playlist download", &format!("Downloading input '{}'", input.name));
2026-01-02 16:29:05 +01:00
let (mut download_err, playlist, error) = download_input(ctx, input).await;
2026-01-02 16:29:05 +01:00
2025-12-25 12:33:28 +01:00
if let Some(err) = error {
2026-01-02 16:29:05 +01:00
broadcast_step("Playlist download", &format!("Failed to persist/load input '{}' playlist", input.name));
2025-12-26 16:03:03 +01:00
error!("Failed to persist input playlist {}", input.name);
2025-12-25 12:33:28 +01:00
download_err.push(err);
}
(playlist, download_err)
};
let tvguide = if input.input_type == InputType::Library {
None
} else {
download_input_epg(ctx,input, &mut error_list).await
};
2025-12-25 12:33:28 +01:00
errors.append(&mut error_list);
let group_count = playlist_groups.get_group_count();
let channel_count = playlist_groups.get_channel_count();
let input_name = &input.name;
2025-12-30 15:06:16 +01:00
if playlist_groups.is_empty() {
2025-12-30 12:31:55 +01:00
broadcast_step("Playlist download", &format!("Input '{}' playlist is empty", input.name));
info!("Source is empty {input_name}");
errors.push(notify_err!("Source is empty {input_name}"));
} else {
source_playlists.push(
FetchedPlaylist {
input,
source: playlist_groups,
epg: tvguide,
}
);
}
let elapsed = start_time.elapsed().as_secs();
input_stats.insert(input_name.clone(), create_input_stat(group_count, channel_count, errors.len(),
input.input_type, input_name, elapsed));
2022-03-24 14:08:25 +01:00
}
}
if source_downloaded {
if source_playlists.is_empty() {
debug!("Source at index {source_idx} is empty");
errors.push(notify_err!("Source at index {source_idx} is empty: {}", source.inputs.iter().map(std::string::String::as_str).collect::<Vec<&str>>().join(", ")));
} else {
debug_if_enabled!("Source has {} groups", source_playlists.iter_mut().map(FetchedPlaylist::get_channel_count).sum::<usize>());
for target in &source.targets {
if is_target_enabled(target, &ctx.user_targets) {
match process_playlist_for_target(ctx, &mut source_playlists, target,
&mut input_stats, &mut errors,
&mut interner).await {
Ok(()) => {
target_stats.push(TargetStats::success(&target.name));
}
Err(mut err) => {
target_stats.push(TargetStats::failure(&target.name));
errors.append(&mut err);
}
}
2025-01-05 13:29:28 +01:00
}
}
}
}
}
2025-01-05 13:29:28 +01:00
(input_stats.into_values().collect(), target_stats, errors)
}
async fn download_input_epg(ctx: &PlaylistProcessingContext, input: &Arc<ConfigInput>,
error_list: &mut Vec<TuliproxError>) -> Option<TVGuide> {
// Download epg for input
let (tvguide, mut tvguide_errors) = if error_list.is_empty() {
let working_dir = &ctx.config.config.load().working_dir;
epg::get_xmltv(ctx, input, working_dir).await
} else {
(None, vec![])
};
error_list.append(&mut tvguide_errors);
tvguide
}
async fn download_input(ctx: &PlaylistProcessingContext, input: &Arc<ConfigInput>)
-> (Vec<TuliproxError>, Box<dyn PlaylistSource>, Option<TuliproxError>) {
// Coordination Logic
let need_download = !ctx.is_input_downloaded(&input.name).await;
let (downloaded_playlist, download_err, was_cached, persisted) = if need_download {
// Acquire named lock to prevent thundering herd on same input
let _input_lock = ctx.get_input_lock(&input.name).await;
// Check again after lock
let already_processed = ctx.is_input_downloaded(&input.name).await;
if already_processed {
// Use empty results, will load from disk below
(vec![], vec![], true, false)
} else {
let res = playlist_download_from_input(&ctx.client, &ctx.config, input).await;
// Mark as processed if NO critical errors?
// playlist_download_from_input returns errors but also potentially a partial playlist.
// If it attempted download, we consider it processed for this session.
ctx.mark_input_downloaded(input.name.clone()).await;
res
}
} else {
(vec![], vec![], true, false)
};
let (playlist, error) = if was_cached || persisted {
match load_input_playlist(ctx, input, None).await {
Ok(pl_source) => (pl_source, None),
Err(e) => (MemoryPlaylistSource::default().boxed(), Some(e)),
}
} else {
debug!("Persisting input '{}' playlist", input.name);
let (pl, err) = persist_input_playlist(&ctx.config, input, downloaded_playlist).await;
(MemoryPlaylistSource::new(pl).boxed(), err)
};
(download_err, playlist, error)
}
2025-12-30 15:06:16 +01:00
fn create_broadcast_callback(event_manager: Option<&Arc<EventManager>>) -> StepMeasureCallback {
if let Some(event_mgr) = event_manager {
let events = event_mgr.clone();
Box::new(move |context: &str, msg: &str| {
events.send_event(EventMessage::PlaylistUpdateProgress(context.to_owned(), msg.to_owned()));
})
} else {
Box::new(move |_context: &str, _msg: &str| { /* noop */ })
}
}
2024-11-03 14:52:56 +01:00
fn create_input_stat(group_count: usize, channel_count: usize, error_count: usize, input_type: InputType, input_name: &str, secs_took: u64) -> InputStats {
2024-10-25 00:39:22 +02:00
InputStats {
name: input_name.to_string(),
input_type,
error_count,
2024-10-25 00:39:22 +02:00
raw_stats: PlaylistStats {
group_count,
channel_count,
},
processed_stats: PlaylistStats {
group_count: 0,
channel_count: 0,
},
2024-11-04 18:46:56 +01:00
secs_took,
2024-10-25 00:39:22 +02:00
}
}
#[derive(Clone)]
pub struct PlaylistProcessingContext {
pub client: reqwest::Client,
pub config: Arc<AppConfig>,
pub user_targets: Arc<ProcessTargets>,
pub event_manager: Option<Arc<EventManager>>,
pub playlist_state: Option<Arc<PlaylistStorageState>>,
// Coordination
processed_inputs: Arc<Mutex<HashSet<String>>>,
input_locks: Arc<Mutex<HashMap<String, Weak<RwLock<()>>>>>,
}
impl PlaylistProcessingContext {
pub async fn is_input_downloaded(&self, input_name: &str) -> bool {
let processed = self.processed_inputs.lock().await;
processed.contains(input_name)
}
pub async fn mark_input_downloaded(&self, input_name: String) -> bool {
let mut processed = self.processed_inputs.lock().await;
processed.insert(input_name)
}
pub async fn get_input_lock(&self, input_name: &str) -> OwnedRwLockWriteGuard<()> {
let mut locks = self.input_locks.lock().await;
// Try to upgrade the existing weak reference
let lock = locks.get(input_name)
.and_then(Weak::upgrade)
.unwrap_or_else(|| {
let new_lock = Arc::new(RwLock::new(()));
locks.insert(input_name.to_string(), Arc::downgrade(&new_lock));
new_lock
});
// Clean up stale references periodically
locks.retain(|_, weak| weak.strong_count() > 0);
drop(locks); // Release mutex before awaiting write lock
lock.write_owned().await
}
}
async fn process_sources(processing_ctx: &PlaylistProcessingContext) -> (Vec<SourceStats>, Vec<TuliproxError>) {
2025-11-19 14:13:54 -06:00
let mut async_tasks = JoinSet::new();
let sources = processing_ctx.config.sources.load();
let process_parallel = processing_ctx.config.config.load().process_parallel && sources.sources.len() > 1;
2024-03-26 16:07:31 +01:00
if process_parallel && log_enabled!(Level::Debug) {
2025-11-20 13:00:01 +01:00
debug!("Parallel processing enabled");
}
2025-05-13 21:50:08 +02:00
let errors = Arc::new(Mutex::<Vec<TuliproxError>>::new(vec![]));
2025-01-05 13:29:28 +01:00
let stats = Arc::new(Mutex::<Vec<SourceStats>>::new(vec![]));
2025-12-16 16:23:15 +01:00
for (index, source) in sources.sources.iter().enumerate() {
if !source.should_process_for_user_targets(&processing_ctx.user_targets) {
2025-12-16 16:23:15 +01:00
continue;
}
2025-01-06 09:17:05 +01:00
// We're using the file lock this way on purpose
let source_lock_path = PathBuf::from(format!("source_{index}"));
let Ok(update_lock) = processing_ctx.config.file_locks.try_write_lock(&source_lock_path).await else {
2025-01-06 09:17:05 +01:00
warn!("The update operation for the source at index {index} was skipped because an update is already in progress.");
continue;
};
2023-10-13 13:59:22 +02:00
let shared_errors = errors.clone();
2023-10-19 18:56:08 +02:00
let shared_stats = stats.clone();
let ctx = processing_ctx.clone();
if process_parallel {
2025-11-19 14:13:54 -06:00
async_tasks.spawn(async move {
2025-11-20 13:40:04 +01:00
// Hold the per-source lock for the full duration of this update.
let current_update_lock = update_lock;
2025-11-19 14:13:54 -06:00
let (input_stats, target_stats, mut res_errors) =
process_source(index, &ctx).await;
2025-11-19 14:13:54 -06:00
shared_errors.lock().await.append(&mut res_errors);
if let Some(process_stats) = SourceStats::try_new(input_stats, target_stats) {
shared_stats.lock().await.push(process_stats);
}
2025-11-20 13:40:04 +01:00
drop(current_update_lock);
2025-11-19 14:13:54 -06:00
});
} else {
2025-10-16 21:19:47 +02:00
let (input_stats, target_stats, mut res_errors) =
process_source(index, &ctx).await;
2025-03-11 14:56:10 +01:00
shared_errors.lock().await.append(&mut res_errors);
if let Some(process_stats) = SourceStats::try_new(input_stats, target_stats) {
shared_stats.lock().await.push(process_stats);
}
2025-11-20 13:40:04 +01:00
drop(update_lock);
}
}
2025-11-19 14:13:54 -06:00
while let Some(result) = async_tasks.join_next().await {
if let Err(err) = result {
error!("Playlist processing task failed: {err:?}");
}
2022-03-24 14:08:25 +01:00
}
if let (Ok(s), Ok(e)) = (Arc::try_unwrap(stats), Arc::try_unwrap(errors)) {
(s.into_inner(), e.into_inner())
} else {
(vec![], vec![])
}
2022-03-24 14:08:25 +01:00
}
2023-09-29 16:03:20 +02:00
pub type ProcessingPipe = Vec<fn(source: &mut dyn PlaylistSource, target: &ConfigTarget, interner: &mut StringInterner) -> Option<Vec<PlaylistGroup>>>;
2023-09-29 16:03:20 +02:00
2023-10-13 13:59:22 +02:00
fn get_processing_pipe(target: &ConfigTarget) -> ProcessingPipe {
match &target.processing_order {
ProcessingOrder::Frm => vec![filter_playlist, rename_playlist, map_playlist],
ProcessingOrder::Fmr => vec![filter_playlist, map_playlist, rename_playlist],
ProcessingOrder::Rfm => vec![rename_playlist, filter_playlist, map_playlist],
ProcessingOrder::Rmf => vec![rename_playlist, map_playlist, filter_playlist],
ProcessingOrder::Mfr => vec![map_playlist, filter_playlist, rename_playlist],
ProcessingOrder::Mrf => vec![map_playlist, rename_playlist, filter_playlist]
}
}
2025-12-30 15:06:16 +01:00
fn execute_pipe<'a>(target: &ConfigTarget, pipe: &ProcessingPipe, fpl: &FetchedPlaylist<'a>,
duplicates: &mut HashSet<UUIDType>,
interner: &mut StringInterner) -> FetchedPlaylist<'a> {
let mut new_fpl = FetchedPlaylist {
input: fpl.input,
source: fpl.clone_source(),
epg: fpl.epg.clone(),
};
2025-01-06 10:11:14 +01:00
if target.options.as_ref().is_some_and(|opt| opt.remove_duplicates) {
new_fpl.deduplicate(duplicates);
2025-01-06 10:11:14 +01:00
}
for f in pipe {
if let Some(groups) = f(new_fpl.source.as_mut(), target, interner) {
new_fpl.source = MemoryPlaylistSource::new(groups).boxed();
}
}
// Ensure source is memory-based for downstream mutable processing (VOD/series resolution)
if !new_fpl.is_memory() {
new_fpl.source = MemoryPlaylistSource::new(new_fpl.source.take_groups()).boxed();
}
new_fpl
}
// This method is needed, because of duplicate group names in different inputs.
// We merge the same group names considering cluster together.
2024-11-04 18:46:56 +01:00
fn flatten_groups(playlistgroups: Vec<PlaylistGroup>) -> Vec<PlaylistGroup> {
2024-09-11 12:13:05 +02:00
let mut sort_order: Vec<PlaylistGroup> = vec![];
let mut idx: usize = 0;
let mut group_map: HashMap<CategoryKey, usize> = HashMap::new();
2024-11-04 18:46:56 +01:00
for group in playlistgroups {
let key = (group.xtream_cluster, group.title.clone());
match group_map.entry(key) {
2024-09-11 12:13:05 +02:00
std::collections::hash_map::Entry::Vacant(v) => {
v.insert(idx);
idx += 1;
sort_order.push(group);
}
std::collections::hash_map::Entry::Occupied(o) => {
if let Some(pl_group) = sort_order.get_mut(*o.get()) {
pl_group.channels.extend(group.channels);
}
}
2025-04-10 00:06:48 +02:00
}
2024-11-04 18:46:56 +01:00
}
2024-09-11 12:13:05 +02:00
sort_order
}
2025-10-16 21:19:47 +02:00
#[allow(clippy::too_many_arguments)]
async fn process_playlist_for_target(ctx: &PlaylistProcessingContext,
2025-01-09 15:58:38 +01:00
playlists: &mut [FetchedPlaylist<'_>],
2025-01-06 10:11:14 +01:00
target: &ConfigTarget,
2025-01-27 15:58:57 +01:00
stats: &mut HashMap<String, InputStats>,
errors: &mut Vec<TuliproxError>,
interner: &mut StringInterner,
2025-10-16 21:19:47 +02:00
) -> Result<(), Vec<TuliproxError>> {
2024-12-09 19:26:39 +01:00
debug_if_enabled!("Processing order is {}", &target.processing_order);
2023-09-29 16:03:20 +02:00
2025-01-06 10:11:14 +01:00
let mut duplicates: HashSet<UUIDType> = HashSet::new();
2024-12-10 23:16:04 +01:00
let mut processed_fetched_playlists: Vec<FetchedPlaylist> = vec![];
2025-04-23 20:21:36 +02:00
debug!("Executing processing pipes");
let broadcast_step = create_broadcast_callback(ctx.event_manager.as_ref());
2025-04-23 20:21:36 +02:00
let pipe = get_processing_pipe(target);
let mut step = StepMeasure::new(&target.name, broadcast_step);
2024-12-10 23:16:04 +01:00
for provider_fpl in playlists.iter_mut() {
2025-12-30 15:06:16 +01:00
step.broadcast("Executing transformations on '{}' playlist", &target.name);
let mut processed_fpl = execute_pipe(target, &pipe, provider_fpl, &mut duplicates, interner);
processed_fpl.sort_by_provider_ordinal();
playlist_resolve_series(&ctx.config, &ctx.client, target, errors, &pipe, provider_fpl, &mut processed_fpl, interner).await;
playlist_resolve_vod(&ctx.config, &ctx.client, target, errors, provider_fpl, &mut processed_fpl).await;
2023-10-19 18:56:08 +02:00
// stats
let input_entry_name = processed_fpl.input.name.clone();
let group_count = processed_fpl.get_group_count();
let channel_count = processed_fpl.get_channel_count();
if let Some(stat) = stats.get_mut(&input_entry_name) {
stat.processed_stats.group_count = group_count;
stat.processed_stats.channel_count = channel_count;
2023-10-19 18:56:08 +02:00
}
2024-12-10 23:16:04 +01:00
processed_fetched_playlists.push(processed_fpl);
}
step.tick("filter rename map");
2025-10-01 14:40:44 +02:00
let (new_epg, mut new_playlist) = process_epg(&mut processed_fetched_playlists).await;
step.tick("epg");
2025-04-15 18:38:18 +02:00
2025-12-30 15:06:16 +01:00
if target.favourites.is_some() {
step.broadcast("Processing favourites for '{}' playlist", &target.name);
process_favourites(&mut new_playlist, target.favourites.as_deref());
}
2025-12-29 15:55:50 +01:00
2025-04-15 18:38:18 +02:00
if new_playlist.is_empty() {
step.stop("");
2025-04-15 18:38:18 +02:00
info!("Playlist is empty: {}", &target.name);
Ok(())
} else {
// Process Trakt categories
if trakt_playlist(&ctx.client, target, errors, &mut new_playlist).await {
2025-09-23 16:56:56 +02:00
step.tick("trakt categories");
}
2025-04-15 18:38:18 +02:00
let mut flat_new_playlist = flatten_groups(new_playlist);
step.tick("playlist merge");
if sort_playlist(target, &mut flat_new_playlist) {
2025-09-23 16:56:56 +02:00
step.tick("playlist sort");
}
2025-05-04 10:59:08 +02:00
assign_channel_no_playlist(&mut flat_new_playlist);
step.tick("assigning channel numbers");
2025-04-15 18:38:18 +02:00
map_playlist_counter(target, &mut flat_new_playlist);
step.tick("assigning channel counter");
2025-06-16 11:49:17 +02:00
let config = ctx.config.config.load();
if process_watch(&config, &ctx.client, target, &flat_new_playlist).await {
2025-09-23 16:56:56 +02:00
step.tick("group watches");
}
let result = persist_playlist(&ctx.config, &mut flat_new_playlist, flatten_tvguide(&new_epg).as_ref(), target, ctx.playlist_state.as_ref(), interner).await;
step.stop("Persisting playlists");
2025-04-23 20:21:36 +02:00
result
2025-04-15 18:38:18 +02:00
}
}
2025-12-29 15:55:50 +01:00
pub fn process_favourites(playlist: &mut Vec<PlaylistGroup>, favourites_cfg: Option<&[ConfigFavourites]>) {
if let Some(favourites) = favourites_cfg {
let mut fav_groups: IndexMap<Arc<str>, Vec<PlaylistItem>> = IndexMap::new();
2025-12-29 15:55:50 +01:00
for pg in playlist.iter() {
for pli in &pg.channels {
// series episodes can't be included in favourites
2025-12-29 16:43:33 +01:00
if pli.header.item_type == PlaylistItemType::Series || pli.header.item_type == PlaylistItemType::LocalSeries {
2025-12-29 15:55:50 +01:00
continue;
}
for fav in favourites {
2025-12-29 16:08:27 +01:00
if is_valid(pli, &fav.filter, fav.match_as_ascii) {
2025-12-29 15:55:50 +01:00
let mut channel = pli.clone();
channel.header.group.clone_from(&fav.group);
// Update UUID to be an alias of the original
channel.header.uuid = create_alias_uuid(&pli.header.uuid, &fav.group);
fav_groups
.entry(fav.group.clone())
.or_default()
.push(channel);
}
}
}
}
for (group_name, channels) in fav_groups {
if !channels.is_empty() {
let xtream_cluster = channels[0].header.xtream_cluster;
playlist.push(PlaylistGroup {
id: 0,
title: group_name,
channels,
xtream_cluster,
});
}
}
}
}
2025-12-11 09:53:49 +01:00
async fn trakt_playlist(client: &reqwest::Client, target: &ConfigTarget, errors: &mut Vec<TuliproxError>, playlist: &mut Vec<PlaylistGroup>) -> bool {
match process_trakt_categories_for_target(client, playlist, target).await {
2025-09-23 16:56:56 +02:00
Ok(Some(trakt_categories)) => {
2025-06-16 17:53:03 +02:00
if !trakt_categories.is_empty() {
info!("Adding {} Trakt categories to playlist", trakt_categories.len());
playlist.extend(trakt_categories);
2025-06-16 17:53:03 +02:00
}
}
2025-09-23 16:56:56 +02:00
Ok(None) => {
return false;
}
2025-06-16 17:53:03 +02:00
Err(trakt_errors) => {
warn!("Trakt processing failed with {} errors", trakt_errors.len());
errors.extend(trakt_errors);
}
}
2025-09-23 16:56:56 +02:00
true
2025-06-16 17:53:03 +02:00
}
2025-10-01 14:40:44 +02:00
async fn process_epg(processed_fetched_playlists: &mut Vec<FetchedPlaylist<'_>>) -> (Vec<Epg>, Vec<PlaylistGroup>) {
let mut new_playlist: Vec<PlaylistGroup> = vec![];
2023-10-27 19:03:29 +02:00
let mut new_epg = vec![];
2024-05-05 19:15:54 +02:00
// each fetched playlist can have its own epgl url.
// we need to process each input epg.
2025-04-15 18:38:18 +02:00
for fp in processed_fetched_playlists {
2025-10-01 14:40:44 +02:00
process_playlist_epg(fp, &mut new_epg).await;
new_playlist.extend(fp.source.take_groups());
2024-11-04 18:46:56 +01:00
}
2025-04-15 18:38:18 +02:00
(new_epg, new_playlist)
}
2025-12-11 09:53:49 +01:00
async fn process_watch(cfg: &Config, client: &reqwest::Client, target: &ConfigTarget, new_playlist: &[PlaylistGroup]) -> bool {
if let Some(watches) = &target.watch {
if default_as_default().eq_ignore_ascii_case(&target.name) {
2025-10-28 20:30:04 +01:00
error!("can't watch a target with no unique name");
2025-10-28 19:52:46 +01:00
return false;
}
2025-10-28 20:21:36 +01:00
futures::stream::iter(
new_playlist
.iter()
.filter(|pl| watches.iter().any(|r| r.is_match(&pl.title)))
.map(|pl| process_group_watch(client, cfg, &target.name, pl))
).for_each_concurrent(16, |f| f).await;
2025-09-23 16:56:56 +02:00
true
} else {
false
}
2023-09-29 16:03:20 +02:00
}
2023-10-19 18:56:08 +02:00
2025-12-16 09:54:59 +01:00
pub async fn exec_processing(client: &reqwest::Client, app_config: Arc<AppConfig>, targets: Arc<ProcessTargets>,
event_manager: Option<Arc<EventManager>>, playlist_state: Option<Arc<PlaylistStorageState>>,
update_guard: Option<UpdateGuard>) {
let _guard = if let Some(guard) = update_guard {
if let Some(permit) = guard.try_playlist() {
Some(permit)
} else {
warn!("Playlist update already in progress; update skipped.");
if let Some(events) = event_manager.as_ref() {
events.send_event(EventMessage::PlaylistUpdate(PlaylistUpdateState::Failure));
}
return;
}
} else {
None
};
// Initialize Context
let ctx = PlaylistProcessingContext {
client: client.clone(),
config: app_config.clone(),
user_targets: targets.clone(),
event_manager: event_manager.clone(),
playlist_state: playlist_state.clone(),
processed_inputs: Arc::new(Mutex::new(HashSet::new())),
input_locks: Arc::new(Mutex::new(HashMap::new())),
};
2025-12-16 09:54:59 +01:00
let start_time = Instant::now();
let (stats, errors) = process_sources(&ctx).await;
2024-12-13 17:51:47 +01:00
// log errors
2024-12-28 20:01:28 +01:00
for err in &errors {
error!("{}", err.message);
}
2025-08-03 10:24:23 +02:00
let config = app_config.config.load();
2025-06-24 01:06:25 +02:00
let messaging = config.messaging.as_ref();
2025-10-23 18:26:25 +02:00
if !stats.is_empty() {
match serde_json::to_value(&stats) {
Ok(val) => {
match serde_json::to_string(&serde_json::Value::Object(
serde_json::map::Map::from_iter([("stats".to_string(), val)]))) {
Ok(stats_msg) => {
// print stats
info!("{stats_msg}");
// send stats
2025-12-11 09:53:49 +01:00
send_message_json(client, MsgKind::Stats, messaging, stats_msg.as_str()).await;
2025-10-23 18:26:25 +02:00
}
Err(err) => error!("Failed to serialize playlist stats {err}"),
}
}
2025-10-23 18:26:25 +02:00
Err(err) => error!("Failed to serialize playlist stats {err}")
}
2024-12-13 17:51:47 +01:00
}
2023-10-20 08:23:25 +02:00
// send errors
2023-10-19 18:56:08 +02:00
if let Some(message) = get_errors_notify_message!(errors, 255) {
if let Some(events) = &event_manager {
2025-08-03 10:24:23 +02:00
events.send_event(EventMessage::PlaylistUpdate(PlaylistUpdateState::Failure));
}
2024-12-25 14:12:30 +01:00
if let Ok(error_msg) = serde_json::to_string(&serde_json::Value::Object(serde_json::map::Map::from_iter([("errors".to_string(), serde_json::Value::String(message))]))) {
2025-12-11 09:53:49 +01:00
send_message_json(client, MsgKind::Error, messaging, error_msg.as_str()).await;
2024-12-25 14:12:30 +01:00
}
} else if let Some(events) = &event_manager {
2025-08-03 10:24:23 +02:00
events.send_event(EventMessage::PlaylistUpdate(PlaylistUpdateState::Success));
2023-10-19 18:56:08 +02:00
}
2024-12-25 14:12:30 +01:00
let elapsed = start_time.elapsed().as_secs();
let update_finished_message = format!("🌷 Update process finished! Took {elapsed} secs.");
if let Some(events) = &event_manager {
events.send_event(EventMessage::PlaylistUpdateProgress("Playlist Update".to_string(), update_finished_message.clone()));
}
info!("{update_finished_message}");
2025-02-01 21:50:13 -06:00
}
2025-04-11 20:25:09 +02:00
2025-08-13 13:40:41 +02:00
// #[cfg(test)]
// mod tests {
// #[test]
// fn test_jaro_winkeler() {
// let data = [("yessport5", "heyessport5gold"), ("yessport5", "heyesport5gold")];
//
// data.iter().for_each(|(first, second)|
// println!("jaro_winkler {} = {} => {}", first, second, strsim::jaro_winkler(first, second)));
// // println!("jaro {}", strsim::jaro(data.0, data.1));
// // println!("levenhstein {}", strsim::levenshtein(data.0, data.1));
// // println!("damerau_levenshtein {:?}", strsim::damerau_levenshtein(data.0, data.1));
// // println!("osa distance {:?}", strsim::osa_distance(data.0, data.1));
// // println!("sorensen dice {:?}", strsim::sorensen_dice(data.0, data.1));
// }
2025-11-19 14:13:54 -06:00
// }