Files
tuliprox/src/processing/processor/xtream.rs
T

117 lines
5.3 KiB
Rust
Raw Normal View History

2025-02-06 10:49:34 +01:00
use crate::m3u_filter_error::{info_err, notify_err};
2025-01-17 18:32:17 +01:00
use crate::m3u_filter_error::{str_to_io_error, to_io_error, M3uFilterError, M3uFilterErrorKind};
2024-12-19 13:02:10 +01:00
use crate::model::config::{Config, ConfigInput};
2024-12-24 15:00:05 +01:00
use crate::model::playlist::{FetchedPlaylist, PlaylistEntry, PlaylistItem, PlaylistItemType, XtreamCluster};
2024-12-10 23:16:04 +01:00
use crate::repository::storage::get_input_storage_path;
2024-12-24 15:00:05 +01:00
use serde::{Deserialize, Serialize};
2024-12-22 10:16:23 +01:00
use std::collections::HashMap;
2024-12-25 14:12:30 +01:00
use std::fs::File;
2025-01-17 18:32:17 +01:00
use std::io::{BufWriter, Write};
2024-12-22 10:16:23 +01:00
use std::path::PathBuf;
2025-01-09 15:58:38 +01:00
use std::sync::Arc;
2025-04-17 12:11:51 +02:00
use crate::model::xtream_const;
2024-12-22 10:16:23 +01:00
use crate::repository::bplustree::BPlusTree;
2025-04-17 12:11:51 +02:00
use crate::repository::storage_const;
2024-12-22 10:16:23 +01:00
use crate::repository::xtream_repository::xtream_get_record_file_path;
2025-02-05 20:01:49 +01:00
use crate::utils::file::file_utils::append_or_crate_file;
use crate::utils::network::xtream;
2024-12-19 13:02:10 +01:00
2025-01-09 15:58:38 +01:00
pub(in crate::processing) async fn playlist_resolve_download_playlist_item(client: Arc<reqwest::Client>, pli: &PlaylistItem, input: &ConfigInput, errors: &mut Vec<M3uFilterError>, resolve_delay: u16, cluster: XtreamCluster) -> Option<String> {
2024-12-10 23:16:04 +01:00
let mut result = None;
2024-12-28 20:01:28 +01:00
let provider_id = pli.get_provider_id()?;
2025-02-05 20:01:49 +01:00
if let Some(info_url) = xtream::get_xtream_player_api_info_url(input, cluster, provider_id) {
result = match xtream::get_xtream_stream_info_content(client, &info_url, input).await {
2024-12-10 23:16:04 +01:00
Ok(content) => Some(content),
Err(err) => {
2024-12-23 11:30:33 +01:00
errors.push(info_err!(format!("{err}")));
2024-12-10 23:16:04 +01:00
None
}
};
}
if resolve_delay > 0 {
2025-03-11 14:56:10 +01:00
tokio::time::sleep(std::time::Duration::new(u64::from(resolve_delay), 0)).await;
2024-12-10 23:16:04 +01:00
}
result
}
2024-12-22 05:00:59 +01:00
pub(in crate::processing) fn write_info_content_to_wal_file(writer: &mut BufWriter<&File>, provider_id: u32, content: &str) -> std::io::Result<()> {
2025-01-17 18:32:17 +01:00
let length = u32::try_from(content.len()).map_err(to_io_error)?;
2024-12-19 13:02:10 +01:00
if length > 0 {
writer.write_all(&provider_id.to_le_bytes())?;
writer.write_all(&length.to_le_bytes())?;
writer.write_all(content.as_bytes())?;
}
Ok(())
}
2024-12-24 15:00:05 +01:00
pub(in crate::processing) fn create_resolve_episode_wal_files(cfg: &Config, input: &ConfigInput) -> Option<(File, PathBuf)> {
2025-03-19 09:54:59 +01:00
match get_input_storage_path(&input.name, &cfg.working_dir) {
2024-12-24 15:00:05 +01:00
Ok(storage_path) => {
2025-04-17 12:11:51 +02:00
let info_path = storage_path.join(format!("{}.{}", xtream_const::XC_FILE_SERIES_EPISODE_RECORD, storage_const::FILE_SUFFIX_WAL));
2024-12-25 12:02:00 +01:00
let info_file = append_or_crate_file(&info_path).ok()?;
2024-12-24 15:00:05 +01:00
Some((info_file, info_path))
}
Err(_) => None
}
}
2024-12-22 10:16:23 +01:00
pub(in crate::processing) fn create_resolve_info_wal_files(cfg: &Config, input: &ConfigInput, cluster: XtreamCluster) -> Option<(File, File, PathBuf, PathBuf)> {
2025-03-19 09:54:59 +01:00
match get_input_storage_path(&input.name, &cfg.working_dir) {
2024-12-19 13:02:10 +01:00
Ok(storage_path) => {
if let Some(file_prefix) = match cluster {
XtreamCluster::Live => None,
2025-04-17 12:11:51 +02:00
XtreamCluster::Video => Some(xtream_const::XC_FILE_VOD_INFO),
XtreamCluster::Series => Some(xtream_const::XC_FILE_SERIES_INFO)
2024-12-19 13:02:10 +01:00
} {
2025-04-17 12:11:51 +02:00
let content_path = storage_path.join(format!("{file_prefix}_content.{}", storage_const::FILE_SUFFIX_WAL));
let info_path = storage_path.join(format!("{file_prefix}_record.{}", storage_const::FILE_SUFFIX_WAL));
2024-12-25 12:02:00 +01:00
let content_file = append_or_crate_file(&content_path).ok()?;
let info_file = append_or_crate_file(&info_path).ok()?;
2024-12-22 10:16:23 +01:00
return Some((content_file, info_file, content_path, info_path));
2024-12-19 13:02:10 +01:00
}
None
}
Err(_) => None
}
}
2024-12-22 05:00:59 +01:00
2025-03-11 14:56:10 +01:00
pub(in crate::processing) fn should_update_info(pli: &mut PlaylistItem, processed_provider_ids: &HashMap<u32, u64>, field: &str) -> (bool, u32, u64) {
let Some(provider_id) = pli.header.get_provider_id() else { return (false, 0, 0) };
let last_modified = pli.header.get_additional_property_as_u64(field);
2024-12-25 14:12:30 +01:00
let old_timestamp = processed_provider_ids.get(&provider_id);
(old_timestamp.is_none()
|| last_modified.is_none()
|| *old_timestamp.unwrap() != last_modified.unwrap(), provider_id, last_modified.unwrap_or(0))
2024-12-22 05:00:59 +01:00
}
2024-12-22 10:16:23 +01:00
pub(in crate::processing) async fn read_processed_info_ids<V, F>(cfg: &Config, errors: &mut Vec<M3uFilterError>, fpl: &FetchedPlaylist<'_>,
2024-12-24 15:00:05 +01:00
item_type: PlaylistItemType, extract_ts: F) -> HashMap<u32, u64>
2024-12-22 10:16:23 +01:00
where
F: Fn(&V) -> u64,
V: Serialize + for<'de> Deserialize<'de> + Clone,
{
let mut processed_info_ids = HashMap::new();
2025-03-19 09:54:59 +01:00
let fpl_name = &fpl.input.name;
let file_path = match get_input_storage_path(fpl_name, &cfg.working_dir)
2025-01-17 18:32:17 +01:00
.map(|storage_path| xtream_get_record_file_path(&storage_path, item_type)).and_then(|opt| opt.ok_or_else(|| str_to_io_error("Not supported")))
2024-12-22 10:16:23 +01:00
{
Ok(file_path) => file_path,
Err(err) => {
2024-12-23 11:30:33 +01:00
errors.push(notify_err!(format!("Could not create storage path for input {fpl_name}: {err}")));
2024-12-22 10:16:23 +01:00
return processed_info_ids;
}
};
2025-02-06 10:49:34 +01:00
{
let file_lock = cfg.file_locks.read_lock(&file_path);
if let Ok(info_records) = BPlusTree::<u32, V>::load(&file_path) {
info_records.iter().for_each(|(provider_id, record)| {
processed_info_ids.insert(*provider_id, extract_ts(record));
});
2024-12-22 10:16:23 +01:00
}
2025-02-06 10:49:34 +01:00
drop(file_lock);
2024-12-22 10:16:23 +01:00
}
processed_info_ids
}