just show title of entry being downloaded

This commit is contained in:
Paul Campbell 2023-07-24 07:53:00 +01:00
parent 2c953b5997
commit de28bae66b

View file

@ -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<FromUtf8Error> for Error {
fn from(value: FromUtf8Error) -> Self {
Self {
details: value.to_string(),
}
}
}
impl From<String> 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<String> {
fn get_feed_url(site: &str, channel_name: &str) -> Result<String> {
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<String> {
.attr("href")
.unwrap()
.to_string();
println!("rss_url: {}", rss_url);
Ok(rss_url)
}
@ -117,17 +125,14 @@ fn get_feed(url: String) -> Result<Feed> {
fn is_already_downloaded(link: &Link, file_name: &str) -> Result<bool> {
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(())
}