Added limit option for dbquerries::latest_dl.

This commit is contained in:
Jordan Petridis 2017-10-03 12:29:40 +03:00
parent fc693a569b
commit d5c4b13b4d
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
3 changed files with 29 additions and 6 deletions

View File

@ -21,7 +21,7 @@ pub fn run() -> Result<()> {
::init()?;
let db = ::establish_connection();
downloader::latest_dl(&db)?;
downloader::latest_dl(&db, 2)?;
Ok(())
}

View File

@ -37,7 +37,25 @@ pub fn get_podcast(con: &SqliteConnection, parent: &Source) -> QueryResult<Vec<P
}
pub fn get_pd_episodes(con: &SqliteConnection, parent: &Podcast) -> QueryResult<Vec<Episode>> {
let eps = Episode::belonging_to(parent).load::<Episode>(con);
use schema::episode::dsl::*;
let eps = Episode::belonging_to(parent)
.order(epoch.desc())
.load::<Episode>(con);
eps
}
pub fn get_pd_episodes_limit(
con: &SqliteConnection,
parent: &Podcast,
limit: u32,
) -> QueryResult<Vec<Episode>> {
use schema::episode::dsl::*;
let eps = Episode::belonging_to(parent)
.order(epoch.desc())
.limit(limit as i64)
.load::<Episode>(con);
eps
}

View File

@ -2,7 +2,7 @@ use reqwest;
use hyper::header::*;
use diesel::prelude::*;
use std::fs::{File, DirBuilder};
use std::fs::{DirBuilder, File};
use std::io::{BufWriter, Read, Write};
use errors::*;
@ -48,12 +48,17 @@ pub fn download_to(target: &str, url: &str) -> Result<()> {
}
// Initial messy prototype, queries load alot of not needed stuff.
pub fn latest_dl(connection: &SqliteConnection) -> Result<()> {
pub fn latest_dl(connection: &SqliteConnection, limit: u32) -> Result<()> {
let pds = dbqueries::get_podcasts(connection)?;
pds.iter()
.map(|x| -> Result<()> {
let eps = dbqueries::get_pd_episodes(connection, &x)?;
let eps;
if limit == 0 {
eps = dbqueries::get_pd_episodes(connection, &x)?;
} else {
eps = 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());