Skip to content

Terminal interface to find, download, and stream movies, TV shows, and live TV using local media players.

refactor(tui): dedupe caption extraction into state helpers

Samir Hossains415ddd4b1174
diff --git a/src/tui/app/playback.rs b/src/tui/app/playback.rs
index a520765..925539f 100644
--- a/src/tui/app/playback.rs
+++ b/src/tui/app/playback.rs
@@ -351,27 +351,7 @@ impl App {
}
Action::ShowSubtitlePopup(link, ext_captions, open_with) => {
self.state.is_resolving_playback = false;
- let mut options = vec![("None".to_string(), "".to_string())];
-
- if let Some(captions_list) =
- ext_captions.get("extCaptions").and_then(|c| c.as_array())
- {
- for cap in captions_list {
- let name = cap
- .get("lanName")
- .and_then(|n| n.as_str())
- .unwrap_or("Unknown")
- .to_string();
- let url = cap
- .get("url")
- .and_then(|u| u.as_str())
- .unwrap_or("")
- .to_string();
- if !url.is_empty() {
- options.push((name, url));
- }
- }
- }
+ let options = crate::tui::state::caption_options(&ext_captions);
if options.len() > 1 {
self.state.show_help = false;
@@ -394,27 +374,7 @@ impl App {
}
Action::ShowDownloadSubtitlePopup(ext_captions) => {
self.state.is_resolving_playback = false;
- let mut options = vec![("None".to_string(), "".to_string())];
-
- if let Some(captions_list) =
- ext_captions.get("extCaptions").and_then(|c| c.as_array())
- {
- for cap in captions_list {
- let name = cap
- .get("lanName")
- .and_then(|n| n.as_str())
- .unwrap_or("Unknown")
- .to_string();
- let url = cap
- .get("url")
- .and_then(|u| u.as_str())
- .unwrap_or("")
- .to_string();
- if !url.is_empty() {
- options.push((name, url));
- }
- }
- }
+ let options = crate::tui::state::caption_options(&ext_captions);
if options.len() > 1 {
self.state.show_help = false;
diff --git a/src/tui/app/requests.rs b/src/tui/app/requests.rs
index 8547fe3..7f0acd2 100644
--- a/src/tui/app/requests.rs
+++ b/src/tui/app/requests.rs
@@ -2129,22 +2129,8 @@ impl App {
if no_pref {
sender.send(Action::ShowDownloadSubtitlePopup(res)).ok();
} else if let Some(pref_lang) = pref {
- let mut sub_url = None;
- if let Some(list) =
- res.get("extCaptions").and_then(|c| c.as_array())
- {
- for sub in list {
- if let (Some(lang), Some(url)) = (
- sub.get("lanName").and_then(|l| l.as_str()),
- sub.get("url").and_then(|u| u.as_str()),
- ) {
- if lang == pref_lang {
- sub_url = Some(url.to_string());
- break;
- }
- }
- }
- }
+ let sub_url =
+ crate::tui::state::caption_url_for(&res, &pref_lang);
sender.send(Action::DownloadStream(sub_url)).ok();
}
} else {
diff --git a/src/tui/state.rs b/src/tui/state.rs
index bfdb07d..4b5ce9a 100644
--- a/src/tui/state.rs
+++ b/src/tui/state.rs
@@ -388,3 +388,38 @@ pub fn stype(value: &serde_json::Value) -> i64 {
.or_else(|| value.get("stype").and_then(|s| s.as_i64()))
.unwrap_or(1)
}
+
+pub fn caption_options(payload: &serde_json::Value) -> Vec<(String, String)> {
+ let mut options = vec![("None".to_string(), "".to_string())];
+ if let Some(list) = payload.get("extCaptions").and_then(|c| c.as_array()) {
+ for cap in list {
+ let name = cap
+ .get("lanName")
+ .and_then(|n| n.as_str())
+ .unwrap_or("Unknown")
+ .to_string();
+ let url = cap
+ .get("url")
+ .and_then(|u| u.as_str())
+ .unwrap_or("")
+ .to_string();
+ if !url.is_empty() {
+ options.push((name, url));
+ }
+ }
+ }
+ options
+}
+
+pub fn caption_url_for(payload: &serde_json::Value, language: &str) -> Option<String> {
+ payload
+ .get("extCaptions")
+ .and_then(|c| c.as_array())
+ .and_then(|list| {
+ list.iter().find_map(|cap| {
+ let lang = cap.get("lanName").and_then(|l| l.as_str())?;
+ let url = cap.get("url").and_then(|u| u.as_str())?;
+ (lang == language).then(|| url.to_string())
+ })
+ })
+}