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