just show title of entry being downloaded
This commit is contained in:
parent
2c953b5997
commit
de28bae66b
1 changed files with 19 additions and 13 deletions
32
src/main.rs
32
src/main.rs
|
@ -1,6 +1,6 @@
|
||||||
// https://www.phind.com/agent?cache=clke9xk39001cmj085upzho1t
|
// 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};
|
use atom_syndication::{Entry, Feed, Link};
|
||||||
|
|
||||||
|
@ -16,6 +16,13 @@ impl Display for Error {
|
||||||
f.write_str(self.details.to_string().as_str())
|
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 {
|
impl From<String> for Error {
|
||||||
fn from(details: String) -> Self {
|
fn from(details: String) -> Self {
|
||||||
Self { details }
|
Self { details }
|
||||||
|
@ -59,10 +66,12 @@ fn main() -> Result<()> {
|
||||||
|
|
||||||
for channel_name in lines_from(subscriptions)? {
|
for channel_name in lines_from(subscriptions)? {
|
||||||
let channel_name = channel_name?;
|
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() {
|
for entry in get_feed(feed_url)?.entries() {
|
||||||
if let Some(link) = get_link(entry) {
|
if let Some(link) = get_link(entry) {
|
||||||
if !is_already_downloaded(&link, history)? {
|
if !is_already_downloaded(&link, history)? {
|
||||||
|
println!("Downloading {}: {}", &channel_name, entry.title().as_str());
|
||||||
download_audio(&link)?;
|
download_audio(&link)?;
|
||||||
mark_as_downloaded(&link, history)?;
|
mark_as_downloaded(&link, history)?;
|
||||||
}
|
}
|
||||||
|
@ -74,7 +83,7 @@ fn main() -> Result<()> {
|
||||||
Ok(())
|
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 let Some(channel_prefix) = channel_name.chars().next() {
|
||||||
if channel_prefix != '@' {
|
if channel_prefix != '@' {
|
||||||
return Err(format!("Channel Name must begin with an '@': {}", channel_name).into());
|
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")
|
.attr("href")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.to_string();
|
.to_string();
|
||||||
println!("rss_url: {}", rss_url);
|
|
||||||
Ok(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> {
|
fn is_already_downloaded(link: &Link, file_name: &str) -> Result<bool> {
|
||||||
use std::io::{BufRead, BufReader};
|
use std::io::{BufRead, BufReader};
|
||||||
|
|
||||||
println!("is already downloaded? {}", link.href);
|
|
||||||
if let Ok(file) = File::open(file_name) {
|
if let Ok(file) = File::open(file_name) {
|
||||||
let reader = BufReader::new(file);
|
let reader = BufReader::new(file);
|
||||||
for line in reader.lines() {
|
for line in reader.lines() {
|
||||||
if line? == link.href {
|
if line? == link.href {
|
||||||
println!("Yes!");
|
|
||||||
return Ok(true); // is already downloaded
|
return Ok(true); // is already downloaded
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
println!("No!");
|
|
||||||
Ok(false) // is not already downloaded
|
Ok(false) // is not already downloaded
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -135,16 +140,17 @@ fn download_audio(link: &Link) -> Result<()> {
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
|
|
||||||
let cmd = "yt-dlp";
|
let cmd = "yt-dlp";
|
||||||
println!("download audio for {}", link.href());
|
// println!("{} --extract-audio --audio-format mp3 {}", cmd, &link.href);
|
||||||
println!("{} --extract-audio --audio-format mp3 {}", cmd, &link.href);
|
let output = Command::new(cmd)
|
||||||
let mut child = Command::new(cmd)
|
|
||||||
.arg("--extract-audio")
|
.arg("--extract-audio")
|
||||||
.arg("--audio-format")
|
.arg("--audio-format")
|
||||||
.arg("mp3")
|
.arg("mp3")
|
||||||
.arg(&link.href)
|
.arg(&link.href)
|
||||||
.spawn()
|
.output()?;
|
||||||
.expect("Failed to execute command");
|
if !output.stderr.is_empty() {
|
||||||
child.wait()?;
|
eprintln!("Error: {}", String::from_utf8(output.stderr)?);
|
||||||
|
println!("{}", String::from_utf8(output.stdout)?);
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue