From fc3c8588a3512fb992441d2df2a4f777a02b963d Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Fri, 13 Oct 2017 04:24:08 +0300 Subject: [PATCH] Factoring out some part of the downloader. --- hammond-downloader/src/downloader.rs | 68 ++++++++++++++++------------ 1 file changed, 40 insertions(+), 28 deletions(-) diff --git a/hammond-downloader/src/downloader.rs b/hammond-downloader/src/downloader.rs index 0f9f030..92e2655 100644 --- a/hammond-downloader/src/downloader.rs +++ b/hammond-downloader/src/downloader.rs @@ -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::(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::(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 { + // 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::(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::(connection)?; + Ok(()) +}