From 91b314a81fcb5d3d56a8041e575d017321dac8f1 Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Tue, 19 Sep 2017 12:16:12 +0300 Subject: [PATCH] Still figuring out the schema. --- .../2017-09-15-001128_init_schema/up.sql | 1 - src/models.rs | 31 +++++++++++++++---- src/parse_feeds.rs | 14 +++------ src/schema.rs | 1 - 4 files changed, 30 insertions(+), 17 deletions(-) diff --git a/migrations/2017-09-15-001128_init_schema/up.sql b/migrations/2017-09-15-001128_init_schema/up.sql index 503404f..53c656a 100644 --- a/migrations/2017-09-15-001128_init_schema/up.sql +++ b/migrations/2017-09-15-001128_init_schema/up.sql @@ -21,7 +21,6 @@ CREATE TABLE `episode` ( CREATE TABLE `podcast` ( `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, `title` TEXT NOT NULL UNIQUE, - `uri` TEXT NOT NULL UNIQUE, `link` TEXT NOT NULL, `description` TEXT NOT NULL, `image_uri` TEXT, diff --git a/src/models.rs b/src/models.rs index 44f19bd..0d4c0d1 100644 --- a/src/models.rs +++ b/src/models.rs @@ -30,7 +30,6 @@ pub struct Episode { pub struct Podcast { id: i32, title: String, - uri: String, link: String, description: String, image_uri: Option, @@ -48,6 +47,22 @@ pub struct Source { } impl<'a> Source { + pub fn id(&self) -> i32 { + self.id + } + + pub fn uri(&self) -> &str { + &self.uri + } + + pub fn last_modified(self) -> Option { + self.last_modified + } + + pub fn http_etag(self) -> Option { + self.http_etag + } + // This is a mess pub fn get_podcast(&mut self) -> Result { use std::io::Read; @@ -73,13 +88,16 @@ impl<'a> Source { info!("Etag: {:?}", etag); info!("Last mod: {:?}", lst_mod); + // 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)); 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.uri)?; + let foo = ::parse_feeds::parse_podcast(&chan, self.id())?; Ok(foo) } @@ -125,17 +143,18 @@ pub struct NewEpisode<'a> { #[derive(Debug, Clone)] pub struct NewPodcast { pub title: String, - pub uri: String, pub link: String, pub description: String, pub image_uri: Option, + pub source_id: i32, } - impl<'a> NewPodcast { - pub fn from_url(uri: &'a str) -> Result { + // pub fn new(parent: &Source) {} + + pub fn from_url(uri: &'a str, parent: &Source) -> Result { let chan = Channel::from_url(uri)?; - let foo = ::parse_feeds::parse_podcast(&chan, uri)?; + let foo = ::parse_feeds::parse_podcast(&chan, parent.id())?; Ok(foo) } } \ No newline at end of file diff --git a/src/parse_feeds.rs b/src/parse_feeds.rs index 142e80c..176e8a9 100644 --- a/src/parse_feeds.rs +++ b/src/parse_feeds.rs @@ -2,7 +2,7 @@ use rss::{Channel, Item}; use models; use errors::*; -pub fn parse_podcast(chan: &Channel, uri: &str) -> Result { +pub fn parse_podcast(chan: &Channel, source_id: i32) -> Result { let title = chan.title().to_owned(); let link = chan.link().to_owned(); @@ -17,11 +17,11 @@ pub fn parse_podcast(chan: &Channel, uri: &str) -> Result { let image_uri = chan.image().map(|foo| foo.url().to_owned()); let foo = models::NewPodcast { - uri: uri.to_owned(), title, link, description, image_uri, + source_id, }; Ok(foo) } @@ -80,13 +80,9 @@ mod tests { // println!("{:#?}", channel); let descr = "The people behind The Intercept’s fearless reporting and incisive commentary—Jeremy Scahill, Glenn Greenwald, Betsy Reed and others—discuss the crucial issues of our time: national security, civil liberties, foreign policy, and criminal justice. Plus interviews with artists, thinkers, and newsmakers who challenge our preconceptions about the world we live in."; - let pd = parse_podcast(&channel, uri).unwrap(); + let pd = parse_podcast(&channel, 0).unwrap(); assert_eq!(pd.title, "Intercepted with Jeremy Scahill".to_string()); - assert_eq!( - pd.uri, - "https://feeds.feedburner.com/InterceptedWithJeremyScahill".to_string() - ); assert_eq!(pd.link, "https://theintercept.com/podcasts".to_string()); assert_eq!(pd.description, descr.to_string()); assert_eq!(pd.image_uri, None); @@ -99,7 +95,7 @@ mod tests { // println!("{:#?}", channel); let descr = "An open show powered by community LINUX Unplugged takes the best attributes of open collaboration and focuses them into a weekly lifestyle show about Linux."; - let pd = parse_podcast(&channel, uri).unwrap(); + let pd = parse_podcast(&channel, 0).unwrap(); assert_eq!(pd.title, "LINUX Unplugged Podcast".to_string()); assert_eq!(pd.link, "http://www.jupiterbroadcasting.com/".to_string()); @@ -119,7 +115,7 @@ mod tests { // println!("{:#?}", channel); let descr = "Latest Articles and Investigations from ProPublica, an independent, non-profit newsroom that produces investigative journalism in the public interest."; - let pd = parse_podcast(&channel, uri).unwrap(); + let pd = parse_podcast(&channel, 0).unwrap(); assert_eq!( pd.title, diff --git a/src/schema.rs b/src/schema.rs index d443c46..868f85f 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -17,7 +17,6 @@ table! { podcast (id) { id -> Integer, title -> Text, - uri -> Text, link -> Text, description -> Text, image_uri -> Nullable,