2026-01-25 12:41:18 +01:00
|
|
|
use crate::model::{AppConfig, ConfigFavourites, ConfigInput, ConfigRename, ReverseProxyDisabledHeaderConfig, TVGuide};
|
2026-02-10 19:37:20 +01:00
|
|
|
use crate::utils::{log_memory_snapshot, m3u};
|
2025-05-04 10:59:08 +02:00
|
|
|
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;
|
2026-01-08 15:11:12 +01:00
|
|
|
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};
|
2026-01-26 23:04:37 +01:00
|
|
|
use crate::messaging::send_message;
|
|
|
|
|
use crate::model::messaging::MessageContent;
|
2026-01-08 15:11:12 +01:00
|
|
|
|
|
|
|
|
|
2025-06-24 01:06:25 +02:00
|
|
|
use crate::model::FetchedPlaylist;
|
2025-10-22 11:33:49 +02:00
|
|
|
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;
|
2023-10-13 18:13:16 +02:00
|
|
|
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-20 18:51:46 +01:00
|
|
|
use crate::repository::{load_input_playlist, persist_input_playlist, persist_playlist};
|
|
|
|
|
use crate::repository::{CategoryKey, 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;
|
2026-01-08 15:11:12 +01:00
|
|
|
use log::{debug, error, info, log_enabled, warn, Level};
|
2026-01-26 23:04:37 +01:00
|
|
|
use shared::concat_string;
|
2025-08-13 23:07:21 +02:00
|
|
|
use shared::error::{get_errors_notify_message, notify_err, TuliproxError};
|
2026-01-26 23:04:37 +01:00
|
|
|
use shared::foundation::{get_field_value, set_field_value, ValueAccessor, ValueProvider};
|
|
|
|
|
use shared::foundation::Filter;
|
2026-01-02 16:29:05 +01:00
|
|
|
use shared::model::xtream_const::XTREAM_CLUSTER;
|
2026-01-26 23:04:37 +01:00
|
|
|
use shared::model::UUIDType;
|
2026-01-25 12:41:18 +01:00
|
|
|
use shared::model::{CounterModifier, FieldGetAccessor, FieldSetAccessor, InputType, ItemField,
|
2026-01-08 15:11:12 +01:00
|
|
|
PlaylistGroup, PlaylistItem, PlaylistItemType, PlaylistUpdateState,
|
2026-01-20 10:09:58 +01:00
|
|
|
ProcessingOrder, XtreamCluster};
|
|
|
|
|
use shared::utils::{create_alias_uuid, default_as_default, interner_gc, Internable};
|
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)
|
2024-05-04 20:30:20 +02:00
|
|
|
}
|
2021-10-15 16:59:20 +02:00
|
|
|
|
2026-01-08 15:11:12 +01:00
|
|
|
pub fn apply_filter_to_source(source: &mut dyn PlaylistSource, filter: &Filter) -> Option<Vec<PlaylistGroup>> {
|
2026-01-13 16:37:27 +01:00
|
|
|
let mut groups: IndexMap<CategoryKey, PlaylistGroup> = IndexMap::new();
|
2026-01-08 15:11:12 +01:00
|
|
|
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;
|
2026-01-27 18:33:16 +01:00
|
|
|
let normalized_group = shared::utils::deunicode_string(&group_title).to_lowercase().intern();
|
|
|
|
|
let key = (cluster, normalized_group);
|
2026-01-13 16:37:27 +01:00
|
|
|
groups.entry(key)
|
2026-01-08 15:11:12 +01:00
|
|
|
.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()) }
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-20 10:09:58 +01:00
|
|
|
fn filter_playlist(source: &mut dyn PlaylistSource, target: &ConfigTarget) -> Option<Vec<PlaylistGroup>> {
|
2026-01-08 15:11:12 +01:00
|
|
|
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() {
|
2024-05-04 20:30:20 +02:00
|
|
|
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 {
|
2023-10-06 19:24:06 +02:00
|
|
|
id: pg.id,
|
2023-01-13 15:56:03 +01:00
|
|
|
title: pg.title.clone(),
|
|
|
|
|
channels,
|
2024-09-10 20:05:29 +02:00
|
|
|
xtream_cluster: pg.xtream_cluster,
|
2023-01-13 15:56:03 +01:00
|
|
|
});
|
|
|
|
|
}
|
2024-05-10 12:01:48 +02:00
|
|
|
}
|
2026-01-08 15:11:12 +01: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();
|
2025-01-29 17:33:06 +01:00
|
|
|
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;
|
|
|
|
|
}
|
2025-01-29 17:33:06 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-20 10:09:58 +01:00
|
|
|
fn exec_rename(pli: &mut PlaylistItem, rename: Option<&Vec<ConfigRename>>) {
|
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);
|
2026-01-10 20:21:43 +01:00
|
|
|
let cap = r.pattern.replace_all(&value, &r.new_name);
|
2026-01-20 10:09:58 +01:00
|
|
|
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);
|
2025-01-01 10:59:51 +01:00
|
|
|
}
|
2023-05-03 17:46:39 +02:00
|
|
|
let value = cap.into_owned();
|
2026-01-20 10:09:58 +01:00
|
|
|
set_field_value(result, r.field, value);
|
2023-01-13 15:56:03 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-20 10:09:58 +01:00
|
|
|
fn rename_playlist(source: &mut dyn PlaylistSource, target: &ConfigTarget) -> Option<Vec<PlaylistGroup>> {
|
2023-01-13 15:56:03 +01:00
|
|
|
match &target.rename {
|
2026-01-08 15:11:12 +01:00
|
|
|
Some(renames) if !renames.is_empty() => {
|
2026-01-13 16:37:27 +01:00
|
|
|
let mut groups: IndexMap<(XtreamCluster, Arc<str>), PlaylistGroup> = IndexMap::new();
|
2026-01-08 15:11:12 +01:00
|
|
|
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 {
|
2026-01-20 10:09:58 +01:00
|
|
|
pli.header.group = cap.intern();
|
2023-01-12 15:59:40 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-01-20 10:09:58 +01:00
|
|
|
exec_rename(&mut pli, Some(renames));
|
2026-01-08 15:11:12 +01:00
|
|
|
let group_title = pli.header.group.clone();
|
|
|
|
|
let cluster = pli.header.xtream_cluster;
|
|
|
|
|
let cat_id = pli.header.category_id;
|
2026-01-13 16:37:27 +01:00
|
|
|
groups.entry((cluster, group_title.clone()))
|
2026-01-08 15:11:12 +01:00
|
|
|
.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
|
|
|
}
|
2026-01-08 15:11:12 +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-11-30 18:35:01 +01:00
|
|
|
|
2025-12-29 15:55:50 +01:00
|
|
|
fn map_channel(mut channel: PlaylistItem, mapping: &Mapping) -> (PlaylistItem, Vec<PlaylistItem>, bool) {
|
2025-11-30 18:35:01 +01:00
|
|
|
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) {
|
2025-11-30 18:35:01 +01:00
|
|
|
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-11-30 18:35:01 +01:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2026-01-20 10:09:58 +01:00
|
|
|
fn map_playlist(source: &mut dyn PlaylistSource, target: &ConfigTarget) -> Option<Vec<PlaylistGroup>> {
|
2026-01-08 15:11:12 +01:00
|
|
|
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>>
|
|
|
|
|
});
|
2026-01-13 17:03:03 +01:00
|
|
|
let mut next_groups: IndexMap<CategoryKey, PlaylistGroup> = IndexMap::new();
|
2026-01-08 15:11:12 +01:00
|
|
|
let mut grp_id: u32 = 0;
|
|
|
|
|
for channel in mapped_iter {
|
|
|
|
|
let group_title = channel.header.group.clone();
|
|
|
|
|
let cluster = channel.header.xtream_cluster;
|
2026-01-13 16:37:27 +01:00
|
|
|
next_groups.entry((cluster, group_title.clone()))
|
2026-01-08 15:11:12 +01:00
|
|
|
.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
|
|
|
}
|
2026-01-08 15:11:12 +01:00
|
|
|
})
|
|
|
|
|
.channels.push(channel);
|
2022-04-05 12:23:30 +02:00
|
|
|
}
|
2026-01-08 15:11:12 +01: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) {
|
2025-11-30 13:12:59 +01:00
|
|
|
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.
|
2025-05-23 18:17:47 +02:00
|
|
|
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))
|
2023-10-12 11:08:37 +02:00
|
|
|
}
|
|
|
|
|
|
2023-12-08 19:08:04 +01:00
|
|
|
fn is_target_enabled(target: &ConfigTarget, user_targets: &ProcessTargets) -> bool {
|
|
|
|
|
(!user_targets.enabled && target.enabled) || (user_targets.enabled && user_targets.has_target(target.id))
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-08 15:11:12 +01:00
|
|
|
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
|
2026-01-30 00:20:13 +01:00
|
|
|
let storage_path = input_cache::resolve_input_storage_path(working_dir, &input.name).await;
|
2026-01-02 16:29:05 +01:00
|
|
|
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 {
|
2026-01-08 15:11:12 +01:00
|
|
|
return (vec![], vec![], true, false);
|
2026-01-02 16:29:05 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-08 15:11:12 +01:00
|
|
|
let (playlist, errors, persisted) = match input.input_type {
|
|
|
|
|
InputType::M3u => {
|
2026-01-22 13:30:59 +01:00
|
|
|
let (p, e) = m3u::download_m3u_playlist(app_config, client, config, input).await;
|
2026-01-08 15:11:12 +01:00
|
|
|
(p, e, false)
|
|
|
|
|
}
|
2026-01-08 16:20:17 +01:00
|
|
|
InputType::Xtream => xtream::download_xtream_playlist(app_config, client, input, clusters_to_download.as_deref()).await,
|
2026-01-08 15:11:12 +01:00
|
|
|
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
|
|
|
|
2026-01-08 15:11:12 +01:00
|
|
|
(playlist, errors, false, persisted)
|
2025-08-13 13:40:41 +02:00
|
|
|
}
|
|
|
|
|
|
2026-01-08 15:11:12 +01:00
|
|
|
async fn process_source(source_idx: usize, ctx: &PlaylistProcessingContext) -> (Vec<InputStats>, Vec<TargetStats>, Vec<TuliproxError>) {
|
2026-02-10 19:37:20 +01:00
|
|
|
log_memory_snapshot(format!("source[{source_idx}] start").as_str());
|
2026-01-08 15:11:12 +01:00
|
|
|
let sources = ctx.config.sources.load();
|
2023-10-13 13:59:22 +02:00
|
|
|
let mut errors = vec![];
|
2026-01-20 10:09:58 +01:00
|
|
|
let mut input_stats = HashMap::<Arc<str>, InputStats>::new();
|
2025-01-05 13:29:28 +01:00
|
|
|
let mut target_stats = Vec::<TargetStats>::new();
|
2025-07-22 11:32:46 +02:00
|
|
|
if let Some(source) = sources.get_source_at(source_idx) {
|
2026-02-10 19:37:20 +01:00
|
|
|
let mut source_playlists = Vec::with_capacity(source.inputs.len());
|
2026-01-08 15:11:12 +01:00
|
|
|
let broadcast_step = create_broadcast_callback(ctx.event_manager.as_ref());
|
2025-07-22 11:32:46 +02:00
|
|
|
// 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;
|
|
|
|
|
};
|
2026-01-08 15:11:12 +01:00
|
|
|
if is_input_enabled(input, &ctx.user_targets) {
|
2025-07-22 11:32:46 +02:00
|
|
|
source_downloaded = true;
|
2026-02-10 19:37:20 +01:00
|
|
|
log_memory_snapshot(format!("source[{source_idx}] input '{}' before_download", input.name).as_str());
|
2026-01-02 16:29:05 +01:00
|
|
|
|
2025-07-22 11:32:46 +02:00
|
|
|
let start_time = Instant::now();
|
2026-01-02 16:29:05 +01:00
|
|
|
// Download the playlist for input
|
2026-01-08 15:11:12 +01:00
|
|
|
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
|
|
|
|
2026-01-08 15:11:12 +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)
|
|
|
|
|
};
|
2026-02-10 19:37:20 +01:00
|
|
|
log_memory_snapshot(format!("source[{source_idx}] input '{}' after_download", input.name).as_str());
|
2025-12-25 12:33:28 +01:00
|
|
|
|
2026-01-08 16:20:17 +01:00
|
|
|
let tvguide = if input.input_type == InputType::Library {
|
|
|
|
|
None
|
2025-07-22 11:32:46 +02:00
|
|
|
} else {
|
2026-01-20 10:09:58 +01:00
|
|
|
download_input_epg(ctx, input, &mut error_list).await
|
2025-07-22 11:32:46 +02:00
|
|
|
};
|
2026-02-10 19:37:20 +01:00
|
|
|
log_memory_snapshot(format!("source[{source_idx}] input '{}' after_epg_download", input.name).as_str());
|
2025-12-25 12:33:28 +01:00
|
|
|
|
2025-07-22 11:32:46 +02:00
|
|
|
errors.append(&mut error_list);
|
2026-01-08 15:11:12 +01:00
|
|
|
let group_count = playlist_groups.get_group_count();
|
|
|
|
|
let channel_count = playlist_groups.get_channel_count();
|
2025-07-22 11:32:46 +02:00
|
|
|
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));
|
2025-07-22 11:32:46 +02:00
|
|
|
info!("Source is empty {input_name}");
|
2026-01-08 15:11:12 +01:00
|
|
|
errors.push(notify_err!("Source is empty {input_name}"));
|
2025-07-22 11:32:46 +02:00
|
|
|
} else {
|
|
|
|
|
source_playlists.push(
|
|
|
|
|
FetchedPlaylist {
|
|
|
|
|
input,
|
2026-01-08 15:11:12 +01:00
|
|
|
source: playlist_groups,
|
2025-07-22 11:32:46 +02:00
|
|
|
epg: tvguide,
|
|
|
|
|
}
|
|
|
|
|
);
|
2026-02-10 19:37:20 +01:00
|
|
|
log_memory_snapshot(format!("source[{source_idx}] input '{}' after_source_push", input.name).as_str());
|
2025-07-22 11:32:46 +02:00
|
|
|
}
|
|
|
|
|
let elapsed = start_time.elapsed().as_secs();
|
2026-01-08 15:11:12 +01:00
|
|
|
input_stats.insert(input_name.clone(), create_input_stat(group_count, channel_count, errors.len(),
|
2025-10-22 11:33:49 +02:00
|
|
|
input.input_type, input_name, elapsed));
|
2022-03-24 14:08:25 +01:00
|
|
|
}
|
|
|
|
|
}
|
2025-07-22 11:32:46 +02:00
|
|
|
if source_downloaded {
|
|
|
|
|
if source_playlists.is_empty() {
|
|
|
|
|
debug!("Source at index {source_idx} is empty");
|
2026-01-20 10:09:58 +01:00
|
|
|
errors.push(notify_err!("Source at index {source_idx} is empty: {}", source.inputs.iter().map(Clone::clone).collect::<Vec<Arc<str>>>().join(", ")));
|
2025-07-22 11:32:46 +02:00
|
|
|
} else {
|
2026-01-08 15:11:12 +01:00
|
|
|
debug_if_enabled!("Source has {} groups", source_playlists.iter_mut().map(FetchedPlaylist::get_channel_count).sum::<usize>());
|
2026-02-10 19:37:20 +01:00
|
|
|
let enabled_targets: Vec<_> = source.targets
|
|
|
|
|
.iter()
|
|
|
|
|
.filter(|target| is_target_enabled(target, &ctx.user_targets))
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
for (idx, target) in enabled_targets.iter().enumerate() {
|
|
|
|
|
let consume_input_source = idx + 1 == enabled_targets.len();
|
|
|
|
|
debug!(
|
|
|
|
|
"Processing target '{}' (use_memory_cache={}, consume_input_source={})",
|
|
|
|
|
target.name, target.use_memory_cache, consume_input_source
|
|
|
|
|
);
|
|
|
|
|
log_memory_snapshot(format!("source[{source_idx}] target '{}' before_process", target.name).as_str());
|
|
|
|
|
match process_playlist_for_target(ctx, &mut source_playlists, target,
|
|
|
|
|
&mut input_stats, &mut errors, consume_input_source).await {
|
|
|
|
|
Ok(()) => {
|
|
|
|
|
target_stats.push(TargetStats::success(&target.name));
|
|
|
|
|
}
|
|
|
|
|
Err(mut err) => {
|
|
|
|
|
target_stats.push(TargetStats::failure(&target.name));
|
|
|
|
|
errors.append(&mut err);
|
2025-05-23 18:17:47 +02:00
|
|
|
}
|
2025-01-05 13:29:28 +01:00
|
|
|
}
|
2026-02-10 19:37:20 +01:00
|
|
|
log_memory_snapshot(format!("source[{source_idx}] target '{}' after_process", target.name).as_str());
|
2023-10-12 11:08:37 +02:00
|
|
|
}
|
|
|
|
|
}
|
2023-12-08 19:08:04 +01:00
|
|
|
}
|
2023-10-12 11:08:37 +02:00
|
|
|
}
|
2026-02-10 19:37:20 +01:00
|
|
|
log_memory_snapshot(format!("source[{source_idx}] end").as_str());
|
2025-01-05 13:29:28 +01:00
|
|
|
(input_stats.into_values().collect(), target_stats, errors)
|
2023-02-13 12:19:58 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-20 10:09:58 +01:00
|
|
|
async fn download_input_epg(ctx: &PlaylistProcessingContext, input: &Arc<ConfigInput>,
|
2026-01-08 16:20:17 +01:00
|
|
|
error_list: &mut Vec<TuliproxError>) -> Option<TVGuide> {
|
2026-01-08 15:11:12 +01:00
|
|
|
// Download epg for input
|
2026-01-08 16:20:17 +01:00
|
|
|
let (tvguide, mut tvguide_errors) = if error_list.is_empty() {
|
2026-01-08 15:11:12 +01:00
|
|
|
let working_dir = &ctx.config.config.load().working_dir;
|
2026-01-22 13:30:59 +01:00
|
|
|
epg::get_xmltv(ctx, input, None, working_dir).await
|
2026-01-08 15:11:12 +01:00
|
|
|
} else {
|
|
|
|
|
(None, vec![])
|
|
|
|
|
};
|
2026-01-08 16:20:17 +01:00
|
|
|
error_list.append(&mut tvguide_errors);
|
|
|
|
|
tvguide
|
2026-01-08 15:11:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn download_input(ctx: &PlaylistProcessingContext, input: &Arc<ConfigInput>)
|
2026-01-20 10:09:58 +01:00
|
|
|
-> (Vec<TuliproxError>, Box<dyn PlaylistSource>, Option<TuliproxError>) {
|
2026-01-08 15:11:12 +01:00
|
|
|
// 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(),
|
2024-10-30 11:14:55 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-08 15:11:12 +01: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>>,
|
2026-01-22 13:30:59 +01:00
|
|
|
pub disabled_headers: Option<ReverseProxyDisabledHeaderConfig>,
|
2026-01-08 15:11:12 +01:00
|
|
|
|
|
|
|
|
// Coordination
|
2026-01-20 10:09:58 +01:00
|
|
|
processed_inputs: Arc<Mutex<HashSet<Arc<str>>>>,
|
|
|
|
|
#[allow(clippy::type_complexity)]
|
|
|
|
|
input_locks: Arc<Mutex<HashMap<Arc<str>, Weak<RwLock<()>>>>>,
|
2026-01-08 15:11:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl PlaylistProcessingContext {
|
|
|
|
|
pub async fn is_input_downloaded(&self, input_name: &str) -> bool {
|
|
|
|
|
let processed = self.processed_inputs.lock().await;
|
|
|
|
|
processed.contains(input_name)
|
|
|
|
|
}
|
2026-01-20 10:09:58 +01:00
|
|
|
pub async fn mark_input_downloaded(&self, input_name: Arc<str>) -> bool {
|
2026-01-08 15:11:12 +01:00
|
|
|
let mut processed = self.processed_inputs.lock().await;
|
|
|
|
|
processed.insert(input_name)
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-20 10:09:58 +01:00
|
|
|
pub async fn get_input_lock(&self, input_name: &Arc<str>) -> OwnedRwLockWriteGuard<()> {
|
2026-01-08 15:11:12 +01:00
|
|
|
let mut locks = self.input_locks.lock().await;
|
2026-01-08 15:47:34 +01:00
|
|
|
// 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(()));
|
2026-01-20 10:09:58 +01:00
|
|
|
locks.insert(input_name.clone(), Arc::downgrade(&new_lock));
|
2026-01-08 15:47:34 +01:00
|
|
|
new_lock
|
|
|
|
|
});
|
2026-01-08 15:11:12 +01:00
|
|
|
|
2026-01-08 15:47:34 +01:00
|
|
|
// Clean up stale references periodically
|
|
|
|
|
locks.retain(|_, weak| weak.strong_count() > 0);
|
2026-01-08 15:11:12 +01:00
|
|
|
|
2026-01-08 15:47:34 +01:00
|
|
|
drop(locks); // Release mutex before awaiting write lock
|
2026-01-08 15:11:12 +01:00
|
|
|
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();
|
2026-01-08 15:11:12 +01:00
|
|
|
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");
|
2023-11-03 18:05:13 +01:00
|
|
|
}
|
2026-01-08 15:11:12 +01:00
|
|
|
|
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![]));
|
2026-01-08 15:11:12 +01:00
|
|
|
|
2025-12-16 16:23:15 +01:00
|
|
|
for (index, source) in sources.sources.iter().enumerate() {
|
2026-01-08 15:11:12 +01:00
|
|
|
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
|
2026-01-20 10:09:58 +01:00
|
|
|
let source_lock_path = PathBuf::from(concat_string!("source_", &index.to_string()));
|
2026-01-08 15:11:12 +01:00
|
|
|
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();
|
2026-01-08 15:11:12 +01:00
|
|
|
let ctx = processing_ctx.clone();
|
|
|
|
|
|
2023-02-13 12:19:58 +01:00
|
|
|
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) =
|
2026-01-08 15:11:12 +01:00
|
|
|
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-07-22 11:32:46 +02:00
|
|
|
}
|
2025-11-20 13:40:04 +01:00
|
|
|
drop(current_update_lock);
|
2025-11-19 14:13:54 -06:00
|
|
|
});
|
2023-02-13 12:19:58 +01:00
|
|
|
} else {
|
2025-10-16 21:19:47 +02:00
|
|
|
let (input_stats, target_stats, mut res_errors) =
|
2026-01-08 15:11:12 +01:00
|
|
|
process_source(index, &ctx).await;
|
2025-03-11 14:56:10 +01:00
|
|
|
shared_errors.lock().await.append(&mut res_errors);
|
2025-10-22 11:33:49 +02:00
|
|
|
if let Some(process_stats) = SourceStats::try_new(input_stats, target_stats) {
|
2025-10-22 11:04:23 +02:00
|
|
|
shared_stats.lock().await.push(process_stats);
|
|
|
|
|
}
|
2025-11-20 13:40:04 +01:00
|
|
|
drop(update_lock);
|
2023-02-13 12:19:58 +01:00
|
|
|
}
|
|
|
|
|
}
|
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
|
|
|
}
|
2025-07-22 11:32:46 +02: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
|
|
|
|
2026-01-20 10:09:58 +01:00
|
|
|
pub type ProcessingPipe = Vec<fn(source: &mut dyn PlaylistSource, target: &ConfigTarget) -> 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]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-10 19:37:20 +01:00
|
|
|
fn execute_pipe<'a>(target: &ConfigTarget, pipe: &ProcessingPipe, fpl: &mut FetchedPlaylist<'a>,
|
|
|
|
|
duplicates: &mut HashSet<UUIDType>, consume_source: bool) -> FetchedPlaylist<'a> {
|
|
|
|
|
let source = if consume_source {
|
|
|
|
|
if fpl.is_memory() {
|
|
|
|
|
MemoryPlaylistSource::new(fpl.source.take_groups()).boxed()
|
|
|
|
|
} else {
|
|
|
|
|
std::mem::replace(&mut fpl.source, MemoryPlaylistSource::default().boxed())
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
fpl.clone_source()
|
|
|
|
|
};
|
|
|
|
|
|
2024-09-10 20:05:29 +02:00
|
|
|
let mut new_fpl = FetchedPlaylist {
|
|
|
|
|
input: fpl.input,
|
2026-02-10 19:37:20 +01:00
|
|
|
source,
|
2024-09-10 20:05:29 +02:00
|
|
|
epg: fpl.epg.clone(),
|
|
|
|
|
};
|
2025-01-06 10:11:14 +01:00
|
|
|
if target.options.as_ref().is_some_and(|opt| opt.remove_duplicates) {
|
2026-01-08 15:11:12 +01:00
|
|
|
new_fpl.deduplicate(duplicates);
|
2025-01-06 10:11:14 +01:00
|
|
|
}
|
|
|
|
|
|
2024-09-10 20:05:29 +02:00
|
|
|
for f in pipe {
|
2026-01-20 10:09:58 +01:00
|
|
|
if let Some(groups) = f(new_fpl.source.as_mut(), target) {
|
2026-01-08 15:11:12 +01:00
|
|
|
new_fpl.source = MemoryPlaylistSource::new(groups).boxed();
|
2024-09-10 20:05:29 +02:00
|
|
|
}
|
|
|
|
|
}
|
2026-01-08 15:11:12 +01:00
|
|
|
// 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();
|
|
|
|
|
}
|
2024-09-10 20:05:29 +02:00
|
|
|
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;
|
2026-01-08 15:11:12 +01:00
|
|
|
let mut group_map: HashMap<CategoryKey, usize> = HashMap::new();
|
2024-11-04 18:46:56 +01:00
|
|
|
for group in playlistgroups {
|
2026-01-27 18:33:16 +01:00
|
|
|
let normalized_title: Arc<str> = shared::utils::deunicode_string(&group.title).to_lowercase().intern();
|
|
|
|
|
let key = (group.xtream_cluster, normalized_title);
|
2024-09-10 20:05:29 +02:00
|
|
|
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);
|
|
|
|
|
}
|
2024-09-10 20:05:29 +02:00
|
|
|
std::collections::hash_map::Entry::Occupied(o) => {
|
2025-07-22 11:32:46 +02:00
|
|
|
if let Some(pl_group) = sort_order.get_mut(*o.get()) {
|
|
|
|
|
pl_group.channels.extend(group.channels);
|
|
|
|
|
}
|
2024-09-10 20:05:29 +02:00
|
|
|
}
|
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
|
2024-09-10 20:05:29 +02:00
|
|
|
}
|
|
|
|
|
|
2025-10-16 21:19:47 +02:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2026-01-08 15:11:12 +01:00
|
|
|
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,
|
2026-01-20 10:09:58 +01:00
|
|
|
stats: &mut HashMap<Arc<str>, InputStats>,
|
2025-08-04 14:18:46 +02:00
|
|
|
errors: &mut Vec<TuliproxError>,
|
2026-02-10 19:37:20 +01:00
|
|
|
consume_input_source: bool,
|
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);
|
2026-02-10 19:37:20 +01:00
|
|
|
log_memory_snapshot(format!("target '{}' start", target.name).as_str());
|
2023-09-29 16:03:20 +02:00
|
|
|
|
2025-01-06 10:11:14 +01:00
|
|
|
let mut duplicates: HashSet<UUIDType> = HashSet::new();
|
2026-02-10 19:37:20 +01:00
|
|
|
let mut new_epg = vec![];
|
|
|
|
|
let mut new_playlist: Vec<PlaylistGroup> = vec![];
|
2025-04-23 20:21:36 +02:00
|
|
|
|
|
|
|
|
debug!("Executing processing pipes");
|
2026-01-08 15:11:12 +01:00
|
|
|
let broadcast_step = create_broadcast_callback(ctx.event_manager.as_ref());
|
2025-04-23 20:21:36 +02:00
|
|
|
|
2026-01-04 14:55:19 +01:00
|
|
|
let pipe = get_processing_pipe(target);
|
2025-08-04 14:18:46 +02:00
|
|
|
let mut step = StepMeasure::new(&target.name, broadcast_step);
|
2024-12-10 23:16:04 +01:00
|
|
|
for provider_fpl in playlists.iter_mut() {
|
2026-02-10 19:37:20 +01:00
|
|
|
log_memory_snapshot(format!("target '{}' input '{}' before_pipe", target.name, provider_fpl.input.name).as_str());
|
2025-12-30 15:06:16 +01:00
|
|
|
step.broadcast("Executing transformations on '{}' playlist", &target.name);
|
2026-02-10 19:37:20 +01:00
|
|
|
let mut processed_fpl = execute_pipe(target, &pipe, provider_fpl, &mut duplicates, consume_input_source);
|
|
|
|
|
log_memory_snapshot(format!("target '{}' input '{}' after_pipe", target.name, provider_fpl.input.name).as_str());
|
2026-01-08 15:11:12 +01:00
|
|
|
processed_fpl.sort_by_provider_ordinal();
|
2026-01-20 10:09:58 +01:00
|
|
|
playlist_resolve_series(&ctx.config, &ctx.client, target, errors, &pipe, provider_fpl, &mut processed_fpl).await;
|
2026-02-10 19:37:20 +01:00
|
|
|
log_memory_snapshot(format!("target '{}' input '{}' after_series_resolve", target.name, provider_fpl.input.name).as_str());
|
2026-01-08 15:11:12 +01:00
|
|
|
playlist_resolve_vod(&ctx.config, &ctx.client, target, errors, provider_fpl, &mut processed_fpl).await;
|
2026-02-10 19:37:20 +01:00
|
|
|
log_memory_snapshot(format!("target '{}' input '{}' after_vod_resolve", target.name, provider_fpl.input.name).as_str());
|
2023-10-19 18:56:08 +02:00
|
|
|
// stats
|
2026-01-08 15:11:12 +01:00
|
|
|
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
|
|
|
}
|
2026-02-10 19:37:20 +01:00
|
|
|
process_playlist_epg(&mut processed_fpl, &mut new_epg).await;
|
|
|
|
|
log_memory_snapshot(format!("target '{}' input '{}' after_epg_apply", target.name, processed_fpl.input.name).as_str());
|
|
|
|
|
new_playlist.extend(processed_fpl.source.take_groups());
|
|
|
|
|
log_memory_snapshot(format!("target '{}' input '{}' after_take_groups", target.name, processed_fpl.input.name).as_str());
|
|
|
|
|
tokio::task::yield_now().await;
|
2023-12-08 19:08:04 +01:00
|
|
|
}
|
2026-02-10 19:37:20 +01:00
|
|
|
step.tick("filter rename map + epg");
|
|
|
|
|
log_memory_snapshot(format!("target '{}' after_filter_rename_map_epg", target.name).as_str());
|
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());
|
2026-02-10 19:37:20 +01:00
|
|
|
log_memory_snapshot(format!("target '{}' after_favourites", target.name).as_str());
|
2025-12-30 15:06:16 +01:00
|
|
|
}
|
2025-12-29 15:55:50 +01:00
|
|
|
|
2025-04-15 18:38:18 +02:00
|
|
|
if new_playlist.is_empty() {
|
2025-08-04 14:18:46 +02:00
|
|
|
step.stop("");
|
2025-04-15 18:38:18 +02:00
|
|
|
info!("Playlist is empty: {}", &target.name);
|
|
|
|
|
Ok(())
|
|
|
|
|
} else {
|
2025-06-17 13:06:52 +02:00
|
|
|
// Process Trakt categories
|
2026-01-08 15:11:12 +01:00
|
|
|
if trakt_playlist(&ctx.client, target, errors, &mut new_playlist).await {
|
2025-09-23 16:56:56 +02:00
|
|
|
step.tick("trakt categories");
|
2026-02-10 19:37:20 +01:00
|
|
|
log_memory_snapshot(format!("target '{}' after_trakt", target.name).as_str());
|
2025-09-23 16:56:56 +02:00
|
|
|
}
|
2025-06-17 13:06:52 +02:00
|
|
|
|
2025-04-15 18:38:18 +02:00
|
|
|
let mut flat_new_playlist = flatten_groups(new_playlist);
|
2025-08-04 14:18:46 +02:00
|
|
|
step.tick("playlist merge");
|
2026-02-10 19:37:20 +01:00
|
|
|
log_memory_snapshot(format!("target '{}' after_playlist_merge", target.name).as_str());
|
2025-06-17 13:06:52 +02:00
|
|
|
|
2025-10-22 11:33:49 +02:00
|
|
|
if sort_playlist(target, &mut flat_new_playlist) {
|
2025-09-23 16:56:56 +02:00
|
|
|
step.tick("playlist sort");
|
2026-02-10 19:37:20 +01:00
|
|
|
log_memory_snapshot(format!("target '{}' after_playlist_sort", target.name).as_str());
|
2025-09-23 16:56:56 +02:00
|
|
|
}
|
2025-05-04 10:59:08 +02:00
|
|
|
assign_channel_no_playlist(&mut flat_new_playlist);
|
2025-08-04 14:18:46 +02:00
|
|
|
step.tick("assigning channel numbers");
|
2026-02-10 19:37:20 +01:00
|
|
|
log_memory_snapshot(format!("target '{}' after_assign_channel_numbers", target.name).as_str());
|
2025-04-15 18:38:18 +02:00
|
|
|
map_playlist_counter(target, &mut flat_new_playlist);
|
2025-08-04 14:18:46 +02:00
|
|
|
step.tick("assigning channel counter");
|
2026-02-10 19:37:20 +01:00
|
|
|
log_memory_snapshot(format!("target '{}' after_assign_channel_counter", target.name).as_str());
|
2025-06-16 11:49:17 +02:00
|
|
|
|
2026-01-25 12:41:18 +01:00
|
|
|
if process_watch(&ctx.config, &ctx.client, target, &flat_new_playlist).await {
|
2025-09-23 16:56:56 +02:00
|
|
|
step.tick("group watches");
|
2026-02-10 19:37:20 +01:00
|
|
|
log_memory_snapshot(format!("target '{}' after_group_watches", target.name).as_str());
|
2025-09-23 16:56:56 +02:00
|
|
|
}
|
2026-01-29 18:08:22 +01:00
|
|
|
let result = persist_playlist(&ctx.config, &mut flat_new_playlist, flatten_tvguide(new_epg).as_ref(), target, ctx.playlist_state.as_ref()).await;
|
2025-08-04 14:18:46 +02:00
|
|
|
step.stop("Persisting playlists");
|
2026-02-10 19:37:20 +01:00
|
|
|
log_memory_snapshot(format!("target '{}' after_persist", target.name).as_str());
|
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 {
|
2026-01-13 16:37:27 +01:00
|
|
|
let mut fav_groups: IndexMap<CategoryKey, Vec<PlaylistItem>> = IndexMap::new();
|
2025-12-29 15:55:50 +01:00
|
|
|
for pg in playlist.iter() {
|
|
|
|
|
for pli in &pg.channels {
|
2026-01-08 15:47:34 +01:00
|
|
|
// 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 {
|
2026-01-13 16:37:27 +01:00
|
|
|
if pli.header.xtream_cluster == fav.cluster && 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
|
2026-01-13 16:37:27 +01:00
|
|
|
.entry((fav.cluster, fav.group.clone()))
|
2025-12-29 15:55:50 +01:00
|
|
|
.or_default()
|
|
|
|
|
.push(channel);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-13 16:37:27 +01:00
|
|
|
for (fav_group, channels) in fav_groups {
|
2025-12-29 15:55:50 +01:00
|
|
|
if !channels.is_empty() {
|
2026-01-13 16:37:27 +01:00
|
|
|
let (xtream_cluster, group_name) = fav_group;
|
2025-12-29 15:55:50 +01:00
|
|
|
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());
|
2025-06-17 13:06:52 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2026-01-25 12:41:18 +01:00
|
|
|
async fn process_watch(app_config: &Arc<AppConfig>, client: &reqwest::Client, target: &ConfigTarget, new_playlist: &[PlaylistGroup]) -> bool {
|
2025-10-22 11:33:49 +02:00
|
|
|
if let Some(watches) = &target.watch {
|
2024-09-10 20:05:29 +02:00
|
|
|
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)))
|
2026-01-25 12:41:18 +01:00
|
|
|
.map(|pl| process_group_watch(app_config, client, &target.name, pl))
|
2025-10-28 20:21:36 +01:00
|
|
|
).for_each_concurrent(16, |f| f).await;
|
|
|
|
|
|
2025-09-23 16:56:56 +02:00
|
|
|
true
|
|
|
|
|
} else {
|
|
|
|
|
false
|
2023-10-06 19:24:06 +02:00
|
|
|
}
|
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>>,
|
2026-01-22 13:30:59 +01:00
|
|
|
update_guard: Option<UpdateGuard>,
|
|
|
|
|
disabled_headers: Option<ReverseProxyDisabledHeaderConfig>) {
|
2026-01-26 23:04:37 +01:00
|
|
|
let _guard = if let Some(guard) = update_guard {
|
2025-12-16 09:54:59 +01:00
|
|
|
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
|
|
|
|
|
};
|
|
|
|
|
|
2026-02-10 19:37:20 +01:00
|
|
|
log_memory_snapshot("exec_processing start");
|
|
|
|
|
|
2026-01-08 15:11:12 +01:00
|
|
|
// 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())),
|
2026-01-22 13:30:59 +01:00
|
|
|
disabled_headers,
|
2026-01-08 15:11:12 +01:00
|
|
|
};
|
|
|
|
|
|
2025-12-16 09:54:59 +01:00
|
|
|
let start_time = Instant::now();
|
2026-01-08 15:11:12 +01:00
|
|
|
let (stats, errors) = process_sources(&ctx).await;
|
2026-02-10 19:37:20 +01:00
|
|
|
log_memory_snapshot("exec_processing after_process_sources");
|
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-10-23 18:26:25 +02:00
|
|
|
|
|
|
|
|
if !stats.is_empty() {
|
2026-01-25 12:41:18 +01:00
|
|
|
// print stats
|
|
|
|
|
if let Ok(stats_msg) = serde_json::to_string(&stats) {
|
|
|
|
|
info!("stats: {stats_msg}");
|
2025-10-22 11:33:49 +02:00
|
|
|
}
|
2026-01-25 12:41:18 +01:00
|
|
|
// send stats
|
|
|
|
|
send_message(&app_config, client, MessageContent::event_stats(stats)).await;
|
2024-12-13 17:51:47 +01:00
|
|
|
}
|
2025-10-22 11:33:49 +02: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) {
|
2026-01-08 15:11:12 +01:00
|
|
|
if let Some(events) = &event_manager {
|
2025-08-03 10:24:23 +02:00
|
|
|
events.send_event(EventMessage::PlaylistUpdate(PlaylistUpdateState::Failure));
|
|
|
|
|
}
|
2026-01-25 12:41:18 +01:00
|
|
|
send_message(&app_config, client, MessageContent::event_error(message)).await;
|
2026-01-08 15:11:12 +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
|
|
|
}
|
2026-01-08 15:11:12 +01:00
|
|
|
|
2024-12-25 14:12:30 +01:00
|
|
|
let elapsed = start_time.elapsed().as_secs();
|
2026-01-08 15:11:12 +01:00
|
|
|
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()));
|
|
|
|
|
}
|
2026-02-10 19:37:20 +01:00
|
|
|
log_memory_snapshot("exec_processing before_interner_gc");
|
2026-01-20 10:09:58 +01:00
|
|
|
debug!("StringInterner GC removed {} strings", interner_gc());
|
2026-02-10 19:37:20 +01:00
|
|
|
log_memory_snapshot("exec_processing after_interner_gc");
|
|
|
|
|
//trim_allocator_after_update();
|
2026-01-20 10:09:58 +01:00
|
|
|
|
2026-01-08 15:11:12 +01:00
|
|
|
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 {
|
2025-10-22 11:33:49 +02:00
|
|
|
// #[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-04-30 17:16:53 +02:00
|
|
|
|
2025-11-19 14:13:54 -06:00
|
|
|
// }
|