From de28bae66bcb8f1f077ce608e31cee2bdb3a8b1f Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Mon, 24 Jul 2023 07:53:00 +0100 Subject: [PATCH] just show title of entry being downloaded --- src/main.rs | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index 566c64b..46cef06 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ // https://www.phind.com/agent?cache=clke9xk39001cmj085upzho1t -use std::{fmt::Display, fs::File}; +use std::{fmt::Display, fs::File, string::FromUtf8Error}; use atom_syndication::{Entry, Feed, Link}; @@ -16,6 +16,13 @@ impl Display for Error { f.write_str(self.details.to_string().as_str()) } } +impl From for Error { + fn from(value: FromUtf8Error) -> Self { + Self { + details: value.to_string(), + } + } +} impl From for Error { fn from(details: String) -> Self { Self { details } @@ -59,10 +66,12 @@ fn main() -> Result<()> { for channel_name in lines_from(subscriptions)? { let channel_name = channel_name?; - let feed_url = get_feed_url(site, channel_name)?; + println!("Channel: {}", channel_name); + let feed_url = get_feed_url(site, &channel_name)?; for entry in get_feed(feed_url)?.entries() { if let Some(link) = get_link(entry) { if !is_already_downloaded(&link, history)? { + println!("Downloading {}: {}", &channel_name, entry.title().as_str()); download_audio(&link)?; mark_as_downloaded(&link, history)?; } @@ -74,7 +83,7 @@ fn main() -> Result<()> { Ok(()) } -fn get_feed_url(site: &str, channel_name: String) -> Result { +fn get_feed_url(site: &str, channel_name: &str) -> Result { if let Some(channel_prefix) = channel_name.chars().next() { if channel_prefix != '@' { return Err(format!("Channel Name must begin with an '@': {}", channel_name).into()); @@ -90,7 +99,6 @@ fn get_feed_url(site: &str, channel_name: String) -> Result { .attr("href") .unwrap() .to_string(); - println!("rss_url: {}", rss_url); Ok(rss_url) } @@ -117,17 +125,14 @@ fn get_feed(url: String) -> Result { fn is_already_downloaded(link: &Link, file_name: &str) -> Result { use std::io::{BufRead, BufReader}; - println!("is already downloaded? {}", link.href); if let Ok(file) = File::open(file_name) { let reader = BufReader::new(file); for line in reader.lines() { if line? == link.href { - println!("Yes!"); return Ok(true); // is already downloaded } } } - println!("No!"); Ok(false) // is not already downloaded } @@ -135,16 +140,17 @@ fn download_audio(link: &Link) -> Result<()> { use std::process::Command; let cmd = "yt-dlp"; - println!("download audio for {}", link.href()); - println!("{} --extract-audio --audio-format mp3 {}", cmd, &link.href); - let mut child = Command::new(cmd) + // println!("{} --extract-audio --audio-format mp3 {}", cmd, &link.href); + let output = Command::new(cmd) .arg("--extract-audio") .arg("--audio-format") .arg("mp3") .arg(&link.href) - .spawn() - .expect("Failed to execute command"); - child.wait()?; + .output()?; + if !output.stderr.is_empty() { + eprintln!("Error: {}", String::from_utf8(output.stderr)?); + println!("{}", String::from_utf8(output.stdout)?); + } Ok(()) }