aboutsummaryrefslogtreecommitdiffstats
path: root/crates
diff options
context:
space:
mode:
authorBenedikt Peetz <benedikt.peetz@b-peetz.de>2026-11-07 15:04:20 +0100
committerBenedikt Peetz <benedikt.peetz@b-peetz.de>2026-11-07 15:04:20 +0100
commit90e45290985f7af4b4bd2b2b074f5cf611427c7b (patch)
treee9205f425612f3f5be9358ef192d0e4d32947200 /crates
parentfeat(yt/cmds/config): Implement showing the default config (diff)
downloadyt-90e45290985f7af4b4bd2b2b074f5cf611427c7b.zip
feat(yt/cmds/update): Be smarter about updating and show expected update times
Diffstat (limited to 'crates')
-rw-r--r--crates/yt/src/commands/update/implm/mod.rs126
-rw-r--r--crates/yt/src/commands/update/implm/updater.rs27
-rw-r--r--crates/yt/src/commands/update/mod.rs4
3 files changed, 111 insertions, 46 deletions
diff --git a/crates/yt/src/commands/update/implm/mod.rs b/crates/yt/src/commands/update/implm/mod.rs
index b3064c1..b23a2c2 100644
--- a/crates/yt/src/commands/update/implm/mod.rs
+++ b/crates/yt/src/commands/update/implm/mod.rs
@@ -20,7 +20,8 @@ use crate::{
};
use anyhow::{Result, bail};
-use chrono::TimeDelta;
+use chrono::{Local, TimeDelta, TimeZone};
+use chrono_humanize::{Accuracy, HumanTime, Tense};
use colors::{Colorize, IntoCanvas};
use log::info;
@@ -32,15 +33,15 @@ impl UpdateCommand {
max_backlog,
subscriptions: subscription_names_to_update,
force,
+ show_expect,
} = self;
- let mut all_subs = Subscriptions::get(app).await?.remove_inactive();
+ let mut all_subs = Subscriptions::get(app).await?;
let max_backlog = max_backlog.unwrap_or(app.config.update.max_backlog);
- let subs: Vec<Subscription> = if subscription_names_to_update.is_empty() {
- all_subs.0.into_values().collect()
- } else {
+ let subs_selected = !subscription_names_to_update.is_empty();
+ let subs: Vec<Subscription> = if subs_selected {
subscription_names_to_update
.into_iter()
.map(|sub| {
@@ -53,9 +54,11 @@ impl UpdateCommand {
}
})
.collect::<Result<_>>()?
+ } else {
+ all_subs.remove_inactive().0.into_values().collect()
};
- let real_subs = if force {
+ let real_subs = if force || subs_selected {
subs
} else {
// We only update these subscriptions if their last videos has been uploaded for longer than
@@ -63,37 +66,60 @@ impl UpdateCommand {
let mut real_subs = vec![];
for sub in subs {
- let mut videos: Vec<TimeStamp> = Video::from_subscription(&sub, app)
- .await?
- .into_iter()
- .filter_map(|v| v.publish_date)
- .collect();
-
- if videos.is_empty() {
+ let Some((average_time_between_videos, last_video)) =
+ average_upload_time(app, &sub).await?
+ else {
real_subs.push(sub);
break;
- }
-
- videos.sort();
-
- let average_time_between_videos: TimeDelta = {
- let mut videos = videos.iter().copied();
- let mut last = videos.next().expect("we removed the empty video case");
-
- let mut deltas = vec![];
- for next in videos {
- deltas.push(next - last);
- last = next;
- }
+ };
- let sum: TimeDelta = deltas.iter().sum();
+ if show_expect {
+ let expected_seconds = (last_video.as_secs() as f64
+ + average_time_between_videos.as_seconds_f64())
+ as i64;
- sum / deltas.len() as i32
- };
+ let tense = match Local::now().timestamp().cmp(&expected_seconds) {
+ std::cmp::Ordering::Less => Tense::Future,
+ std::cmp::Ordering::Equal => Tense::Present,
+ std::cmp::Ordering::Greater => Tense::Past,
+ };
- let last_video = videos.last().expect("to exist");
+ println!(
+ "{} a video for {} {} (average upload time: {})",
+ match tense {
+ Tense::Past => "Expected",
+ Tense::Present => "Expect",
+ Tense::Future => "Expecting",
+ }
+ .violet()
+ .bold()
+ .render(app.config.global.display_colors),
+ sub.name
+ .clone()
+ .blue()
+ .render(app.config.global.display_colors),
+ HumanTime::from(
+ Local
+ .timestamp_opt(expected_seconds, 0)
+ .single()
+ .expect("This should be valid")
+ )
+ .to_text_en(Accuracy::Rough, tense,)
+ .green()
+ .bold()
+ .render(app.config.global.display_colors),
+ MaybeDuration::from_std(
+ average_time_between_videos
+ .to_std()
+ .expect("Should be positive")
+ )
+ .green()
+ .bold()
+ .render(app.config.global.display_colors)
+ );
+ }
- if average_time_between_videos <= (TimeStamp::from_now() - *last_video) {
+ if average_time_between_videos <= (TimeStamp::from_now() - last_video) {
real_subs.push(sub);
} else {
info!(
@@ -111,7 +137,7 @@ impl UpdateCommand {
.magenta()
.render(app.config.global.display_colors),
MaybeDuration::from_std(
- (TimeStamp::from_now() - *last_video)
+ (TimeStamp::from_now() - last_video)
.to_std()
.expect("Should be strictly positive")
)
@@ -135,3 +161,39 @@ impl UpdateCommand {
Ok(())
}
}
+
+async fn average_upload_time(
+ app: &App,
+ sub: &Subscription,
+) -> Result<Option<(TimeDelta, TimeStamp)>> {
+ let mut videos: Vec<TimeStamp> = Video::from_subscription(sub, app)
+ .await?
+ .into_iter()
+ .filter_map(|v| v.publish_date)
+ .collect();
+
+ if videos.is_empty() {
+ return Ok(None);
+ }
+
+ videos.sort();
+
+ let average_time_between_videos: TimeDelta = {
+ let mut videos = videos.iter().copied();
+ let mut last = videos.next().expect("we removed the empty video case");
+
+ let mut deltas = vec![];
+ for next in videos {
+ deltas.push(next - last);
+ last = next;
+ }
+
+ let sum: TimeDelta = deltas.iter().sum();
+
+ sum / i32::try_from(deltas.len()).expect("to not truncate")
+ };
+
+ let last_video = videos.last().expect("to exist");
+
+ Ok(Some((average_time_between_videos, *last_video)))
+}
diff --git a/crates/yt/src/commands/update/implm/updater.rs b/crates/yt/src/commands/update/implm/updater.rs
index d6fcbd0..f2379f3 100644
--- a/crates/yt/src/commands/update/implm/updater.rs
+++ b/crates/yt/src/commands/update/implm/updater.rs
@@ -19,8 +19,7 @@ use log::{Level, debug, error, log_enabled};
use tokio::io::{AsyncWriteExt, stderr};
use tokio_util::task::LocalPoolHandle;
use yt_dlp::{
- info_json::InfoJson, json_cast, json_try_get, options::YoutubeDLOptions, process_ie_result,
- python_error::PythonError,
+ info_json::InfoJson, json_cast, json_try_get, process_ie_result, python_error::PythonError,
};
use crate::{
@@ -55,14 +54,13 @@ impl Updater {
let total_number = subscriptions.len();
let mut stream = stream::iter(subscriptions)
- .map(|sub| self.get_new_entries(sub, total_number))
+ .map(|sub| self.get_new_entries(sub, total_number, app))
.buffer_unordered(app.config.update.futures);
while let Some(output) = stream.next().await {
let mut entries = output?;
- if let Some(next) = entries.next() {
- let (sub, entry) = next;
+ if let Some((sub, entry)) = entries.next() {
process_subscription(app, sub, entry).await?;
join_all(entries.map(|(sub, entry)| process_subscription(app, sub, entry)))
@@ -80,11 +78,13 @@ impl Updater {
&self,
sub: Subscription,
total_number: usize,
+ app: &App,
) -> Result<impl Iterator<Item = (Subscription, InfoJson)>> {
let max_backlog = self.max_backlog;
let hashes = self.hashes.clone();
- let yt_dlp = yt_dlp_opts_updating(max_backlog)?;
+ let yt_dlp = yt_dlp_opts_updating(app, Some(max_backlog), true)?;
+ let inner_yt_dlp = yt_dlp_opts_updating(app, None, false)?;
self.pool
.spawn_pinned(move || {
@@ -129,12 +129,7 @@ impl Updater {
Ok(valid_entries
.into_iter()
- .map(|(sub, entry)| {
- let inner_yt_dlp = YoutubeDLOptions::new()
- .set("noplaylist", true)
- .build()
- .expect("Worked before, should work now");
-
+ .map(move |(sub, entry)| {
match inner_yt_dlp.process_ie_result(entry, false) {
Ok(output) => Ok((sub, output)),
Err(err) => Err(err),
@@ -145,7 +140,11 @@ impl Updater {
Ok(ok) => Some(ok),
Err(err) => {
match err {
- process_ie_result::Error::Python(PythonError(err)) => {
+ process_ie_result::Error::Python(PythonError {
+ python_error_message,
+ ..
+ }) => {
+ let err = python_error_message;
if err.contains(
"Join this channel to get access \
to members-only content ",
@@ -199,7 +198,7 @@ async fn process_subscription(app: &App, sub: Subscription, entry: InfoJson) ->
})?;
println!(
"{}",
- &video
+ video
.to_line_display(app, None)
.await
.with_context(|| format!("Failed to format video: '{}'", video.title))?
diff --git a/crates/yt/src/commands/update/mod.rs b/crates/yt/src/commands/update/mod.rs
index f181626..482d944 100644
--- a/crates/yt/src/commands/update/mod.rs
+++ b/crates/yt/src/commands/update/mod.rs
@@ -25,6 +25,10 @@ pub(super) struct UpdateCommand {
#[arg(short, long)]
force: bool,
+ /// Print when `yt` expects a new video for every selected subscription.
+ #[arg(short, long)]
+ show_expect: bool,
+
/// The subscriptions to update
#[arg(add = ArgValueCompleter::new(complete_subscription))]
subscriptions: Vec<String>,