i13-cli-history-file #14

Merged
kemitix merged 4 commits from i13-cli-history-file into main 2023-08-06 15:07:56 +01:00
7 changed files with 64 additions and 31 deletions

View file

@ -1,3 +1,4 @@
use crate::params::Args;
use crate::prelude::*; use crate::prelude::*;
use std::fs::{File, OpenOptions}; use std::fs::{File, OpenOptions};
@ -12,13 +13,13 @@ pub struct FileEnv {
pub append_line: FileAppendLineFn, pub append_line: FileAppendLineFn,
} }
impl FileEnv { impl FileEnv {
pub fn create(directory: String) -> Self { pub fn create(a: &Args) -> Self {
let open_dir = directory.clone(); let open_dir = a.downloads.clone();
let append_dir = directory.clone(); let append_dir = a.downloads.clone();
Self { Self {
open: Box::new(move |file_name| { open: Box::new(move |file_name| {
let path = format!("{}/{}", &open_dir, file_name); let path = format!("{}/{}", &open_dir, file_name);
let file = File::open(&path)?; let file = File::open(path)?;
Ok(file) Ok(file)
}), }),
append_line: Box::new(move |file_name, line| { append_line: Box::new(move |file_name, line| {
@ -27,7 +28,7 @@ impl FileEnv {
.write(true) .write(true)
.append(true) .append(true)
.create(true) .create(true)
.open(&path) .open(path)
.unwrap(); .unwrap();
writeln!(file, "{}", line)?; writeln!(file, "{}", line)?;
Ok(()) Ok(())

View file

@ -18,7 +18,7 @@ pub fn lines_from(file_name: &str, e: &FileEnv) -> Result<Vec<String>> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::test_utils::create_text_file; use crate::{params::Args, test_utils::create_text_file};
use super::*; use super::*;
@ -30,7 +30,10 @@ mod tests {
file_name, file_name,
include_bytes!("../../test/data/subscriptions.txt"), include_bytes!("../../test/data/subscriptions.txt"),
)?; )?;
let file_env = FileEnv::create(dir.path().to_string_lossy().to_string()); let file_env = FileEnv::create(&Args {
downloads: dir.path().to_string_lossy().to_string(),
history: "downloaded.txt".to_string(),
});
//when //when
let result = lines_from(file_name, &file_env)?; let result = lines_from(file_name, &file_env)?;
@ -49,7 +52,10 @@ mod tests {
file_name, file_name,
include_bytes!("../../test/data/subscriptions-blank-line.txt"), include_bytes!("../../test/data/subscriptions-blank-line.txt"),
)?; )?;
let file_env = FileEnv::create(dir.path().to_string_lossy().to_string()); let file_env = FileEnv::create(&Args {
downloads: dir.path().to_string_lossy().to_string(),
history: "downloaded.txt".to_string(),
});
//when //when
let result = lines_from(file_name, &file_env)?; let result = lines_from(file_name, &file_env)?;
@ -68,7 +74,10 @@ mod tests {
file_name, file_name,
include_bytes!("../../test/data/subscriptions-comment.txt"), include_bytes!("../../test/data/subscriptions-comment.txt"),
)?; )?;
let file_env = FileEnv::create(dir.path().to_string_lossy().to_string()); let file_env = FileEnv::create(&Args {
downloads: dir.path().to_string_lossy().to_string(),
history: "downloaded.txt".to_string(),
});
//when //when
let result = lines_from(file_name, &file_env)?; let result = lines_from(file_name, &file_env)?;

View file

@ -13,6 +13,7 @@ mod tests {
use crate::{ use crate::{
history::Link, history::Link,
params::Args,
test_utils::{create_text_file, read_text_file}, test_utils::{create_text_file, read_text_file},
}; };
@ -38,7 +39,10 @@ mod tests {
add( add(
&link, &link,
file_name, file_name,
&FileEnv::create(dir.path().to_string_lossy().to_string()), &FileEnv::create(&Args {
downloads: dir.path().to_string_lossy().to_string(),
history: "downloaded.txt".to_string(),
}),
)?; )?;
//then //then
@ -69,7 +73,10 @@ mod tests {
add( add(
&link, &link,
file_name, file_name,
&FileEnv::create(dir.path().to_string_lossy().to_string()), &FileEnv::create(&Args {
downloads: dir.path().to_string_lossy().to_string(),
history: "downloaded.txt".to_string(),
}),
)?; )?;
//then //then

View file

@ -18,7 +18,7 @@ pub fn find(link: &Link, file_name: &str, e: &FileEnv) -> Result<bool> {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use crate::{history::Link, test_utils::create_text_file}; use crate::{history::Link, params::Args, test_utils::create_text_file};
use super::*; use super::*;
#[test] #[test]
@ -38,7 +38,10 @@ mod test {
let result = find( let result = find(
&link, &link,
file_name, file_name,
&FileEnv::create(dir.path().to_string_lossy().to_string()), &FileEnv::create(&Args {
downloads: dir.path().to_string_lossy().to_string(),
history: "downloaded.txt".to_string(),
}),
)?; )?;
//then //then
@ -70,7 +73,10 @@ mod test {
let result = find( let result = find(
&link, &link,
file_name, file_name,
&FileEnv::create(dir.path().to_string_lossy().to_string()), &FileEnv::create(&Args {
downloads: dir.path().to_string_lossy().to_string(),
history: "downloaded.txt".to_string(),
}),
)?; )?;
//then //then
@ -102,7 +108,10 @@ mod test {
let result = find( let result = find(
&link, &link,
file_name, file_name,
&FileEnv::create(dir.path().to_string_lossy().to_string()), &FileEnv::create(&Args {
downloads: dir.path().to_string_lossy().to_string(),
history: "downloaded.txt".to_string(),
}),
)?; )?;
//then //then

View file

@ -1,3 +1,4 @@
use params::Args;
use prelude::*; use prelude::*;
mod errors; mod errors;
@ -19,16 +20,16 @@ pub struct Env {
pub file: FileEnv, pub file: FileEnv,
} }
pub fn run(subscriptions: &str, history: &str, site: &str, e: Env) -> Result<()> { pub fn run(subscriptions: &str, site: &str, a: &Args, e: Env) -> Result<()> {
for channel_name in file::read::lines_from(subscriptions, &e.file)? { for channel_name in file::read::lines_from(subscriptions, &e.file)? {
println!("Channel: {}", channel_name); println!("Channel: {}", channel_name);
let feed_url = feed::find(site, &channel_name, &e.network)?; let feed_url = feed::find(site, &channel_name, &e.network)?;
for entry in feed::get(&feed_url, &e.network)?.entries() { for entry in feed::get(&feed_url, &e.network)?.entries() {
if let Some(link) = entry.links().get(0).cloned() { if let Some(link) = entry.links().get(0).cloned() {
if !history::find(&link, history, &e.file)? { if !history::find(&link, &a.history, &e.file)? {
println!("Downloading {}: {}", &channel_name, entry.title().as_str()); println!("Downloading {}: {}", &channel_name, entry.title().as_str());
(e.network.download_as_mp3)(&link.href)?; (e.network.download_as_mp3)(&link.href)?;
history::add(&link, history, &e.file)?; history::add(&link, &a.history, &e.file)?;
} }
} }
} }
@ -65,6 +66,15 @@ mod tests {
let history_file_name = "history"; let history_file_name = "history";
let history_dir = create_text_file(history_file_name, "c1-f2\nc2-f3".as_bytes())?; let history_dir = create_text_file(history_file_name, "c1-f2\nc2-f3".as_bytes())?;
let history_file_name = format!(
"{}/{}",
history_dir.path().to_string_lossy(),
history_file_name
);
let args = Args {
downloads: subs_dir.path().to_string_lossy().to_string(),
history: history_file_name.clone(),
};
let env = Env { let env = Env {
network: NetworkEnv { network: NetworkEnv {
fetch_as_text: mock_fetch_as_text_with_rss_url(HashMap::from([ fetch_as_text: mock_fetch_as_text_with_rss_url(HashMap::from([
@ -89,21 +99,14 @@ mod tests {
subs_file_name.to_string(), subs_file_name.to_string(),
format!("{}/{}", subs_dir.path().to_string_lossy(), subs_file_name), format!("{}/{}", subs_dir.path().to_string_lossy(), subs_file_name),
), ),
( (history_file_name.to_string(), history_file_name),
history_file_name.to_string(),
format!(
"{}/{}",
history_dir.path().to_string_lossy(),
history_file_name
),
),
])), ])),
append_line: mock_file_append_line(), append_line: mock_file_append_line(),
}, },
}; };
//when //when
run(subs_file_name, history_file_name, site, env)?; run(subs_file_name, site, &args, env)?;
//then //then
drop(subs_dir); drop(subs_dir);
drop(history_dir); drop(history_dir);

View file

@ -7,18 +7,17 @@ use podal::prelude::*;
fn main() -> Result<()> { fn main() -> Result<()> {
println!("Podal"); println!("Podal");
let subscriptions = "subscriptions.txt"; let subscriptions = "subscriptions.txt";
let history = "downloaded.txt";
let site = "https://www.youtube.com/"; let site = "https://www.youtube.com/";
let args = Args::parse(); let args = Args::parse();
podal::run( podal::run(
subscriptions, subscriptions,
history,
site, site,
&args,
podal::Env { podal::Env {
network: NetworkEnv::default(), network: NetworkEnv::default(),
file: FileEnv::create(args.directory), file: FileEnv::create(&args),
}, },
)?; )?;

View file

@ -6,5 +6,10 @@ pub struct Args {
/// This is also the directory where the subscription and history files are stored. /// This is also the directory where the subscription and history files are stored.
/// Defaults to the current directory /// Defaults to the current directory
#[arg(short, long, default_value = ".")] #[arg(short, long, default_value = ".")]
pub directory: String, pub downloads: String,
/// The name of the history file.
/// Defaults to "downloaded.txt" located in the downloads directory.
#[arg(long, default_value = "downloaded.txt")]
pub history: String,
} }