diff --git a/Cargo.lock b/Cargo.lock index 27bc8ae..07966ba 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1311,6 +1311,7 @@ dependencies = [ "ratatui-image", "serde", "serde_json", + "time", "toml_edit", ] diff --git a/Cargo.toml b/Cargo.toml index 98d7cd4..92ffea3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ ratatui-image = { version = "11.1.0", default-features = false, features = ["cro serde = { version = "1.0.229", features = ["derive"] } serde_json = "1.0.151" toml_edit = "0.25.17" +time = "0.3.55" [target."cfg(unix)".dependencies] libc = "0.2.190" diff --git a/src/main.rs b/src/main.rs index 087a3ba..8dd1047 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,6 +36,8 @@ //! Switching views cancels in-flight extraction; returning resumes the same pending range. //! Errors keep loaded rows and stop automatic retries; r reloads config and restarts the current view. //! Deep pages may slow down because yt-dlp offsets re-traverse earlier continuation pages. +//! Video tables show Published as YYYY-MM-DD; ~ marks estimated UTC dates, Unknown missing metadata. +//! Prefer exact upload dates and cache date labels during extraction, not on every redraw. //! IINA receives `--stdin`; other players must read standard input when passed `-`. //! Audio and video are streamed through a pipe, never saved to a video file. //! See `--help` for controls; replacing playback or quitting cancels its processes. @@ -71,7 +73,7 @@ use youtube::{Job, PAGE_SIZE, Page, Playback, Video}; #[command( version, about = "Search YouTube and stream videos to your chosen player", - after_help = "Requires yt-dlp, FFmpeg, and a player on PATH; thumbnails also use curl.\nDefaults to IINA on macOS and mpv elsewhere; --player overrides the default.\nIINA receives '--stdin'; other players receive '-' to read audio/video from standard input.\nWithout a CLI query, start in INSERT; a CLI query is searched automatically in NORMAL.\nINSERT: type to edit; Enter submits if changed and returns to NORMAL; Esc returns without submitting.\nTEXT EDITING (search and new tag): Option/Alt+Backspace or Ctrl-W deletes the previous whitespace-delimited word, including trailing whitespace; Cmd/Super+Backspace or Ctrl-U clears the entire line. Plain Backspace removes one character.\nThe terminal must transmit modified Backspace. If it intercepts Option/Cmd, map those shortcuts to Ctrl-W/Ctrl-U respectively.\nNORMAL: j/k or arrows select; Enter streams; i edits the query; d toggles description; t toggles thumbnail; c edits channel tags; q quits.\nTab/Shift-Tab switch Search, Feed, then tag views; r reloads config and refreshes the current view from its first page.\nLOADING: Result/feed and preview titles show an animated spinner while content loads, including further pages and thumbnail decoding/resizing; it disappears when ready or on error.\nFeed sits immediately right of Search and combines every saved [channels] registration, including untagged channels, plus channels entered directly in tags.\nChannel URLs and videos are deduplicated; Feed keeps its cached selection and page position when only tag assignments change.\nSubscriptions: $XDG_CONFIG_HOME/tubular/config.toml, or ~/.config/tubular/config.toml when XDG_CONFIG_HOME is unset/empty/relative.\nExample TOML:\n[categories]\nCreators = [\"@jawed\"]\nFavorites = [\"@jawed\"]\n[channels]\n\"@jawed\" = \"https://www.youtube.com/channel/UC4QobU6STFB0P71PMvOGN5A\"\n[categories] defines flat, overlapping tags, each with its own feed; there is no hierarchy or inheritance.\nEntries accept @handles, HTTPS YouTube channel URLs, or canonical UC channel IDs.\n[channels] registers saved handles with canonical channel URLs/IDs; feeds use these stable identities.\nChecked tags save the handle plus its mapping; existing URLs/aliases are replaced with the handle, preserving comments.\nUnchecked tags remove only that channel's tag assignment; other creators, omitted tags, empty tag arrays, and saved registrations are retained.\nUntagging does not unsubscribe from Feed. To unsubscribe, remove all of that channel's [channels] mappings and tag entries, then press r.\nUnmapped handles work directly. Existing URL entries still load.\nSaving requires a handle and canonical ID; unavailable metadata or conflicting mappings are reported without changing the file.\nPAGINATION: Search loads 20 results per page; Feed and tags request 20 uploads per channel's /videos tab per page.\nMoving near the loaded end in NORMAL fetches more asynchronously, preserving existing rows, selection, and previews. Insert mode and open popups pause automatic prefetch.\nFeed views merge newest-first using approximate upload dates. Older uploads wait while newer channel pages catch up; undated uploads appear when the feed is exhausted.\nStatus shows the page and whether more is available or the end is reached. Switching views cancels pending extraction; returning resumes the same range with cached rows intact.\nPage errors retain loaded rows and stop automatic retries; r reloads config and restarts the current view from page one.\nDeep pages may take longer because yt-dlp offsets re-traverse earlier continuation pages.\nTAGS: existing assignments are checked; j/k or arrows move; Space toggles; Enter saves all staged memberships; n prompts for a new tag; Esc cancels without writing.\nNEW TAG: type a name; Enter stages it checked and returns to the list; Esc abandons the name; Ctrl-U clears.\nNew tags and membership changes reach disk only on Enter in the main tag list.\nOpening the description fetches full video metadata, preserving paragraphs and caching it for these results.\nPreview windows use up to 70% width and 60% height, capped at 96 by 20 cells.\nIn descriptions, Up/Down scroll lines and PgUp/PgDn scroll pages; j/k still selects videos.\nEsc closes the description; i closes it to edit the query. Scrolling stays within the text.\nTHUMBNAIL: t or Esc closes; j/k or arrows load the selected video's image; d switches to description.\nThumbnails are fetched lazily into memory and fit proportionally inside the popup on resize.\nTerminal graphics are detected at startup, with colored half-block rendering when image protocols are unavailable.\nDownloading, JPEG decoding, and image resizing/encoding do not block navigation.\ni, c, view changes, and refresh dismiss the thumbnail; typing t in INSERT or a tag name edits text.\nCtrl-C quits in any mode or popup.\nSelecting another video replaces playback. Quitting stops playback.\nExample: tubular --player mpv --player-arg=--volume=50 \"rust programming\"" + after_help = "Requires yt-dlp, FFmpeg, and a player on PATH; thumbnails also use curl.\nDefaults to IINA on macOS and mpv elsewhere; --player overrides the default.\nIINA receives '--stdin'; other players receive '-' to read audio/video from standard input.\nWithout a CLI query, start in INSERT; a CLI query is searched automatically in NORMAL.\nINSERT: type to edit; Enter submits if changed and returns to NORMAL; Esc returns without submitting.\nTEXT EDITING (search and new tag): Option/Alt+Backspace or Ctrl-W deletes the previous whitespace-delimited word, including trailing whitespace; Cmd/Super+Backspace or Ctrl-U clears the entire line. Plain Backspace removes one character.\nThe terminal must transmit modified Backspace. If it intercepts Option/Cmd, map those shortcuts to Ctrl-W/Ctrl-U respectively.\nNORMAL: j/k or arrows select; Enter streams; i edits the query; d toggles description; t toggles thumbnail; c edits channel tags; q quits.\nTab/Shift-Tab switch Search, Feed, then tag views; r reloads config and refreshes the current view from its first page.\nPUBLISHED: Every video view shows YYYY-MM-DD. Exact upload dates take precedence; ~ marks an estimated UTC date from flat metadata, and Unknown means unavailable. Dates are cached without fetching full metadata for every video.\nLOADING: Result/feed and preview titles show an animated spinner while content loads, including further pages and thumbnail decoding/resizing; it disappears when ready or on error.\nFeed sits immediately right of Search and combines every saved [channels] registration, including untagged channels, plus channels entered directly in tags.\nChannel URLs and videos are deduplicated; Feed keeps its cached selection and page position when only tag assignments change.\nSubscriptions: $XDG_CONFIG_HOME/tubular/config.toml, or ~/.config/tubular/config.toml when XDG_CONFIG_HOME is unset/empty/relative.\nExample TOML:\n[categories]\nCreators = [\"@jawed\"]\nFavorites = [\"@jawed\"]\n[channels]\n\"@jawed\" = \"https://www.youtube.com/channel/UC4QobU6STFB0P71PMvOGN5A\"\n[categories] defines flat, overlapping tags, each with its own feed; there is no hierarchy or inheritance.\nEntries accept @handles, HTTPS YouTube channel URLs, or canonical UC channel IDs.\n[channels] registers saved handles with canonical channel URLs/IDs; feeds use these stable identities.\nChecked tags save the handle plus its mapping; existing URLs/aliases are replaced with the handle, preserving comments.\nUnchecked tags remove only that channel's tag assignment; other creators, omitted tags, empty tag arrays, and saved registrations are retained.\nUntagging does not unsubscribe from Feed. To unsubscribe, remove all of that channel's [channels] mappings and tag entries, then press r.\nUnmapped handles work directly. Existing URL entries still load.\nSaving requires a handle and canonical ID; unavailable metadata or conflicting mappings are reported without changing the file.\nPAGINATION: Search loads 20 results per page; Feed and tags request 20 uploads per channel's /videos tab per page.\nMoving near the loaded end in NORMAL fetches more asynchronously, preserving existing rows, selection, and previews. Insert mode and open popups pause automatic prefetch.\nFeed views merge newest-first using approximate upload dates. Older uploads wait while newer channel pages catch up; undated uploads appear when the feed is exhausted.\nStatus shows the page and whether more is available or the end is reached. Switching views cancels pending extraction; returning resumes the same range with cached rows intact.\nPage errors retain loaded rows and stop automatic retries; r reloads config and restarts the current view from page one.\nDeep pages may take longer because yt-dlp offsets re-traverse earlier continuation pages.\nTAGS: existing assignments are checked; j/k or arrows move; Space toggles; Enter saves all staged memberships; n prompts for a new tag; Esc cancels without writing.\nNEW TAG: type a name; Enter stages it checked and returns to the list; Esc abandons the name; Ctrl-U clears.\nNew tags and membership changes reach disk only on Enter in the main tag list.\nOpening the description fetches full video metadata, preserving paragraphs and caching it for these results.\nPreview windows use up to 70% width and 60% height, capped at 96 by 20 cells.\nIn descriptions, Up/Down scroll lines and PgUp/PgDn scroll pages; j/k still selects videos.\nEsc closes the description; i closes it to edit the query. Scrolling stays within the text.\nTHUMBNAIL: t or Esc closes; j/k or arrows load the selected video's image; d switches to description.\nThumbnails are fetched lazily into memory and fit proportionally inside the popup on resize.\nTerminal graphics are detected at startup, with colored half-block rendering when image protocols are unavailable.\nDownloading, JPEG decoding, and image resizing/encoding do not block navigation.\ni, c, view changes, and refresh dismiss the thumbnail; typing t in INSERT or a tag name edits text.\nCtrl-C quits in any mode or popup.\nSelecting another video replaces playback. Quitting stops playback.\nExample: tubular --player mpv --player-arg=--volume=50 \"rust programming\"" )] struct Options { /// Player executable (IINA or a player supporting '-' for standard input). @@ -895,11 +897,16 @@ impl App { Row::new([ video.title.as_str(), video.channel.as_deref().unwrap_or("Unknown"), + video.publication_date.as_deref().unwrap_or("Unknown"), ]) }), - [Constraint::Percentage(75), Constraint::Percentage(25)], + [ + Constraint::Fill(3), + Constraint::Fill(1), + Constraint::Length(11), + ], ) - .header(Row::new(["Video", "Channel"]).bold()) + .header(Row::new(["Video", "Channel", "Published"]).bold()) .block(Block::bordered().title(loading_title( if self.view_index == 0 { " Results " @@ -1614,6 +1621,7 @@ mod tests { channel_id: None, uploader_id: None, timestamp: None, + publication_date: None, description: None, }, Video { @@ -1623,6 +1631,7 @@ mod tests { channel_id: None, uploader_id: None, timestamp: None, + publication_date: None, description: None, }, ]; diff --git a/src/youtube.rs b/src/youtube.rs index 8d0b8b7..7b3e9f7 100644 --- a/src/youtube.rs +++ b/src/youtube.rs @@ -24,6 +24,9 @@ pub struct Video { pub channel_id: Option, pub uploader_id: Option, pub timestamp: Option, + /// Cached YYYY-MM-DD label; a leading ~ marks an estimated UTC date. + #[serde(default, rename = "upload_date")] + pub publication_date: Option, // Search descriptions are truncated; only full video metadata populates this cache. #[serde(skip)] pub description: Option, @@ -89,6 +92,24 @@ fn validate_video(video: &mut Video) -> Result<(), String> { if let Some(channel) = &mut video.channel { channel.retain(|c| !c.is_control()); } + let exact_date = video.publication_date.as_deref().and_then(|date| { + if date.len() != 8 || !date.bytes().all(|byte| byte.is_ascii_digit()) { + return None; + } + time::Date::from_calendar_date( + date[..4].parse().ok()?, + date[4..6].parse::().ok()?.try_into().ok()?, + date[6..].parse().ok()?, + ) + .ok() + }); + video.publication_date = exact_date.map(|date| date.to_string()).or_else(|| { + video.timestamp.and_then(|timestamp| { + time::OffsetDateTime::from_unix_timestamp(timestamp) + .ok() + .map(|date| format!("~{}", date.date())) + }) + }); Ok(()) } @@ -188,6 +209,8 @@ impl Job { &start.to_string(), "--playlist-end", &end.to_string(), + "--extractor-args", + "youtubetab:approximate_date", "--dump-single-json", &format!("ytsearchall:{query}"), ])) @@ -524,6 +547,33 @@ impl Drop for Playback { mod tests { use super::*; + #[test] + fn publication_dates_prefer_exact_metadata_and_handle_utc_boundaries_and_invalid_dates() { + for parse in [search_results, feed_results] { + for (upload_date, timestamp, expected) in [ + (Some("20240229"), Some(0), Some("2024-02-29")), + (Some("20000229"), None, Some("2000-02-29")), + (Some("19000229"), None, None), + (Some("20230229"), Some(0), Some("~1970-01-01")), + (Some("20241301"), None, None), + (Some("日本語"), None, None), + (None, Some(-1), Some("~1969-12-31")), + (None, Some(1_709_251_199), Some("~2024-02-29")), + (None, Some(1_709_251_200), Some("~2024-03-01")), + (None, Some(i64::MAX), None), + (None, None, None), + ] { + let output = serde_json::json!({"entries":[{ + "id": "jNQXAC9IVRw", "title": "Video", + "upload_date": upload_date, "timestamp": timestamp, + }]}) + .to_string(); + let page = parse(&output).unwrap(); + assert_eq!(page.videos[0].publication_date.as_deref(), expected); + } + } + } + #[test] fn thumbnails_bound_binary_input_and_reject_unsafe_ids() { let bytes = [0xff, 0xd8, 0x00, 0xfe];