diff --git a/src/dbqueries.rs b/src/dbqueries.rs index db79f5b..2ae0981 100644 --- a/src/dbqueries.rs +++ b/src/dbqueries.rs @@ -26,7 +26,17 @@ pub fn get_podcasts(con: &SqliteConnection) -> QueryResult> { pub fn get_episodes(con: &SqliteConnection) -> QueryResult> { use schema::episode::dsl::*; - let eps = episode.load::(con); + let eps = episode.order(epoch.desc()).load::(con); + eps +} + +pub fn get_episodes_with_limit(con: &SqliteConnection, limit: u32) -> QueryResult> { + use schema::episode::dsl::*; + + let eps = episode + .order(epoch.desc()) + .limit(limit as i64) + .load::(con); eps } diff --git a/src/index_feed.rs b/src/index_feed.rs index 956580a..e6eb991 100644 --- a/src/index_feed.rs +++ b/src/index_feed.rs @@ -57,7 +57,7 @@ fn index_episode(con: &SqliteConnection, ep: &NewEpisode) -> Result<()> { } Ok(()) } -fn insert_return_source(con: &SqliteConnection, url: &str) -> Result { +pub fn insert_return_source(con: &SqliteConnection, url: &str) -> Result { let foo = NewSource::new_with_uri(url); index_source(con, &foo)?; diff --git a/src/main.rs b/src/main.rs index 94e1074..5cb829d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,9 @@ -#[macro_use] extern crate log; extern crate loggerv; +extern crate structopt; #[macro_use] extern crate structopt_derive; -extern crate structopt; #[macro_use] extern crate error_chain; @@ -14,13 +13,24 @@ extern crate hammond; use structopt::StructOpt; use hammond::errors::*; use hammond::downloader; +use hammond::index_feed; +use hammond::dbqueries; +// Should probably had made an Enum instead. #[derive(StructOpt, Debug)] #[structopt(name = "example", about = "An example of StructOpt usage.")] struct Opt { /// Enable logging, use multiple `v`s to increase verbosity #[structopt(short = "v", long = "verbose")] verbosity: u64, + + #[structopt(long = "update")] up: bool, + + #[structopt(long = "latest")] latest: bool, + + #[structopt(long = "download", default_value = "-1")] dl: i64, + + #[structopt(short = "a", long = "add", default_value = "")] add: String, } fn run() -> Result<()> { @@ -28,14 +38,33 @@ fn run() -> Result<()> { loggerv::init_with_verbosity(args.verbosity)?; - let foo = args; - info!("{:?}", foo); - hammond::init()?; - let db = hammond::establish_connection(); - downloader::latest_dl(&db, 2)?; + + // Initial prototype for testings. + // The plan is to write a Gtk+ gui later. + if args.add != "".to_string() { + let db = hammond::establish_connection(); + let _ = index_feed::insert_return_source(&db, &args.add); + } + + if args.up { + let db = hammond::establish_connection(); + index_feed::index_loop(db)?; + } + + if args.dl >= 0 { + let db = hammond::establish_connection(); + downloader::latest_dl(&db, args.dl as u32)?; + } + + if args.latest { + let db = hammond::establish_connection(); + let foo = dbqueries::get_episodes_with_limit(&db, 10)?; + // This ends up horribly but works for now. + let _: Vec<_> = foo.iter().map(|x| println!("{:?}", x)).collect(); + } Ok(()) } -quick_main!(run); \ No newline at end of file +quick_main!(run);