Finally getting somewhere.

This commit is contained in:
Jordan Petridis 2017-09-19 14:16:40 +03:00
parent 91b314a81f
commit 159ac4cd5d
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
4 changed files with 47 additions and 23 deletions

View File

@ -12,8 +12,8 @@ CREATE TABLE `episode` (
`local_uri` TEXT,
`description` TEXT,
`published_date` TEXT NOT NULL,
`epoch` INTEGER NOT NULL DEFAULT 0,
`length` INTEGER DEFAULT 0,
`epoch` INTEGER NOT NULL,
`length` INTEGER,
`guid` TEXT,
`podcast_id` INTEGER NOT NULL
);

View File

@ -12,7 +12,6 @@ pub fn get_pd_episodes(con: &SqliteConnection, parent: &Podcast) -> QueryResult<
let eps = Episode::belonging_to(parent).load::<Episode>(con);
eps
}
pub fn get_sources(con: &SqliteConnection) -> QueryResult<Vec<Source>>{
let s = source.load::<Source>(con);

View File

@ -3,7 +3,7 @@ use diesel;
use schema;
use dbqueries;
use errors::*;
use models::{NewPodcast, NewSource};
use models::{NewPodcast, NewSource, Source};
pub fn foo() {
let inpt = vec![
@ -24,8 +24,7 @@ pub fn foo() {
}
}
let f = dbqueries::get_sources(&db);
info!("{:?}", f);
index_loop(db);
}
fn insert_source(connection: &SqliteConnection, url: &str) -> Result<()> {
@ -37,3 +36,23 @@ fn insert_source(connection: &SqliteConnection, url: &str) -> Result<()> {
Ok(())
}
pub fn index_loop(db: SqliteConnection) -> Result<()> {
// let db = ::establish_connection();
use parse_feeds;
let f = dbqueries::get_sources(&db);
for feed in f.unwrap().iter_mut() {
info!("{:?}", feed.id());
// This method will defently get split and nuked
// but for now its poc
let chan = feed.get_podcast_chan(&db)?;
let pd = parse_feeds::parse_podcast(&chan, feed.id())?;
info!("{:#?}", pd);
// info!("{:?}", chan);
}
Ok(())
}

View File

@ -1,5 +1,8 @@
use reqwest;
use rss::Channel;
use diesel::SaveChangesDsl;
use SqliteConnection;
use reqwest::header::{ETag, LastModified};
use schema::{episode, podcast, source};
use errors::*;
@ -63,10 +66,10 @@ impl<'a> Source {
self.http_etag
}
// This is a mess
pub fn get_podcast(&mut self) -> Result<NewPodcast> {
/// Fetch the xml feed from the source url, update the etag headers,
/// and parse the feed into an rss:Channel and return it.
pub fn get_podcast_chan(&mut self, con: &SqliteConnection) -> Result<Channel> {
use std::io::Read;
use reqwest::header::*;
use std::str::FromStr;
let mut req = reqwest::get(&self.uri)?;
@ -78,28 +81,31 @@ impl<'a> Source {
let headers = req.headers();
debug!("{:#?}", headers);
// for h in headers.iter() {
// info!("{}: {}", h.name(), h.value_string());
// }
// let etag = headers.get_raw("ETag").unwrap();
let etag = headers.get::<ETag>();
let lst_mod = headers.get::<LastModified>();
info!("Etag: {:?}", etag);
info!("Last mod: {:?}", lst_mod);
self.update(con, etag, lst_mod)?;
let chan = Channel::from_str(&buf)?;
// let foo = ::parse_feeds::parse_podcast(&chan, self.id())?;
Ok(chan)
}
pub fn update(
&mut self,
con: &SqliteConnection,
etag: Option<&ETag>,
lmod: Option<&LastModified>,
) -> Result<()> {
// This is useless atm since theres no db passed to save the change
// but I needed to have it somewhere implemented for later.
self.http_etag = etag.map(|x| x.tag().to_string().to_owned());
self.last_modified = lst_mod.map(|x| format!("{}", x));
self.last_modified = lmod.map(|x| format!("{}", x));
info!("Self etag: {:?}", self.http_etag);
info!("Self last_mod: {:?}", self.last_modified);
// Maybe it would be better to just return buf
let chan = Channel::from_str(&buf)?;
let foo = ::parse_feeds::parse_podcast(&chan, self.id())?;
Ok(foo)
self.save_changes::<Source>(con)?;
Ok(())
}
}