diff --git a/crates/app/src/bridge.rs b/crates/app/src/bridge.rs index 12bc259..1c455ea 100644 --- a/crates/app/src/bridge.rs +++ b/crates/app/src/bridge.rs @@ -249,6 +249,11 @@ impl Bridge { Some(self.manager()?.probe(task, secret)) } + /// What a video site offers at the link. None without an engine. Ask for the answer with `poll`. + pub fn probe_video(&self, url: String) -> Option>> { + Some(self.manager()?.probe_video(url, None)) + } + /// Adds downloads. Their secrets are put in the keyring first, under the names the tasks have. pub fn add(&self, tasks: Vec, secrets: Vec<(String, Secret)>) { let (sites, manager) = match &mut *lock(&self.inner.engine) { diff --git a/crates/app/src/views/add/draw.rs b/crates/app/src/views/add/draw.rs index 1dfb77d..a419b52 100644 --- a/crates/app/src/views/add/draw.rs +++ b/crates/app/src/views/add/draw.rs @@ -76,7 +76,7 @@ impl Add { }; // Nothing is made of the rest when another download goes on. let free = matches!(choice, Choice::Show | Choice::Resume); - let good = n > 0 && (free || !wrong.any()); + let good = n > 0 && (free || !wrong.any() && !self.video_empty()); let title = tr!("add.title"); let open = self.open; let dialog = Dialog::new("add", &title, &verb).width(size::DIALOG_L).enabled(good).busy(self.check.busy()); @@ -99,6 +99,12 @@ impl Add { if !matches!(self.choice(), Choice::Download) { return; } + match &mut self.video { + Some(Page::Looking) => say(ui, &tr!("video.looking"), t.on_variant), + Some(Page::Failed(why)) => say(ui, why, t.error), + Some(Page::Ready(video)) => video.show(ui, t), + None => {} + } if !self.many() { let (label, hint) = (tr!("add.name"), tr!("add.name_hint")); let field = Field::new(&label).hint(&hint); @@ -154,6 +160,7 @@ impl Add { _ if self.single().is_none() => tr!("add.nothing"), Look::Nothing => tr!("add.unknown"), Look::Waits(_) | Look::Asks(_) => tr!("add.asking", host = host), + Look::Found(_) if matches!(self.video, Some(Page::Ready(_))) => tr!("video.title"), Look::Found(p) if p.page => tr!("add.page"), Look::Found(p) => { let size = p.size.map_or_else(|| tr!("add.no_size"), amount); @@ -193,7 +200,7 @@ impl Add { let galley = p.layout_job(job); p.galley(pos2(x, rect.center().y - galley.size().y / 2.0), galley, ink); } else { - let warn = matches!(&self.look, Look::Found(p) if p.page); + let warn = matches!(&self.look, Look::Found(p) if p.page) && !matches!(self.video, Some(Page::Ready(_))); left(p, pos2(x, rect.center().y - 11.0), line(p, &self.name, fonts::med(text::BODY), ink, end - x)); left(p, pos2(x, rect.center().y + 11.0), line(p, &words, fonts::ui(text::LABEL), if warn { t.warning } else { faint }, end - x)); } diff --git a/crates/app/src/views/add/mod.rs b/crates/app/src/views/add/mod.rs index 82eaa38..f889216 100644 --- a/crates/app/src/views/add/mod.rs +++ b/crates/app/src/views/add/mod.rs @@ -10,6 +10,7 @@ use crate::ui::shape::Kind; use crate::ui::toast::Toasts; use crate::views::list::model::{kind_key, kind_of}; use crate::views::main::Reply as Click; +use crate::views::video::{Page, Video}; use chrono::{Days, Local, NaiveDateTime, NaiveTime}; use eframe::egui::{Context, Ui}; use medlar_core::auth::{Login, take_login}; @@ -365,6 +366,9 @@ pub struct Add { focus_links: bool, asked: Option, pub look: Look, + /// A video site's page at the single link; None when it is not one. Its choice goes to yt-dlp. + pub video: Option, + video_asked: Option>>, /// Tests put answers here themselves. pub still: bool, /// What `known` was found for. @@ -422,6 +426,8 @@ impl Add { focus_links: true, asked: None, look: Look::Nothing, + video: None, + video_asked: None, still: false, sought: None, known: None, @@ -512,6 +518,7 @@ impl Add { self.read_text(); self.usual_folder(cx); self.ask_server(ctx, cx); + self.follow_video(ctx); self.seek(cx); self.ask_space(ctx); if self.check.follow(ctx) && self.check.good().is_some_and(|c| c.verdict() == Verdict::Same) { @@ -579,6 +586,7 @@ impl Add { if self.asked != asked { self.look = if asked.is_some() { Look::Waits(now + PAUSE) } else { Look::Nothing }; self.asked = asked; + (self.video, self.video_asked) = (None, None); if !self.name_own { self.name = self.single().map(name_of).unwrap_or_default(); } @@ -591,7 +599,16 @@ impl Add { ctx.request_repaint(); } Look::Asks(reply) => match poll(reply, ctx) { - Some(Ok(found)) => self.learned(found), + Some(Ok(found)) => { + // A web page may be a video site's. + if found.page + && let Some(asked) = &self.asked + { + self.video_asked = cx.bridge.probe_video(asked.task.url.clone()); + self.video = self.video_asked.is_some().then_some(Page::Looking); + } + self.learned(found); + } Some(Err(manager::Error::Link(f))) => self.look = Look::Failed(Some(f.kind), f.text.clone()), Some(Err(e)) => self.look = Look::Failed(None, e.to_string()), None => {} @@ -600,6 +617,29 @@ impl Add { } } + /// What yt-dlp said about the page. + fn follow_video(&mut self, ctx: &Context) { + let Some(answer) = self.video_asked.as_mut().and_then(|r| poll(r, ctx)) else { return }; + self.video_asked = None; + self.video = match answer { + Ok(probe) => { + if !self.name_own { + self.name = safe_name(&probe.title).unwrap_or_default(); + } + Some(Page::Ready(Box::new(Video::new(probe)))) + } + // Not a video site, or no yt-dlp to ask: the page itself is what the link gives. + Err(manager::Error::Link(f)) if matches!(f.kind, ErrorKind::BadLink | ErrorKind::Tool) => None, + Err(manager::Error::Link(f)) => Some(Page::Failed(f.text.clone())), + Err(e) => Some(Page::Failed(e.to_string())), + }; + } + + /// Nothing of a playlist is picked. + fn video_empty(&self) -> bool { + matches!(&self.video, Some(Page::Ready(v)) if v.empty()) + } + /// What the server said. pub fn learned(&mut self, found: Probed) { if !self.name_own { @@ -814,6 +854,9 @@ impl Add { } task.mirrors = Batch::of(&self.mirrors, false).links.iter().map(|m| take_login(m).0).collect(); task.checksum = self.checksum_text(); + if let Some(Page::Ready(v)) = &self.video { + task.video = Some(v.choice()); + } } (tasks, secrets) } diff --git a/crates/core/src/manager/actor.rs b/crates/core/src/manager/actor.rs index 551e87a..568c9d7 100644 --- a/crates/core/src/manager/actor.rs +++ b/crates/core/src/manager/actor.rs @@ -23,7 +23,7 @@ use crate::speed::{History, Meter}; use crate::store::{self, Store}; use crate::stream::{self, Tools}; use crate::verify::Checksum; -use crate::{schedule, adopt, disk, host, metalink, redact, site, strings, tr, verify}; +use crate::{schedule, adopt, disk, host, metalink, redact, site, strings, tr, verify, ytdlp}; use arc_swap::ArcSwap; use std::collections::{BTreeMap, BTreeSet, HashMap, VecDeque}; use std::path::{Path, PathBuf}; @@ -423,6 +423,7 @@ impl Actor { Msg::FindChecksum(id, answer) => self.find_checksum(id, answer), Msg::ReadMetalink(link, answer) => self.read_metalink(link, answer), Msg::Probe(task, secret, answer) => self.probe(*task, secret, answer), + Msg::ProbeVideo(url, cookies, answer) => self.probe_video(url, cookies, answer), Msg::Limits(limits) => self.set_limits(limits), Msg::Rescan => self.choose = true, Msg::OnChange(call) => self.on_change.push(call), @@ -784,6 +785,14 @@ impl Actor { }); } + fn probe_video(&self, url: String, cookies: Option, answer: oneshot::Sender>>) { + let (tool, cache) = (self.config.ytdlp.clone(), self.config.videos.clone()); + self.look(answer, host_of(&url), async move { + let found = ytdlp::Ytdlp::found(tool.as_deref()).await?; + found.probe(&cache, &url, cookies.as_deref(), std::future::pending()).await + }); + } + fn await_link(&mut self, id: TaskId) -> Result<()> { if matches!(self.entries[&id].task.state, State::Checking | State::Finishing | State::Done | State::Gone) { return Ok(()); @@ -1128,9 +1137,12 @@ impl Actor { monitor.finish(); } let (shared, options, to) = (self.config.shared.clone(), self.config.options.clone(), self.to.clone()); - let sites = self.config.sites.clone(); + let (sites, tool) = (self.config.sites.clone(), self.config.ytdlp.clone()); self.runtime().spawn(async move { let work = tokio::spawn(async move { + if task.video.is_some() { + return ytdlp::download(&shared, &task, tool.as_deref(), &monitor, stopped).await; + } let source = site::source(sites.as_ref(), &task, &stopped).await?; if stream::kind_of(&task).is_some() { let request = stream::Request { id, source, choice: task.stream.clone().unwrap_or_default(), tools: &Tools::find(), options: &options }; diff --git a/crates/core/src/manager/mod.rs b/crates/core/src/manager/mod.rs index c76d96b..0ec57da 100644 --- a/crates/core/src/manager/mod.rs +++ b/crates/core/src/manager/mod.rs @@ -206,6 +206,10 @@ pub struct Config { pub tick: Duration, /// Time for queue schedules; tests set their own. pub clock: Arc, + /// Where yt-dlp is; None: looked for beside Medlar and on the PATH. Tests set a stub. + pub ytdlp: Option, + /// What video sites answered, for a while. + pub videos: Arc, } impl Config { @@ -221,6 +225,8 @@ impl Config { undo: Duration::from_secs(60), tick: Duration::from_millis(100), clock: Arc::new(crate::schedule::SystemClock), + ytdlp: None, + videos: Arc::default(), } } } @@ -234,6 +240,7 @@ enum Msg { ReadMetalink(NewLink, oneshot::Sender>>), CheckPartial(PathBuf, NewLink, oneshot::Sender>), Probe(Box, Option, oneshot::Sender>), + ProbeVideo(String, Option, oneshot::Sender>>), Limits(Limits), /// Queues were changed in the store. Rescan, @@ -312,6 +319,14 @@ impl Manager { Reply(reply) } + /// What a video site offers at the link, through yt-dlp; nothing is added. `cookies` is the text of a cookie file, used + /// without being kept. Dropping the reply ends it. + pub fn probe_video(&self, url: String, cookies: Option) -> Reply> { + let (answer, reply) = oneshot::channel(); + let _ = self.to.send(Msg::ProbeVideo(url, cookies, answer)); + Reply(reply) + } + /// Takes effect at once, also on running downloads. 0 is no limit. Connections that may stay go on as they are. pub fn set_limits(&self, limits: Limits) { let _ = self.to.send(Msg::Limits(limits)); diff --git a/crates/core/src/model.rs b/crates/core/src/model.rs index f24e465..c989f19 100644 --- a/crates/core/src/model.rs +++ b/crates/core/src/model.rs @@ -194,6 +194,8 @@ pub struct Task { pub after: Option, /// What was chosen of a stream. None is the best of it, for as long as it goes on. pub stream: Option, + /// What was chosen of a video site's page; such a task runs through yt-dlp. + pub video: Option, /// The unfinished one this starts over. Removed when this is done. pub replaces: Option, pub start_at: Option