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 atom_syndication::Link;
pub fn download_audio(link: &Link) -> Result<()> {
use std::process::Command;
pub type FetchDownload = fn(&Link) -> Result<()>;
pub fn download(link: &Link) -> Result<()> {
let cmd = "yt-dlp";
// println!("{} --extract-audio --audio-format mp3 {}", cmd, &link.href);
let output = Command::new(cmd)

View file

@ -4,7 +4,7 @@ use atom_syndication::Link;
use std::fs::OpenOptions;
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()
.write(true)
.append(true)

View file

@ -4,7 +4,7 @@ use atom_syndication::Link;
use std::fs::File;
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) {
let reader = BufReader::new(file);
for line in reader.lines() {

View file

@ -1,5 +1,12 @@
use crate::prelude::*;
mod add;
mod find;
pub use add::mark_as_downloaded;
pub use find::is_already_downloaded;
pub use add::add;
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;
pub mod feed;
mod fetch;
mod history;
pub mod fetch;
pub mod history;
pub mod prelude;
mod subscriptions;
use feed::{FeedFind, FeedGet};
use fetch::FetchDownload;
use history::{HistoryAdd, HistoryFind};
use prelude::*;
pub fn run(
subscriptions: &str,
history: &str,
site: &str,
feed_find: feed::FeedFind,
feed_get: feed::FeedGet,
feed_find: FeedFind,
feed_get: FeedGet,
history_find: HistoryFind,
history_add: HistoryAdd,
fetch_download: FetchDownload,
) -> Result<()> {
for channel_name in subscriptions::lines_from(subscriptions)? {
let channel_name = channel_name?;
@ -20,10 +26,10 @@ pub fn run(
let feed_url = feed_find(site, &channel_name)?;
for entry in feed_get(&feed_url)?.entries() {
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());
fetch::download_audio(&link)?;
history::mark_as_downloaded(&link, history)?;
fetch_download(&link)?;
history_add(&link, history)?;
}
}
}

View file

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