Skip to content

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

refactor: modularize cache logic, optimize M3U parsing without regex, simplify TUI greetings, and replace dependency-heavy open crate with direct system calls.

Samir Hossainsba5ae3115ea2
diff --git a/Cargo.lock b/Cargo.lock
index 0bc24e1..831b590 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -41,15 +41,6 @@ version = "0.2.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923"
-[[package]]
-name = "android_system_properties"
-version = "0.1.5"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311"
-dependencies = [
- "libc",
-]
-
[[package]]
name = "anyhow"
version = "1.0.104"
@@ -381,19 +372,6 @@ dependencies = [
"rand_core 0.10.1",
]
-[[package]]
-name = "chrono"
-version = "0.4.45"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "1aa79e62e7697b8e29b513a68abacf485adcd1fe8284a4316c5ae868e6633327"
-dependencies = [
- "iana-time-zone",
- "js-sys",
- "num-traits",
- "wasm-bindgen",
- "windows-link",
-]
-
[[package]]
name = "cmake"
version = "0.1.58"
@@ -1393,30 +1371,6 @@ dependencies = [
"tracing",
]
-[[package]]
-name = "iana-time-zone"
-version = "0.1.65"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "e31bc9ad994ba00e440a8aa5c9ef0ec67d5cb5e5cb0cc7f8b744a35b389cc470"
-dependencies = [
- "android_system_properties",
- "core-foundation-sys",
- "iana-time-zone-haiku",
- "js-sys",
- "log",
- "wasm-bindgen",
- "windows-core 0.62.2",
-]
-
-[[package]]
-name = "iana-time-zone-haiku"
-version = "0.1.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f"
-dependencies = [
- "cc",
-]
-
[[package]]
name = "icu_collections"
version = "2.1.1"
@@ -1637,25 +1591,6 @@ version = "2.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d98f6fed1fde3f8c21bc40a1abb88dd75e67924f9cffc3ef95607bad8017f8e2"
-[[package]]
-name = "is-docker"
-version = "0.2.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "928bae27f42bc99b60d9ac7334e3a21d10ad8f1835a4e12ec3ec0464765ed1b3"
-dependencies = [
- "once_cell",
-]
-
-[[package]]
-name = "is-wsl"
-version = "0.4.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "173609498df190136aa7dea1a91db051746d339e18476eed5ca40521f02d7aa5"
-dependencies = [
- "is-docker",
- "once_cell",
-]
-
[[package]]
name = "itertools"
version = "0.14.0"
@@ -1957,7 +1892,6 @@ name = "moviebox-tui"
version = "0.1.6"
dependencies = [
"base64",
- "chrono",
"crossterm",
"dirs",
"futures",
@@ -1965,11 +1899,9 @@ dependencies = [
"image",
"lru",
"md-5",
- "open",
"rand 0.10.2",
"ratatui",
"ratatui-image",
- "regex",
"reqwest",
"self_update",
"serde",
@@ -2124,16 +2056,6 @@ version = "1.21.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"
-[[package]]
-name = "open"
-version = "5.4.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "a0b3d059e795d52b8a72fef45658620edd4d9c359b338564aa14391ffa511ed5"
-dependencies = [
- "is-wsl",
- "libc",
-]
-
[[package]]
name = "openssl-probe"
version = "0.2.1"
@@ -4276,7 +4198,7 @@ version = "0.58.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd04d41d93c4992d421894c18c8b43496aa748dd4c081bac0dc93eb0489272b6"
dependencies = [
- "windows-core 0.58.0",
+ "windows-core",
"windows-targets",
]
@@ -4286,26 +4208,13 @@ version = "0.58.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ba6d44ec8c2591c134257ce647b7ea6b20335bf6379a27dac5f1641fcf59f99"
dependencies = [
- "windows-implement 0.58.0",
- "windows-interface 0.58.0",
- "windows-result 0.2.0",
- "windows-strings 0.1.0",
+ "windows-implement",
+ "windows-interface",
+ "windows-result",
+ "windows-strings",
"windows-targets",
]
-[[package]]
-name = "windows-core"
-version = "0.62.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb"
-dependencies = [
- "windows-implement 0.60.2",
- "windows-interface 0.59.3",
- "windows-link",
- "windows-result 0.4.1",
- "windows-strings 0.5.1",
-]
-
[[package]]
name = "windows-implement"
version = "0.58.0"
@@ -4317,17 +4226,6 @@ dependencies = [
"syn 2.0.119",
]
-[[package]]
-name = "windows-implement"
-version = "0.60.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf"
-dependencies = [
- "proc-macro2",
- "quote",
- "syn 2.0.119",
-]
-
[[package]]
name = "windows-interface"
version = "0.58.0"
@@ -4339,17 +4237,6 @@ dependencies = [
"syn 2.0.119",
]
-[[package]]
-name = "windows-interface"
-version = "0.59.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358"
-dependencies = [
- "proc-macro2",
- "quote",
- "syn 2.0.119",
-]
-
[[package]]
name = "windows-link"
version = "0.2.1"
@@ -4365,34 +4252,16 @@ dependencies = [
"windows-targets",
]
-[[package]]
-name = "windows-result"
-version = "0.4.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5"
-dependencies = [
- "windows-link",
-]
-
[[package]]
name = "windows-strings"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4cd9b125c486025df0eabcb585e62173c6c9eddcec5d117d3b6e8c30e2ee4d10"
dependencies = [
- "windows-result 0.2.0",
+ "windows-result",
"windows-targets",
]
-[[package]]
-name = "windows-strings"
-version = "0.5.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091"
-dependencies = [
- "windows-link",
-]
-
[[package]]
name = "windows-sys"
version = "0.52.0"
diff --git a/Cargo.toml b/Cargo.toml
index bcf152a..e27bf56 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -14,7 +14,6 @@ rust-version = "1.85.0"
[dependencies]
base64 = "0.22.1"
-chrono = "0.4.45"
crossterm = { version = "0.29.0", features = ["event-stream"] }
dirs = "6.0.0"
futures = "0.3.31"
@@ -22,11 +21,9 @@ hmac = "0.13.0"
image = "0.25.10"
lru = "0.18.1"
md-5 = "0.11.0"
-open = "5.4.0"
rand = "0.10.2"
ratatui = "0.30.2"
ratatui-image = { version = "11.0.6", default-features = false, features = ["crossterm", "image-defaults"] }
-regex = "1.13.1"
reqwest = { version = "0.13.4", default-features = false, features = ["json", "rustls"] }
self_update = { version = "0.44.0", default-features = false, features = ["archive-tar", "archive-zip", "rustls", "reqwest"] }
serde = { version = "1.0.228", features = ["derive"] }
diff --git a/src/cache.rs b/src/cache.rs
index 674baad..c7c69fa 100644
--- a/src/cache.rs
+++ b/src/cache.rs
@@ -1,9 +1,29 @@
use std::fs;
use std::path::PathBuf;
-use std::time::SystemTime;
const CACHE_EXPIRY_SECS: u64 = 24 * 60 * 60;
+fn read_json_cache(path: &PathBuf, expiry_secs: u64) -> Option<serde_json::Value> {
+ if path.exists() {
+ if let Ok(metadata) = fs::metadata(path) {
+ if let Ok(modified) = metadata.modified() {
+ if let Ok(elapsed) = modified.elapsed() {
+ if elapsed.as_secs() > expiry_secs {
+ let _ = fs::remove_file(path);
+ return None;
+ }
+ }
+ }
+ }
+ if let Ok(content) = fs::read_to_string(path) {
+ if let Ok(val) = serde_json::from_str(&content) {
+ return Some(val);
+ }
+ }
+ }
+ None
+}
+
pub fn get_cache_dir(subdir: &str) -> PathBuf {
let mut path = dirs::cache_dir().unwrap_or_else(std::env::temp_dir);
path.push("moviebox-tui");
@@ -25,26 +45,10 @@ pub fn get_stream_cache(
season: usize,
episode: usize,
) -> Option<serde_json::Value> {
- let path = get_cache_path(subject_id, season, episode);
- if path.exists() {
- if let Ok(metadata) = fs::metadata(&path) {
- if let Ok(modified) = metadata.modified() {
- if let Ok(duration) = SystemTime::now().duration_since(modified) {
- if duration.as_secs() > CACHE_EXPIRY_SECS {
- let _ = fs::remove_file(&path);
- return None;
- }
- }
- }
- }
-
- if let Ok(content) = fs::read_to_string(&path) {
- if let Ok(val) = serde_json::from_str(&content) {
- return Some(val);
- }
- }
- }
- None
+ read_json_cache(
+ &get_cache_path(subject_id, season, episode),
+ CACHE_EXPIRY_SECS,
+ )
}
pub fn get_details_path(subject_id: &str) -> PathBuf {
@@ -54,26 +58,7 @@ pub fn get_details_path(subject_id: &str) -> PathBuf {
}
pub fn get_details_cache(subject_id: &str) -> Option<serde_json::Value> {
- let path = get_details_path(subject_id);
- if path.exists() {
- if let Ok(metadata) = fs::metadata(&path) {
- if let Ok(modified) = metadata.modified() {
- if let Ok(duration) = SystemTime::now().duration_since(modified) {
- if duration.as_secs() > CACHE_EXPIRY_SECS {
- let _ = fs::remove_file(&path);
- return None;
- }
- }
- }
- }
-
- if let Ok(content) = fs::read_to_string(&path) {
- if let Ok(val) = serde_json::from_str(&content) {
- return Some(val);
- }
- }
- }
- None
+ read_json_cache(&get_details_path(subject_id), CACHE_EXPIRY_SECS)
}
pub fn get_search_path(query: &str) -> PathBuf {
@@ -92,26 +77,7 @@ pub fn get_search_path(query: &str) -> PathBuf {
}
pub fn get_search_cache(query: &str) -> Option<serde_json::Value> {
- let path = get_search_path(query);
- if path.exists() {
- if let Ok(metadata) = fs::metadata(&path) {
- if let Ok(modified) = metadata.modified() {
- if let Ok(duration) = SystemTime::now().duration_since(modified) {
- if duration.as_secs() > CACHE_EXPIRY_SECS {
- let _ = fs::remove_file(&path);
- return None;
- }
- }
- }
- }
-
- if let Ok(content) = fs::read_to_string(&path) {
- if let Ok(val) = serde_json::from_str(&content) {
- return Some(val);
- }
- }
- }
- None
+ read_json_cache(&get_search_path(query), CACHE_EXPIRY_SECS)
}
pub fn set_search_cache(query: &str, data: &serde_json::Value) {
@@ -157,25 +123,7 @@ fn get_homepage_path(tab_id: &str, page: usize) -> PathBuf {
}
pub fn get_homepage_cache(tab_id: &str, page: usize) -> Option<serde_json::Value> {
- let path = get_homepage_path(tab_id, page);
- if path.exists() {
- if let Ok(metadata) = fs::metadata(&path) {
- if let Ok(modified) = metadata.modified() {
- if let Ok(elapsed) = modified.elapsed() {
- if elapsed.as_secs() > 3600 {
- let _ = fs::remove_file(&path);
- return None;
- }
- }
- }
- }
- if let Ok(content) = fs::read_to_string(&path) {
- if let Ok(val) = serde_json::from_str(&content) {
- return Some(val);
- }
- }
- }
- None
+ read_json_cache(&get_homepage_path(tab_id, page), 3600)
}
pub fn set_homepage_cache(tab_id: &str, page: usize, data: &serde_json::Value) {
diff --git a/src/providers/iptv_org/m3u.rs b/src/providers/iptv_org/m3u.rs
index 377f465..461c88a 100644
--- a/src/providers/iptv_org/m3u.rs
+++ b/src/providers/iptv_org/m3u.rs
@@ -1,4 +1,3 @@
-use regex::Regex;
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::PathBuf;
@@ -74,9 +73,15 @@ impl M3UParser {
stream_url: String::new(),
};
- let tvg_id_re = Regex::new(r#"tvg-id="([^"]*)""#).unwrap();
- let tvg_logo_re = Regex::new(r#"tvg-logo="([^"]*)""#).unwrap();
- let group_title_re = Regex::new(r#"group-title="([^"]*)""#).unwrap();
+ let extract_attr = |line: &str, attr: &str| -> String {
+ if let Some(idx) = line.find(attr) {
+ let start = idx + attr.len();
+ if let Some(end) = line[start..].find('"') {
+ return line[start..start + end].to_string();
+ }
+ }
+ String::new()
+ };
for line in content.lines() {
let line = line.trim();
@@ -84,15 +89,9 @@ impl M3UParser {
continue;
}
if line.starts_with("#EXTINF:") {
- if let Some(caps) = tvg_id_re.captures(line) {
- current_channel.id = caps.get(1).map_or("", |m| m.as_str()).to_string();
- }
- if let Some(caps) = tvg_logo_re.captures(line) {
- current_channel.logo = caps.get(1).map_or("", |m| m.as_str()).to_string();
- }
- if let Some(caps) = group_title_re.captures(line) {
- current_channel.group = caps.get(1).map_or("", |m| m.as_str()).to_string();
- }
+ current_channel.id = extract_attr(line, "tvg-id=\"");
+ current_channel.logo = extract_attr(line, "tvg-logo=\"");
+ current_channel.group = extract_attr(line, "group-title=\"");
if let Some(idx) = line.rfind(',') {
current_channel.name = line[idx + 1..].trim().to_string();
diff --git a/src/tui/app.rs b/src/tui/app.rs
index 47b8b73..963fff0 100644
--- a/src/tui/app.rs
+++ b/src/tui/app.rs
@@ -334,23 +334,7 @@ impl App {
if self.state.search_query.is_empty()
&& matches!(self.state.active_screen, crate::tui::state::Screen::Home)
{
- use chrono::Timelike;
- let hour = chrono::Local::now().hour();
- let greeting_time = if (5..12).contains(&hour) {
- "Good morning"
- } else if (12..17).contains(&hour) {
- "Good afternoon"
- } else if (17..21).contains(&hour) {
- "Good evening"
- } else {
- "Working late"
- };
- let punct = if greeting_time == "Working late" {
- "?"
- } else {
- ""
- };
- let greeting = format!("{}, {}{}", greeting_time, self.state.username, punct);
+ let greeting = format!("Welcome back, {}", self.state.username);
let prompts = if self.state.is_tv_mode {
vec![
@@ -1268,7 +1252,18 @@ impl App {
}
if lower_query == "/github" {
- let _ = open::that("https://github.com/mesamirh/MovieBox-Tui");
+ #[cfg(target_os = "windows")]
+ let _ = std::process::Command::new("cmd")
+ .args(["/C", "start", "https://github.com/mesamirh/MovieBox-Tui"])
+ .spawn();
+ #[cfg(target_os = "macos")]
+ let _ = std::process::Command::new("open")
+ .arg("https://github.com/mesamirh/MovieBox-Tui")
+ .spawn();
+ #[cfg(all(target_os = "linux", not(target_os = "android")))]
+ let _ = std::process::Command::new("xdg-open")
+ .arg("https://github.com/mesamirh/MovieBox-Tui")
+ .spawn();
self.state.search_query.clear();
self.state.input_mode = InputMode::Normal;
return None;