Added an etag bypass when updating the feeds.
This commit is contained in:
parent
3b4acf9fc3
commit
cbc0692482
@ -17,6 +17,7 @@ use hammond_data::index_feed;
|
|||||||
use hammond_downloader::downloader;
|
use hammond_downloader::downloader;
|
||||||
|
|
||||||
// Should probably had made an Enum instead.
|
// Should probably had made an Enum instead.
|
||||||
|
// TODO: Refactor to enum, add --force for update
|
||||||
#[derive(StructOpt, Debug)]
|
#[derive(StructOpt, Debug)]
|
||||||
#[structopt(name = "example", about = "An example of StructOpt usage.")]
|
#[structopt(name = "example", about = "An example of StructOpt usage.")]
|
||||||
struct Opt {
|
struct Opt {
|
||||||
@ -49,7 +50,7 @@ fn run() -> Result<()> {
|
|||||||
|
|
||||||
if args.up {
|
if args.up {
|
||||||
let db = hammond_data::establish_connection();
|
let db = hammond_data::establish_connection();
|
||||||
index_feed::index_loop(db)?;
|
index_feed::index_loop(db, false)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
if args.dl >= 0 {
|
if args.dl >= 0 {
|
||||||
|
|||||||
@ -77,10 +77,10 @@ fn insert_return_episode(con: &SqliteConnection, ep: &NewEpisode) -> Result<Epis
|
|||||||
Ok(dbqueries::load_episode(con, &ep.uri.unwrap())?)
|
Ok(dbqueries::load_episode(con, &ep.uri.unwrap())?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn index_loop(db: SqliteConnection) -> Result<()> {
|
pub fn index_loop(db: SqliteConnection, force: bool) -> Result<()> {
|
||||||
let m = Arc::new(Mutex::new(db));
|
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)| {
|
// f.par_iter_mut().for_each(|&mut (ref mut req, ref source)| {
|
||||||
// TODO: Once for_each is stable, uncomment above line and delete collect.
|
// 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.
|
// TODO: After fixing etag/lmod, add sent_etag:bool arg and logic to bypass it.
|
||||||
pub fn fetch_feeds(
|
pub fn fetch_feeds(
|
||||||
connection: Arc<Mutex<SqliteConnection>>,
|
connection: Arc<Mutex<SqliteConnection>>,
|
||||||
|
force: bool,
|
||||||
) -> Result<Vec<(reqwest::Response, Source)>> {
|
) -> Result<Vec<(reqwest::Response, Source)>> {
|
||||||
let tempdb = connection.lock().unwrap();
|
let tempdb = connection.lock().unwrap();
|
||||||
let mut feeds = dbqueries::get_sources(&tempdb)?;
|
let mut feeds = dbqueries::get_sources(&tempdb)?;
|
||||||
@ -163,7 +164,7 @@ pub fn fetch_feeds(
|
|||||||
.map(|x| {
|
.map(|x| {
|
||||||
let dbmutex = connection.clone();
|
let dbmutex = connection.clone();
|
||||||
let db = dbmutex.lock().unwrap();
|
let db = dbmutex.lock().unwrap();
|
||||||
refresh_source(&db, x).unwrap()
|
refresh_source(&db, x, force).unwrap()
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
@ -173,24 +174,32 @@ pub fn fetch_feeds(
|
|||||||
fn refresh_source(
|
fn refresh_source(
|
||||||
connection: &SqliteConnection,
|
connection: &SqliteConnection,
|
||||||
feed: &mut Source,
|
feed: &mut Source,
|
||||||
|
force: bool,
|
||||||
) -> Result<(reqwest::Response, Source)> {
|
) -> Result<(reqwest::Response, Source)> {
|
||||||
use reqwest::header::{ETag, EntityTag, Headers, HttpDate, LastModified};
|
use reqwest::header::{ETag, EntityTag, Headers, HttpDate, LastModified};
|
||||||
|
|
||||||
let client = reqwest::Client::new()?;
|
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() {
|
if let Some(foo) = feed.http_etag() {
|
||||||
headers.set(ETag(EntityTag::new(true, foo.to_owned())));
|
headers.set(ETag(EntityTag::new(true, foo.to_owned())));
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(foo) = feed.last_modified() {
|
if let Some(foo) = feed.last_modified() {
|
||||||
headers.set(LastModified(foo.parse::<HttpDate>()?));
|
headers.set(LastModified(foo.parse::<HttpDate>()?));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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());
|
info!("{}", req.status());
|
||||||
|
|
||||||
// TODO match on more stuff
|
// TODO match on more stuff
|
||||||
@ -210,8 +219,8 @@ fn refresh_source(
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
|
||||||
extern crate tempdir;
|
|
||||||
extern crate rand;
|
extern crate rand;
|
||||||
|
extern crate tempdir;
|
||||||
|
|
||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
use rss;
|
use rss;
|
||||||
@ -223,11 +232,6 @@ mod tests {
|
|||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
// struct TempDB {
|
|
||||||
// tmp_dir: tempdir::TempDir,
|
|
||||||
// db_path: PathBuf,
|
|
||||||
// db: SqliteConnection,
|
|
||||||
// }
|
|
||||||
struct TempDB(tempdir::TempDir, PathBuf, SqliteConnection);
|
struct TempDB(tempdir::TempDir, PathBuf, SqliteConnection);
|
||||||
|
|
||||||
/// Create and return a Temporary DB.
|
/// Create and return a Temporary DB.
|
||||||
@ -236,16 +240,13 @@ mod tests {
|
|||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::thread_rng();
|
||||||
|
|
||||||
let tmp_dir = tempdir::TempDir::new("hammond_unit_test").unwrap();
|
let tmp_dir = tempdir::TempDir::new("hammond_unit_test").unwrap();
|
||||||
let db_path = tmp_dir.path().join(format!("hammonddb_{}.db", rng.gen::<usize>()));
|
let db_path = tmp_dir
|
||||||
|
.path()
|
||||||
|
.join(format!("hammonddb_{}.db", rng.gen::<usize>()));
|
||||||
|
|
||||||
let db = SqliteConnection::establish(db_path.to_str().unwrap()).unwrap();
|
let db = SqliteConnection::establish(db_path.to_str().unwrap()).unwrap();
|
||||||
::run_migration_on(&db).unwrap();
|
::run_migration_on(&db).unwrap();
|
||||||
|
|
||||||
// TempDB {
|
|
||||||
// tmp_dir,
|
|
||||||
// db_path,
|
|
||||||
// db,
|
|
||||||
// }
|
|
||||||
TempDB(tmp_dir, db_path, db)
|
TempDB(tmp_dir, db_path, db)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -267,13 +268,13 @@ mod tests {
|
|||||||
})
|
})
|
||||||
.fold((), |(), _| ());
|
.fold((), |(), _| ());
|
||||||
|
|
||||||
index_loop(db).unwrap();
|
index_loop(db, true).unwrap();
|
||||||
|
|
||||||
// index_loop takes oweneship of the dbconnection in order to create mutexes.
|
// index_loop takes oweneship of the dbconnection in order to create mutexes.
|
||||||
let db = SqliteConnection::establish(db_path.to_str().unwrap()).unwrap();
|
let db = SqliteConnection::establish(db_path.to_str().unwrap()).unwrap();
|
||||||
|
|
||||||
// Run again to cover Unique constrains erros.
|
// Run again to cover Unique constrains erros.
|
||||||
index_loop(db).unwrap();
|
index_loop(db, true).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user