Terminal interface to find, download, and stream movies, TV shows, and live TV using local media players.
fix(providers): cache detail payloads across stream fetches and stream 4khdhub mirror probes
Samir Hossainscaec4679725d
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 0d10903..ed84a89 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -16,8 +16,10 @@- Reported specific subtitle and DASH download errors (HTTP status, timeout, missing `yt-dlp` vs `ffmpeg`) and matched `"at"` after `"of"` in `yt-dlp` progress lines.- **Providers & Disk Cache**:- Removed the broad `"notice"` substring filter in MovieBox stream adaptation while keeping `/notice.mp4` and known deprecation hashes, which restores streams for titles like *Red Notice* and *Burn Notice*.+ - Advanced the active MovieBox host index immediately on connection or HTTP retry errors, removed the duplicate `/subject-api/resource` preflight round-trip in `InitStreamPool`, and removed the 120ms delay on cached episode switches.+ - Streamed 4KHDHub mirror resolution directly into preflight probes via `FuturesUnordered` without waiting for slow mirrors, cached fetched detail pages across `details` and `releases` in 4KHDHub, Dramachi, and CircleFTP, and bounded CircleFTP `HEAD` size probes to 1.2 seconds.- Required a leading `'S'` prefix in 4KHDHub season count parsing so release years after `"Score"` do not parse as season counts, and cached static HTML selectors in 4KHDHub parsers.- - Matched Dramachi episode numbers across Unicode boundaries and quality suffixes, and inserted `/manifest.json` before query parameters in Stremio addon URLs.+ - Matched Dramachi episode numbers across Unicode boundaries and quality suffixes, parallelized Stremio addon movie/series catalog searches and M3U playlist loads, and inserted `/manifest.json` before query parameters in addon URLs.- Evicted cached poster images only when file age exceeds the 30-day TTL so valid cache files survive minor clock skew.- Replaced synchronous `fsync` barriers on ephemeral cache writes with atomic temp-file renames, cached default directory paths via `LazyLock`, reused a static HTTP client in `probe_url`, and shared `Arc<[u8]>` poster buffers across disk caching and decoding.- **TUI Navigation, Themes, Modals & Mouse**:diff --git a/src/providers/addons/mod.rs b/src/providers/addons/mod.rsindex 8e66a6b..99dd745 100644--- a/src/providers/addons/mod.rs+++ b/src/providers/addons/mod.rs@@ -47,16 +47,14 @@ impl Provider for AddonClient {let mut combined = Vec::new();for addon in catalog_addons {let base_url = Self::base_addon_url(&addon.manifest_url);- if let Ok(movies) = self- .fetch_catalog_search(&base_url, "movie", "top", query)- .await- {+ let (movies_res, series_res) = tokio::join!(+ self.fetch_catalog_search(&base_url, "movie", "top", query),+ self.fetch_catalog_search(&base_url, "series", "top", query),+ );+ if let Ok(movies) = movies_res {combined.extend(movies);}- if let Ok(series) = self- .fetch_catalog_search(&base_url, "series", "top", query)- .await- {+ if let Ok(series) = series_res {combined.extend(series);}if !combined.is_empty() {diff --git a/src/providers/bdix/circleftp/client.rs b/src/providers/bdix/circleftp/client.rsindex 466850f..dabcfa5 100644--- a/src/providers/bdix/circleftp/client.rs+++ b/src/providers/bdix/circleftp/client.rs@@ -16,10 +16,14 @@ pub enum CircleFtpError {Parse(String),}+type PostCache =+ std::sync::Arc<std::sync::Mutex<Option<(String, std::time::Instant, serde_json::Value)>>>;+#[derive(Clone)]pub struct CircleFtpClient {client: reqwest::Client,base_url: String,+ post_cache: PostCache,}impl Default for CircleFtpClient {@@ -38,7 +42,25 @@ impl CircleFtpClient {Self {client: build_client(),base_url: API_URL.to_string(),+ post_cache: std::sync::Arc::new(std::sync::Mutex::new(None)),+ }+ }++ async fn fetch_post(&self, id: &str) -> Result<serde_json::Value, CircleFtpError> {+ if let Ok(lock) = self.post_cache.lock()+ && let Some((cached_id, ts, val)) = lock.as_ref()+ && cached_id == id+ && ts.elapsed() < Duration::from_secs(60)+ {+ return Ok(val.clone());+ }+ let url = format!("{}/posts/{}", self.base_url, id);+ let resp = self.client.get(&url).send().await?.error_for_status()?;+ let json: serde_json::Value = resp.json().await?;+ if let Ok(mut lock) = self.post_cache.lock() {+ *lock = Some((id.to_string(), std::time::Instant::now(), json.clone()));}+ Ok(json)}pub async fn search(&self, query: &str) -> Result<Vec<CatalogItem>, CircleFtpError> {@@ -55,9 +77,7 @@ impl CircleFtpClient {}pub async fn details(&self, id: &str) -> Result<MediaDetails, CircleFtpError> {- let url = format!("{}/posts/{}", self.base_url, id);- let resp = self.client.get(&url).send().await?.error_for_status()?;- let json: serde_json::Value = resp.json().await?;+ let json = self.fetch_post(id).await?;let title = json.get("title")@@ -192,11 +212,10 @@ impl CircleFtpClient {}async fn fetch_size(&self, link: &str) -> Option<u64> {- self.client- .head(link)- .send()+ tokio::time::timeout(Duration::from_millis(1200), self.client.head(link).send()).await.ok()?+ .ok()?.headers().get(reqwest::header::CONTENT_LENGTH)?.to_str()@@ -211,9 +230,7 @@ impl CircleFtpClient {season: Option<usize>,episode: Option<usize>,) -> Result<Vec<Release>, CircleFtpError> {- let url = format!("{}/posts/{}", self.base_url, id);- let resp = self.client.get(&url).send().await?;- let json: serde_json::Value = resp.json().await?;+ let json = self.fetch_post(id).await?;let mut releases = Vec::new();let r#type = json.get("type").and_then(|v| v.as_str()).unwrap_or("");diff --git a/src/providers/dramachi/client.rs b/src/providers/dramachi/client.rsindex 66badad..3229a0a 100644--- a/src/providers/dramachi/client.rs+++ b/src/providers/dramachi/client.rs@@ -33,10 +33,21 @@ impl From<DramachiError> for ProviderError {}}+type TitleCache = std::sync::Arc<+ std::sync::Mutex<Option<(String, std::time::Instant, DramachiTitleDetailsResponse)>>,+>;+type EpListCache = std::sync::Arc<+ std::sync::Mutex<+ std::collections::HashMap<(String, String), (std::time::Instant, Vec<DramachiEpisodeItem>)>,+ >,+>;+#[derive(Debug, Clone)]pub struct DramachiClient {client: Client,base_url: String,+ title_cache: TitleCache,+ eplist_cache: EpListCache,}impl Default for DramachiClient {@@ -47,14 +58,7 @@ impl Default for DramachiClient {impl DramachiClient {pub fn new() -> Self {- let client = crate::net::http_client_builder()- .timeout(Duration::from_secs(15))- .build()- .unwrap_or_default();- Self {- client,- base_url: DEFAULT_BASE_URL.to_string(),- }+ Self::with_base_url(DEFAULT_BASE_URL)}pub fn with_base_url(base_url: impl Into<String>) -> Self {@@ -65,9 +69,44 @@ impl DramachiClient {Self {client,base_url: base_url.into(),+ title_cache: std::sync::Arc::new(std::sync::Mutex::new(None)),+ eplist_cache: std::sync::Arc::new(std::sync::Mutex::new(+ std::collections::HashMap::new(),+ )),}}+ async fn fetch_title_details(+ &self,+ title_id: &str,+ ) -> Result<DramachiTitleDetailsResponse, DramachiError> {+ if let Ok(lock) = self.title_cache.lock()+ && let Some((cached_id, ts, data)) = lock.as_ref()+ && cached_id == title_id+ && ts.elapsed() < Duration::from_secs(60)+ {+ return Ok(data.clone());+ }+ let url = format!(+ "{}?interface=title_v2&id={}",+ self.base_url,+ encode_param(title_id)+ );+ let resp = self.client.get(&url).send().await?.error_for_status()?;+ let data: DramachiTitleDetailsResponse = resp+ .json()+ .await+ .map_err(|e| DramachiError::Parsing(format!("failed to parse title details: {e}")))?;+ if let Ok(mut lock) = self.title_cache.lock() {+ *lock = Some((+ title_id.to_string(),+ std::time::Instant::now(),+ data.clone(),+ ));+ }+ Ok(data)+ }+pub async fn search(&self,query: &str,@@ -138,16 +177,7 @@ impl DramachiClient {return Err(DramachiError::NotFound);}- let url = format!(- "{}?interface=title_v2&id={}",- self.base_url,- encode_param(title_id)- );- let resp = self.client.get(&url).send().await?.error_for_status()?;- let data: DramachiTitleDetailsResponse = resp- .json()- .await- .map_err(|e| DramachiError::Parsing(format!("failed to parse title details: {e}")))?;+ let data = self.fetch_title_details(title_id).await?;let album = data.album@@ -344,6 +374,13 @@ impl DramachiClient {title_id: &str,rip: &str,) -> Result<Vec<DramachiEpisodeItem>, DramachiError> {+ let key = (title_id.to_string(), rip.to_string());+ if let Ok(lock) = self.eplist_cache.lock()+ && let Some((ts, cached)) = lock.get(&key)+ && ts.elapsed() < Duration::from_secs(60)+ {+ return Ok(cached.clone());+ }let url = format!("{}?interface=eplist&season={}&id={}",self.base_url,@@ -359,6 +396,12 @@ impl DramachiClient {let mut episodes = data.episode_list.unwrap_or_default();episodes.sort_by_key(|ep| parse_episode_number(&ep.f_title).unwrap_or(usize::MAX));+ if let Ok(mut lock) = self.eplist_cache.lock() {+ if lock.len() >= 16 {+ lock.clear();+ }+ lock.insert(key, (std::time::Instant::now(), episodes.clone()));+ }Ok(episodes)}@@ -427,15 +470,7 @@ impl DramachiClient {let rip_to_query = if let Some(rip) = target_rip {rip.to_string()} else {- let details_url = format!(- "{}?interface=title_v2&id={}",- self.base_url,- encode_param(title_id)- );- let resp = self.client.get(&details_url).send().await?;- let data: DramachiTitleDetailsResponse = resp.json().await.map_err(|e| {- DramachiError::Parsing(format!("failed to parse title details: {e}"))- })?;+ let data = self.fetch_title_details(title_id).await?;let mut resolved_rip = None;if let Some(seasons_map) = &data.seasons {diff --git a/src/providers/fourkhdhub/client.rs b/src/providers/fourkhdhub/client.rsindex a93dd46..ef46b91 100644--- a/src/providers/fourkhdhub/client.rs+++ b/src/providers/fourkhdhub/client.rs@@ -54,10 +54,13 @@ impl FourKHdHubError {}}+type PageCache = std::sync::Arc<std::sync::Mutex<Option<(String, std::time::Instant, String)>>>;+#[derive(Clone)]pub struct FourKHdHubClient {client: reqwest::Client,base_url: Url,+ page_cache: PageCache,}impl FourKHdHubClient {@@ -76,6 +79,7 @@ impl FourKHdHubClient {Ok(Self {client: build_client(),base_url,+ page_cache: std::sync::Arc::new(std::sync::Mutex::new(None)),})}@@ -97,9 +101,25 @@ impl FourKHdHubClient {parser::parse_search(&self.base_url, &html)}- pub async fn details(&self, id: &str) -> Result<MediaDetails, FourKHdHubError> {+ async fn fetch_cached_page(&self, id: &str) -> Result<String, FourKHdHubError> {let url = self.provider_url(id)?;+ let key = url.as_str().to_string();+ if let Ok(lock) = self.page_cache.lock()+ && let Some((cached_key, ts, html)) = lock.as_ref()+ && cached_key == &key+ && ts.elapsed() < std::time::Duration::from_secs(60)+ {+ return Ok(html.clone());+ }let html = self.fetch_text(url).await?;+ if let Ok(mut lock) = self.page_cache.lock() {+ *lock = Some((key, std::time::Instant::now(), html.clone()));+ }+ Ok(html)+ }++ pub async fn details(&self, id: &str) -> Result<MediaDetails, FourKHdHubError> {+ let html = self.fetch_cached_page(id).await?;parser::parse_details(id, &html)}@@ -109,8 +129,7 @@ impl FourKHdHubClient {season: usize,episode: usize,) -> Result<Vec<Release>, FourKHdHubError> {- let url = self.provider_url(id)?;- let html = self.fetch_text(url).await?;+ let html = self.fetch_cached_page(id).await?;parser::parse_releases(&html, season, episode)}@@ -125,12 +144,14 @@ impl FourKHdHubClient {));}let referer = self.base_url.as_str().trim_end_matches('/').to_string();- let fetch_futures = release.mirrors.iter().map(|mirror| {+ use futures::StreamExt;+ let mut resolve_tasks = futures::stream::FuturesUnordered::new();+ for mirror in &release.mirrors {let client = self.client.clone();let mirror_url = mirror.resolver_url.clone();let mirror_label = mirror.label.clone();let mirror_headers = mirror.headers.clone();- async move {+ resolve_tasks.push(async move {let fetch = async {if mirror_url.contains("hubcloud.") {hubcloud::resolve(&client, &mirror_url, intent).await@@ -151,78 +172,85 @@ impl FourKHdHubClient {FourKHdHubError::NoPlayableMirror("mirror resolver timed out".into())}).and_then(|res| res)- }- });- let mirror_results = futures::future::join_all(fetch_futures).await;-- let mut candidates = Vec::new();- for cand_list in mirror_results.into_iter().flatten() {- for (url, label, headers) in cand_list {- let score = hubcloud::score(&url, &label, intent);- candidates.push((score, url, label, headers));- }+ });}- candidates.sort_by_key(|cand| cand.0);- let mut unique_candidates = Vec::new();++ let mut preflight_tasks = futures::stream::FuturesUnordered::new();let mut seen = std::collections::HashSet::new();- for (cand_score, url, label, headers) in candidates {- if seen.insert(url.clone()) {- unique_candidates.push((cand_score, url, label, headers));+ let mut spawned_preflights = 0usize;+ let mut fallbacks = Vec::new();++ loop {+ tokio::select! {+ Some(resolve_res) = resolve_tasks.next(), if !resolve_tasks.is_empty() => {+ if let Ok(mut cand_list) = resolve_res {+ cand_list.sort_by_key(|(url, label, _)| hubcloud::score(url, label, intent));+ for (url, label, headers) in cand_list {+ if spawned_preflights >= 6 || !seen.insert(url.clone()) {+ continue;+ }+ spawned_preflights += 1;+ let cand_score = hubcloud::score(&url, &label, intent);+ let this = self.clone();+ let mut merged = headers;+ if !merged+ .iter()+ .any(|(name, _)| name.eq_ignore_ascii_case("referer"))+ {+ merged.push(("Referer".to_string(), referer.clone()));+ }+ if !merged+ .iter()+ .any(|(name, _)| name.eq_ignore_ascii_case("user-agent"))+ {+ merged.push(("User-Agent".to_string(), BROWSER_UA.to_string()));+ }+ preflight_tasks.push(async move {+ let (playable_url, is_seekable) =+ this.preflight_with_seekable(&url, &merged).await?;+ let resolved_score =+ cand_score.min(hubcloud::score(&playable_url, &label, intent));+ Ok::<_, FourKHdHubError>((+ is_seekable,+ resolved_score,+ playable_url,+ label,+ merged,+ ))+ });+ }+ }+ }+ Some(preflight_res) = preflight_tasks.next(), if !preflight_tasks.is_empty() => {+ if let Ok((is_seekable, resolved_score, playable_url, label, headers)) =+ preflight_res+ {+ if is_seekable && resolved_score <= 1 {+ log::info!(+ "4KHDHub seekable mirror playable: {label} ({})",+ crate::logging::sanitize_url(&playable_url)+ );+ return Ok(PlaybackSource {+ provider: ProviderKind::FourKHdHub,+ url: playable_url,+ headers,+ subtitle: None,+ source_label: label,+ max_height: None,+ });+ }+ fallbacks.push((is_seekable, resolved_score, playable_url, label, headers));+ }+ }+ else => break,}}- if unique_candidates.is_empty() {+ if spawned_preflights == 0 {return Err(FourKHdHubError::NoPlayableMirror("no working mirror links extracted".into(),));}-- use futures::StreamExt;- let mut preflight_tasks = futures::stream::FuturesUnordered::new();- for (cand_score, url, label, headers) in unique_candidates.into_iter().take(6) {- let this = self.clone();- let mut merged = headers;- if !merged- .iter()- .any(|(name, _)| name.eq_ignore_ascii_case("referer"))- {- merged.push(("Referer".to_string(), referer.clone()));- }- if !merged- .iter()- .any(|(name, _)| name.eq_ignore_ascii_case("user-agent"))- {- merged.push(("User-Agent".to_string(), BROWSER_UA.to_string()));- }- preflight_tasks.push(async move {- let (playable_url, is_seekable) =- this.preflight_with_seekable(&url, &merged).await?;- let resolved_score = cand_score.min(hubcloud::score(&playable_url, &label, intent));- Ok::<_, FourKHdHubError>((is_seekable, resolved_score, playable_url, label, merged))- });- }-- let mut fallbacks = Vec::new();- while let Some(res) = preflight_tasks.next().await {- if let Ok((is_seekable, resolved_score, playable_url, label, headers)) = res {- if is_seekable && resolved_score <= 1 {- log::info!(- "4KHDHub seekable mirror playable: {label} ({})",- crate::logging::sanitize_url(&playable_url)- );- return Ok(PlaybackSource {- provider: ProviderKind::FourKHdHub,- url: playable_url,- headers,- subtitle: None,- source_label: label,- max_height: None,- });- }- fallbacks.push((is_seekable, resolved_score, playable_url, label, headers));- }- }-fallbacks.sort_by_key(|(is_seekable, resolved_score, _, _, _)| (!*is_seekable, *resolved_score));if let Some((_, _, playable_url, label, headers)) = fallbacks.into_iter().next() {diff --git a/src/providers/moviebox/client.rs b/src/providers/moviebox/client.rsindex 1db6811..defc6bc 100644--- a/src/providers/moviebox/client.rs+++ b/src/providers/moviebox/client.rs@@ -277,6 +277,8 @@ impl MovieBoxClient {"moviebox host {idx} returned retryable status {status}: {}",crate::logging::sanitize_url(&url));+ self.active_base_idx+ .store((idx + 1) % HOST_POOL.len(), Ordering::Relaxed);if status == 429 {backoff_ms = resp.headers()@@ -307,6 +309,8 @@ impl MovieBoxClient {"moviebox host {idx} request failed: {error} [{}]",crate::logging::sanitize_url(&url));+ self.active_base_idx+ .store((idx + 1) % HOST_POOL.len(), Ordering::Relaxed);continue;}}diff --git a/src/providers/tv/parser.rs b/src/providers/tv/parser.rsindex b19c845..500bea4 100644--- a/src/providers/tv/parser.rs+++ b/src/providers/tv/parser.rs@@ -29,7 +29,7 @@ impl M3UParser {pub async fn fetch_playlist(&self,url: &str,- ) -> Result<Vec<Channel>, Box<dyn std::error::Error>> {+ ) -> Result<Vec<Channel>, Box<dyn std::error::Error + Send + Sync>> {let trimmed = url.trim().trim_matches(|c| c == '"' || c == '\'').trim();let is_remote = crate::net::is_http_url(trimmed);let content = if is_remote {diff --git a/src/tui/app/mod.rs b/src/tui/app/mod.rsindex a0098e8..b71379b 100644--- a/src/tui/app/mod.rs+++ b/src/tui/app/mod.rs@@ -425,10 +425,21 @@ impl App {let sender = self.action_sender.clone();handle.spawn(async move {let parser = crate::providers::tv::M3UParser::new();+ let futs = playlists.iter().map(|source| {+ let parser = &parser;+ async move {+ let res = parser+ .fetch_playlist(source)+ .await+ .map_err(|e| e.to_string());+ (source, res)+ }+ });+ let results = futures::future::join_all(futs).await;let mut all_channels = Vec::new();let mut failed = 0usize;- for source in &playlists {- match parser.fetch_playlist(source).await {+ for (source, res) in results {+ match res {Ok(channels) => all_channels.extend(channels),Err(error) => {failed += 1;diff --git a/src/tui/app/navigation.rs b/src/tui/app/navigation.rsindex 9438cd7..1dfdc7e 100644--- a/src/tui/app/navigation.rs+++ b/src/tui/app/navigation.rs@@ -307,14 +307,11 @@ impl App {context.provider = self.provider_for_subject(&id);let request_id = self.state.active_resource_request;self.request_tasks.cancel_episode_prefetch();- self.request_tasks.episode_prefetch = Some(tokio::spawn(async move {- tokio::time::sleep(std::time::Duration::from_millis(120)).await;- sender- .send(Action::EpisodeStreamsReady(- context, request_id, id, se, ep, streams,- ))- .ok();- }));+ sender+ .send(Action::EpisodeStreamsReady(+ context, request_id, id, se, ep, streams,+ ))+ .ok();} else {self.state.selected_resources.clear();self.state.is_loading = true;diff --git a/src/tui/app/requests.rs b/src/tui/app/requests.rsindex acd5ae4..3877b62 100644--- a/src/tui/app/requests.rs+++ b/src/tui/app/requests.rs@@ -1152,24 +1152,10 @@ impl App {self.state.is_fetching_streams = true;self.state.has_streams_settled = false;self.state.stream_error = None;- if self.provider_for_subject(&subject_id) != ProviderKind::MovieBox {- self.state- .stream_pool- .insert(subject_id.clone(), Default::default());- self.trigger_episode_fetch();- return None;- }- let service = self.service.clone();- let sender = self.action_sender.clone();- self.request_tasks.spawn_stream_pool_init(async move {- let resolutions = service- .fetch_collection_resolutions(&subject_id)- .await- .unwrap_or_default();- sender- .send(Action::StreamPoolInitialized(subject_id, resolutions))- .ok();- });+ self.request_tasks.cancel_stream_pool_init();+ self.action_sender+ .send(Action::StreamPoolInitialized(subject_id, Vec::new()))+ .ok();}Action::StreamPoolInitialized(subject_id, resolutions) => {@@ -1471,22 +1457,17 @@ impl App {.or_default();if !force_refresh {if let Some(cached) = pool.episode_index.get(&(season, episode)) {- let sender = self.action_sender.clone();- let cached = cached.clone();- let cached_subject_id = subject_id.clone();- self.request_tasks.spawn_streams(async move {- tokio::time::sleep(std::time::Duration::from_millis(120)).await;- sender- .send(Action::EpisodeStreamsReady(- context,- request_id,- cached_subject_id,- season,- episode,- cached,- ))- .ok();- });+ self.request_tasks.cancel_streams();+ self.action_sender+ .send(Action::EpisodeStreamsReady(+ context,+ request_id,+ subject_id.clone(),+ season,+ episode,+ cached.clone(),+ ))+ .ok();return None;}}