extract history::find
This commit is contained in:
parent
05f7258978
commit
8b7c88e85c
3 changed files with 22 additions and 15 deletions
17
src/history/find.rs
Normal file
17
src/history/find.rs
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
use crate::prelude::*;
|
||||||
|
|
||||||
|
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> {
|
||||||
|
if let Ok(file) = File::open(file_name) {
|
||||||
|
let reader = BufReader::new(file);
|
||||||
|
for line in reader.lines() {
|
||||||
|
if line? == link.href {
|
||||||
|
return Ok(true); // is already downloaded
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(false) // is not already downloaded
|
||||||
|
}
|
3
src/history/mod.rs
Normal file
3
src/history/mod.rs
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
mod find;
|
||||||
|
|
||||||
|
pub use find::is_already_downloaded;
|
17
src/lib.rs
17
src/lib.rs
|
@ -6,6 +6,7 @@ use atom_syndication::Link;
|
||||||
|
|
||||||
mod errors;
|
mod errors;
|
||||||
mod feed;
|
mod feed;
|
||||||
|
mod history;
|
||||||
pub mod prelude;
|
pub mod prelude;
|
||||||
|
|
||||||
use prelude::*;
|
use prelude::*;
|
||||||
|
@ -17,7 +18,7 @@ pub fn run(subscriptions: &str, history: &str, site: &str) -> Result<()> {
|
||||||
let feed_url = feed::get_feed_url(site, &channel_name)?;
|
let feed_url = feed::get_feed_url(site, &channel_name)?;
|
||||||
for entry in feed::get_feed(feed_url)?.entries() {
|
for entry in feed::get_feed(feed_url)?.entries() {
|
||||||
if let Some(link) = entry.links().get(0).cloned() {
|
if let Some(link) = entry.links().get(0).cloned() {
|
||||||
if !is_already_downloaded(&link, history)? {
|
if !history::is_already_downloaded(&link, history)? {
|
||||||
println!("Downloading {}: {}", &channel_name, entry.title().as_str());
|
println!("Downloading {}: {}", &channel_name, entry.title().as_str());
|
||||||
download_audio(&link)?;
|
download_audio(&link)?;
|
||||||
mark_as_downloaded(&link, history)?;
|
mark_as_downloaded(&link, history)?;
|
||||||
|
@ -37,20 +38,6 @@ fn lines_from(file_name: &str) -> Result<std::io::Lines<std::io::BufReader<std::
|
||||||
Ok(reader.lines())
|
Ok(reader.lines())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_already_downloaded(link: &Link, file_name: &str) -> Result<bool> {
|
|
||||||
use std::io::{BufRead, BufReader};
|
|
||||||
|
|
||||||
if let Ok(file) = File::open(file_name) {
|
|
||||||
let reader = BufReader::new(file);
|
|
||||||
for line in reader.lines() {
|
|
||||||
if line? == link.href {
|
|
||||||
return Ok(true); // is already downloaded
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(false) // is not already downloaded
|
|
||||||
}
|
|
||||||
|
|
||||||
fn download_audio(link: &Link) -> Result<()> {
|
fn download_audio(link: &Link) -> Result<()> {
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue