From cbc0692482c71c225c571728709677dc2eca74ad Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Thu, 5 Oct 2017 18:29:37 +0300 Subject: [PATCH] Added an etag bypass when updating the feeds. --- hammond-cli/src/main.rs | 3 +- hammond-data/src/index_feed.rs | 57 +++++++++++++++++----------------- 2 files changed, 31 insertions(+), 29 deletions(-) diff --git a/hammond-cli/src/main.rs b/hammond-cli/src/main.rs index 20c77b6..1894804 100644 --- a/hammond-cli/src/main.rs +++ b/hammond-cli/src/main.rs @@ -17,6 +17,7 @@ use hammond_data::index_feed; use hammond_downloader::downloader; // Should probably had made an Enum instead. +// TODO: Refactor to enum, add --force for update #[derive(StructOpt, Debug)] #[structopt(name = "example", about = "An example of StructOpt usage.")] struct Opt { @@ -49,7 +50,7 @@ fn run() -> Result<()> { if args.up { let db = hammond_data::establish_connection(); - index_feed::index_loop(db)?; + index_feed::index_loop(db, false)?; } if args.dl >= 0 { diff --git a/hammond-data/src/index_feed.rs b/hammond-data/src/index_feed.rs index f4e8b2a..077aaf4 100644 --- a/hammond-data/src/index_feed.rs +++ b/hammond-data/src/index_feed.rs @@ -77,10 +77,10 @@ fn insert_return_episode(con: &SqliteConnection, ep: &NewEpisode) -> Result Result<()> { +pub fn index_loop(db: SqliteConnection, force: bool) -> Result<()> { let m = Arc::new(Mutex::new(db)); - let mut f = fetch_feeds(m.clone())?; + let mut f = fetch_feeds(m.clone(), force)?; // f.par_iter_mut().for_each(|&mut (ref mut req, ref source)| { // TODO: Once for_each is stable, uncomment above line and delete collect. @@ -153,6 +153,7 @@ fn index_channel_items( // TODO: After fixing etag/lmod, add sent_etag:bool arg and logic to bypass it. pub fn fetch_feeds( connection: Arc>, + force: bool, ) -> Result> { let tempdb = connection.lock().unwrap(); let mut feeds = dbqueries::get_sources(&tempdb)?; @@ -163,7 +164,7 @@ pub fn fetch_feeds( .map(|x| { let dbmutex = connection.clone(); let db = dbmutex.lock().unwrap(); - refresh_source(&db, x).unwrap() + refresh_source(&db, x, force).unwrap() }) .collect(); @@ -173,24 +174,32 @@ pub fn fetch_feeds( fn refresh_source( connection: &SqliteConnection, feed: &mut Source, + force: bool, ) -> Result<(reqwest::Response, Source)> { use reqwest::header::{ETag, EntityTag, Headers, HttpDate, LastModified}; let client = reqwest::Client::new()?; - let mut headers = Headers::new(); + let req; + match force { + true => req = client.get(feed.uri())?.send()?, + false => { + let mut headers = Headers::new(); - if let Some(foo) = feed.http_etag() { - headers.set(ETag(EntityTag::new(true, foo.to_owned()))); - } + if let Some(foo) = feed.http_etag() { + headers.set(ETag(EntityTag::new(true, foo.to_owned()))); + } - if let Some(foo) = feed.last_modified() { - headers.set(LastModified(foo.parse::()?)); - } + if let Some(foo) = feed.last_modified() { + headers.set(LastModified(foo.parse::()?)); + } + + // FIXME: I have fucked up somewhere here. + // Getting back 200 codes even though I supposedly sent etags. + info!("Headers: {:?}", headers); + req = client.get(feed.uri())?.headers(headers).send()?; + } + }; - info!("Headers: {:?}", headers); - // FIXME: I have fucked up somewhere here. - // Getting back 200 codes even though I supposedly sent etags. - let req = client.get(feed.uri())?.headers(headers).send()?; info!("{}", req.status()); // TODO match on more stuff @@ -210,8 +219,8 @@ fn refresh_source( #[cfg(test)] mod tests { - extern crate tempdir; extern crate rand; + extern crate tempdir; use diesel::prelude::*; use rss; @@ -223,11 +232,6 @@ mod tests { use super::*; - // struct TempDB { - // tmp_dir: tempdir::TempDir, - // db_path: PathBuf, - // db: SqliteConnection, - // } struct TempDB(tempdir::TempDir, PathBuf, SqliteConnection); /// Create and return a Temporary DB. @@ -236,16 +240,13 @@ mod tests { let mut rng = rand::thread_rng(); let tmp_dir = tempdir::TempDir::new("hammond_unit_test").unwrap(); - let db_path = tmp_dir.path().join(format!("hammonddb_{}.db", rng.gen::())); + let db_path = tmp_dir + .path() + .join(format!("hammonddb_{}.db", rng.gen::())); let db = SqliteConnection::establish(db_path.to_str().unwrap()).unwrap(); ::run_migration_on(&db).unwrap(); - // TempDB { - // tmp_dir, - // db_path, - // db, - // } TempDB(tmp_dir, db_path, db) } @@ -267,13 +268,13 @@ mod tests { }) .fold((), |(), _| ()); - index_loop(db).unwrap(); + index_loop(db, true).unwrap(); // index_loop takes oweneship of the dbconnection in order to create mutexes. let db = SqliteConnection::establish(db_path.to_str().unwrap()).unwrap(); // Run again to cover Unique constrains erros. - index_loop(db).unwrap(); + index_loop(db, true).unwrap(); } #[test]