Factoring out some part of the downloader.
This commit is contained in:
parent
e39a89d63d
commit
fc3c8588a3
@ -8,7 +8,7 @@ use std::path::Path;
|
||||
|
||||
use errors::*;
|
||||
use hammond_data::dbqueries;
|
||||
use hammond_data::models::Episode;
|
||||
use hammond_data::models::{Episode, Podcast};
|
||||
use hammond_data::DL_DIR;
|
||||
|
||||
// Adapted from https://github.com/mattgathu/rget .
|
||||
@ -68,36 +68,13 @@ pub fn latest_dl(connection: &SqliteConnection, limit: u32) -> Result<()> {
|
||||
dbqueries::get_pd_episodes_limit(connection, x, limit)?
|
||||
};
|
||||
|
||||
// It might be better to make it a hash of the title
|
||||
let dl_fold = format!("{}/{}", DL_DIR.to_str().unwrap(), x.title());
|
||||
|
||||
// Create the folder
|
||||
DirBuilder::new().recursive(true).create(&dl_fold).unwrap();
|
||||
let dl_fold = get_dl_folder(x)?;
|
||||
|
||||
// Download the episodes
|
||||
let _ :Vec<_>= eps.iter_mut()
|
||||
.map(|y| -> Result<()> {
|
||||
// Check if its alrdy downloaded
|
||||
if y.local_uri().is_some() {
|
||||
if Path::new(y.local_uri().unwrap()).exists() {
|
||||
return Ok(());
|
||||
}
|
||||
y.set_local_uri(None);
|
||||
y.save_changes::<Episode>(connection)?;
|
||||
};
|
||||
|
||||
// Unreliable and hacky way to extract the file extension from the url.
|
||||
let ext = y.uri().split('.').last().unwrap().to_owned();
|
||||
|
||||
// Construct the download path.
|
||||
let dlpath = format!("{}/{}.{}", dl_fold, y.title().unwrap().to_owned(), ext);
|
||||
// info!("Downloading {:?} into: {}", y.title(), dlpath);
|
||||
download_to(&dlpath, y.uri())?;
|
||||
|
||||
// If download succedes set episode local_uri to dlpath.
|
||||
y.set_local_uri(Some(&dlpath));
|
||||
y.save_changes::<Episode>(connection)?;
|
||||
Ok(())
|
||||
.map(|ep| -> Result<()> {
|
||||
// TODO: handle Result here and replace map with for_each
|
||||
get_episode(connection, ep, &dl_fold)
|
||||
})
|
||||
.collect();
|
||||
|
||||
@ -107,3 +84,38 @@ pub fn latest_dl(connection: &SqliteConnection, limit: u32) -> Result<()> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_dl_folder(pd: &Podcast) -> Result<String> {
|
||||
// It might be better to make it a hash of the title
|
||||
let dl_fold = format!("{}/{}", DL_DIR.to_str().unwrap(), pd.title());
|
||||
|
||||
// Create the folder
|
||||
// TODO: handle the unwrap properly
|
||||
DirBuilder::new().recursive(true).create(&dl_fold)?;
|
||||
Ok(dl_fold)
|
||||
}
|
||||
|
||||
fn get_episode(connection: &SqliteConnection, ep: &mut Episode, dl_folder: &str) -> Result<()> {
|
||||
// Check if its alrdy downloaded
|
||||
if ep.local_uri().is_some() {
|
||||
if Path::new(ep.local_uri().unwrap()).exists() {
|
||||
return Ok(());
|
||||
}
|
||||
ep.set_local_uri(None);
|
||||
ep.save_changes::<Episode>(connection)?;
|
||||
};
|
||||
|
||||
// Unreliable and hacky way to extract the file extension from the url.
|
||||
let ext = ep.uri().split('.').last().unwrap().to_owned();
|
||||
|
||||
// Construct the download path.
|
||||
// TODO: Check if its a valid path
|
||||
let dlpath = format!("{}/{}.{}", dl_folder, ep.title().unwrap().to_owned(), ext);
|
||||
// info!("Downloading {:?} into: {}", y.title(), dlpath);
|
||||
download_to(&dlpath, ep.uri())?;
|
||||
|
||||
// If download succedes set episode local_uri to dlpath.
|
||||
ep.set_local_uri(Some(&dlpath));
|
||||
ep.save_changes::<Episode>(connection)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user