2024-12-19 13:02:10 +01:00
|
|
|
use crate::m3u_filter_error::{M3uFilterError, M3uFilterErrorKind};
|
|
|
|
|
use crate::model::config::{Config, ConfigTarget, InputType};
|
2024-12-24 15:00:05 +01:00
|
|
|
use crate::model::playlist::{FetchedPlaylist, PlaylistItem, PlaylistItemType, XtreamCluster};
|
2024-12-26 12:11:28 +01:00
|
|
|
use crate::processing::xtream_processor::{create_resolve_info_wal_files, playlist_resolve_download_playlist_item, read_processed_info_ids, should_update_info, write_info_content_to_wal_file};
|
2024-12-22 10:16:23 +01:00
|
|
|
use crate::repository::xtream_repository::{xtream_update_input_info_file, xtream_update_input_vod_record_from_wal_file, InputVodInfoRecord};
|
2024-12-23 11:30:33 +01:00
|
|
|
use crate::{create_resolve_options_function_for_xtream_target, handle_error, handle_error_and_return, notify_err};
|
2024-12-26 12:11:28 +01:00
|
|
|
use crate::utils::json_utils::{get_u32_from_serde_value, get_u64_from_serde_value};
|
2024-12-19 13:02:10 +01:00
|
|
|
use serde_json::{Map, Value};
|
2024-12-22 10:16:23 +01:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
use std::fs::File;
|
2024-12-19 13:02:10 +01:00
|
|
|
use std::io::{BufWriter, Write};
|
2025-01-01 10:59:51 +01:00
|
|
|
use std::time::Instant;
|
2025-01-01 11:09:27 +01:00
|
|
|
use log::{info, log_enabled, Level};
|
2024-12-19 13:02:10 +01:00
|
|
|
|
|
|
|
|
const TAG_VOD_INFO_INFO: &str = "info";
|
|
|
|
|
const TAG_VOD_INFO_MOVIE_DATA: &str = "movie_data";
|
|
|
|
|
const TAG_VOD_INFO_TMDB_ID: &str = "tmdb_id";
|
|
|
|
|
const TAG_VOD_INFO_STREAM_ID: &str = "stream_id";
|
2024-12-22 05:00:59 +01:00
|
|
|
const TAG_VOD_INFO_ADDED: &str = "added";
|
2024-12-19 13:02:10 +01:00
|
|
|
|
2024-12-28 16:26:04 +01:00
|
|
|
create_resolve_options_function_for_xtream_target!(vod);
|
2024-12-22 05:00:59 +01:00
|
|
|
|
2024-12-24 15:00:05 +01:00
|
|
|
async fn read_processed_vod_info_ids(cfg: &Config, errors: &mut Vec<M3uFilterError>, fpl: &FetchedPlaylist<'_>) -> HashMap<u32, u64> {
|
|
|
|
|
read_processed_info_ids(cfg, errors, fpl, PlaylistItemType::Video, |record: &InputVodInfoRecord| record.ts).await
|
2024-12-22 05:00:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn extract_info_record_from_vod_info(content: &str) -> Option<(u32, InputVodInfoRecord)> {
|
|
|
|
|
let doc = serde_json::from_str::<Map<String, Value>>(content).ok()?;
|
|
|
|
|
|
|
|
|
|
let movie_data = doc.get(TAG_VOD_INFO_MOVIE_DATA)?.as_object()?;
|
|
|
|
|
let provider_id = get_u32_from_serde_value(
|
|
|
|
|
movie_data.get(TAG_VOD_INFO_STREAM_ID)?,
|
|
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
let added = movie_data
|
|
|
|
|
.get(TAG_VOD_INFO_ADDED)
|
|
|
|
|
.and_then(get_u64_from_serde_value)
|
|
|
|
|
.unwrap_or(0);
|
|
|
|
|
|
2024-12-22 10:16:23 +01:00
|
|
|
let tmdb_id = doc.get(TAG_VOD_INFO_INFO)?.as_object()
|
2024-12-22 05:00:59 +01:00
|
|
|
.and_then(|info| info.get(TAG_VOD_INFO_TMDB_ID))
|
|
|
|
|
.and_then(get_u32_from_serde_value)
|
|
|
|
|
.unwrap_or(0);
|
|
|
|
|
|
|
|
|
|
Some((provider_id, InputVodInfoRecord {
|
|
|
|
|
tmdb_id,
|
|
|
|
|
ts: added,
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn write_vod_info_record_to_wal_file(
|
|
|
|
|
writer: &mut BufWriter<&File>,
|
|
|
|
|
provider_id: u32,
|
|
|
|
|
record: &InputVodInfoRecord,
|
|
|
|
|
) -> std::io::Result<()> {
|
|
|
|
|
writer.write_all(&provider_id.to_le_bytes())?;
|
|
|
|
|
writer.write_all(&record.tmdb_id.to_le_bytes())?;
|
|
|
|
|
writer.write_all(&record.ts.to_le_bytes())?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-25 12:02:00 +01:00
|
|
|
fn should_update_vod_info(pli: &PlaylistItem, processed_provider_ids: &HashMap<u32, u64>) -> (bool, u32, u64) {
|
2024-12-22 10:16:23 +01:00
|
|
|
should_update_info(pli, processed_provider_ids, TAG_VOD_INFO_ADDED)
|
2024-12-19 13:02:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn playlist_resolve_vod(cfg: &Config, target: &ConfigTarget, errors: &mut Vec<M3uFilterError>, fpl: &FetchedPlaylist<'_>) {
|
2024-12-28 16:26:04 +01:00
|
|
|
let (resolve_movies, resolve_delay) = get_resolve_vod_options(target, fpl);
|
2024-12-19 13:02:10 +01:00
|
|
|
if !resolve_movies { return; }
|
|
|
|
|
|
|
|
|
|
// we cant write to the indexed-document directly because of the write lock and time-consuming operation.
|
|
|
|
|
// All readers would be waiting for the lock and the app would be unresponsive.
|
2024-12-22 05:00:59 +01:00
|
|
|
// We collect the content into a wal file and write it once we collected everything.
|
2024-12-25 12:02:00 +01:00
|
|
|
let Some((wal_content_file, wal_record_file, wal_content_path, wal_record_path)) = create_resolve_info_wal_files(cfg, fpl.input, XtreamCluster::Video)
|
2024-12-22 05:00:59 +01:00
|
|
|
else { return; };
|
2024-12-19 13:02:10 +01:00
|
|
|
|
2024-12-24 15:00:05 +01:00
|
|
|
let mut processed_info_ids = read_processed_vod_info_ids(cfg, errors, fpl).await;
|
2024-12-22 10:16:23 +01:00
|
|
|
let mut content_writer = BufWriter::new(&wal_content_file);
|
|
|
|
|
let mut record_writer = BufWriter::new(&wal_record_file);
|
2024-12-19 13:02:10 +01:00
|
|
|
let mut content_updated = false;
|
2024-12-22 05:00:59 +01:00
|
|
|
|
2025-01-01 10:59:51 +01:00
|
|
|
let vod_info_iter = fpl.playlistgroups.iter()
|
2024-12-22 05:00:59 +01:00
|
|
|
.flat_map(|plg| &plg.channels)
|
2025-01-01 10:59:51 +01:00
|
|
|
.filter(|&pli| pli.header.borrow().xtream_cluster == XtreamCluster::Video);
|
|
|
|
|
|
|
|
|
|
let vod_info_count = vod_info_iter.clone().count();
|
|
|
|
|
|
|
|
|
|
info!("Found {vod_info_count} vod info to resolve");
|
|
|
|
|
let start_time = Instant::now();
|
|
|
|
|
let mut processed_vod_info_count = 0;
|
|
|
|
|
let mut last_processed_vod_info_count = 0;
|
|
|
|
|
|
|
|
|
|
for pli in vod_info_iter {
|
2024-12-25 12:02:00 +01:00
|
|
|
let (should_update, _provider_id, _ts) = should_update_vod_info(pli, &processed_info_ids);
|
2024-12-22 10:16:23 +01:00
|
|
|
if should_update {
|
2024-12-26 12:11:28 +01:00
|
|
|
if let Some(content) = playlist_resolve_download_playlist_item(pli, fpl.input, errors, resolve_delay, XtreamCluster::Video).await {
|
2024-12-22 05:00:59 +01:00
|
|
|
if let Some((provider_id, info_record)) = extract_info_record_from_vod_info(&content) {
|
|
|
|
|
let ts = info_record.ts;
|
2024-12-23 11:30:33 +01:00
|
|
|
handle_error_and_return!(write_info_content_to_wal_file(&mut content_writer, provider_id, &content),
|
|
|
|
|
|err| errors.push(notify_err!(format!("Failed to resolve vod, could not write to content wal file {err}"))));
|
2024-12-22 05:00:59 +01:00
|
|
|
processed_info_ids.insert(provider_id, ts);
|
2024-12-23 11:30:33 +01:00
|
|
|
handle_error_and_return!(write_vod_info_record_to_wal_file(&mut record_writer, provider_id, &info_record),
|
|
|
|
|
|err| errors.push(notify_err!(format!("Failed to resolve vod wal, could not write to record wal file {err}"))));
|
2024-12-19 13:02:10 +01:00
|
|
|
content_updated = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-22 10:16:23 +01:00
|
|
|
}
|
2025-01-01 11:09:27 +01:00
|
|
|
if log_enabled!(Level::Info) {
|
|
|
|
|
processed_vod_info_count += 1;
|
|
|
|
|
let elapsed = start_time.elapsed().as_secs();
|
2025-01-02 15:09:11 +01:00
|
|
|
if elapsed > 0 && ((processed_vod_info_count - last_processed_vod_info_count) > 50) && (elapsed % 30 == 0) {
|
2025-01-01 11:09:27 +01:00
|
|
|
info!("resolved {processed_vod_info_count}/{vod_info_count} vod info");
|
|
|
|
|
last_processed_vod_info_count = processed_vod_info_count;
|
|
|
|
|
}
|
2025-01-01 10:59:51 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if last_processed_vod_info_count != processed_vod_info_count {
|
|
|
|
|
info!("resolved {processed_vod_info_count}/{vod_info_count} vod info");
|
2024-12-19 13:02:10 +01:00
|
|
|
}
|
|
|
|
|
if content_updated {
|
2024-12-23 11:30:33 +01:00
|
|
|
handle_error!(content_writer.flush(),
|
|
|
|
|
|err| errors.push(notify_err!(format!("Failed to resolve vod, could not write to wal file {err}"))));
|
|
|
|
|
handle_error!(record_writer.flush(),
|
|
|
|
|
|err| errors.push(notify_err!(format!("Failed to resolve vod tmdb, could not write to wal file {err}"))));
|
2024-12-25 12:02:00 +01:00
|
|
|
drop(content_writer);
|
2024-12-22 05:00:59 +01:00
|
|
|
drop(record_writer);
|
2024-12-25 12:02:00 +01:00
|
|
|
drop(wal_content_file);
|
|
|
|
|
drop(wal_record_file);
|
|
|
|
|
handle_error!(xtream_update_input_info_file(cfg, fpl.input, &wal_content_path, XtreamCluster::Video).await,
|
2024-12-23 11:30:33 +01:00
|
|
|
|err| errors.push(err));
|
2024-12-25 12:02:00 +01:00
|
|
|
handle_error!(xtream_update_input_vod_record_from_wal_file(cfg, fpl.input, &wal_record_path).await,
|
2024-12-23 11:30:33 +01:00
|
|
|
|err| errors.push(err));
|
2024-12-19 13:02:10 +01:00
|
|
|
}
|
|
|
|
|
}
|