provide history find/add and fetch download as parameters to main

This commit is contained in:
Paul Campbell 2023-07-25 14:56:59 +01:00
parent 17a92e45e4
commit 65156db75e
6 changed files with 30 additions and 13 deletions

View file

@ -1,10 +1,11 @@
use crate::prelude::*; use crate::prelude::*;
use atom_syndication::Link; use atom_syndication::Link;
use std::process::Command;
pub fn download_audio(link: &Link) -> Result<()> { pub type FetchDownload = fn(&Link) -> Result<()>;
use std::process::Command;
pub fn download(link: &Link) -> Result<()> {
let cmd = "yt-dlp"; let cmd = "yt-dlp";
// println!("{} --extract-audio --audio-format mp3 {}", cmd, &link.href); // println!("{} --extract-audio --audio-format mp3 {}", cmd, &link.href);
let output = Command::new(cmd) let output = Command::new(cmd)

View file

@ -4,7 +4,7 @@ use atom_syndication::Link;
use std::fs::OpenOptions; use std::fs::OpenOptions;
use std::io::prelude::*; use std::io::prelude::*;
pub fn mark_as_downloaded(link: &Link, file_name: &str) -> Result<()> { pub fn add(link: &Link, file_name: &str) -> Result<()> {
let mut file = OpenOptions::new() let mut file = OpenOptions::new()
.write(true) .write(true)
.append(true) .append(true)

View file

@ -4,7 +4,7 @@ use atom_syndication::Link;
use std::fs::File; use std::fs::File;
use std::io::{BufRead, BufReader}; use std::io::{BufRead, BufReader};
pub fn is_already_downloaded(link: &Link, file_name: &str) -> Result<bool> { pub fn find(link: &Link, file_name: &str) -> Result<bool> {
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() {

View file

@ -1,5 +1,12 @@
use crate::prelude::*;
mod add; mod add;
mod find; mod find;
pub use add::mark_as_downloaded; pub use add::add;
pub use find::is_already_downloaded; pub use find::find;
type Link = atom_syndication::Link;
pub type HistoryFind = fn(&Link, &str) -> Result<bool>;
pub type HistoryAdd = fn(&Link, &str) -> Result<()>;

View file

@ -1,18 +1,24 @@
mod errors; mod errors;
pub mod feed; pub mod feed;
mod fetch; pub mod fetch;
mod history; pub mod history;
pub mod prelude; pub mod prelude;
mod subscriptions; mod subscriptions;
use feed::{FeedFind, FeedGet};
use fetch::FetchDownload;
use history::{HistoryAdd, HistoryFind};
use prelude::*; use prelude::*;
pub fn run( pub fn run(
subscriptions: &str, subscriptions: &str,
history: &str, history: &str,
site: &str, site: &str,
feed_find: feed::FeedFind, feed_find: FeedFind,
feed_get: feed::FeedGet, feed_get: FeedGet,
history_find: HistoryFind,
history_add: HistoryAdd,
fetch_download: FetchDownload,
) -> Result<()> { ) -> Result<()> {
for channel_name in subscriptions::lines_from(subscriptions)? { for channel_name in subscriptions::lines_from(subscriptions)? {
let channel_name = channel_name?; let channel_name = channel_name?;
@ -20,10 +26,10 @@ pub fn run(
let feed_url = feed_find(site, &channel_name)?; let feed_url = feed_find(site, &channel_name)?;
for entry in feed_get(&feed_url)?.entries() { for entry in feed_get(&feed_url)?.entries() {
if let Some(link) = entry.links().get(0).cloned() { if let Some(link) = entry.links().get(0).cloned() {
if !history::is_already_downloaded(&link, history)? { if !history_find(&link, history)? {
println!("Downloading {}: {}", &channel_name, entry.title().as_str()); println!("Downloading {}: {}", &channel_name, entry.title().as_str());
fetch::download_audio(&link)?; fetch_download(&link)?;
history::mark_as_downloaded(&link, history)?; history_add(&link, history)?;
} }
} }
} }

View file

@ -12,6 +12,9 @@ fn main() -> Result<()> {
site, site,
podal::feed::find, podal::feed::find,
podal::feed::get, podal::feed::get,
podal::history::find,
podal::history::add,
podal::fetch::download,
)?; )?;
println!("Done"); println!("Done");