diff --git a/migrations/2017-09-15-001128_init_schema/up.sql b/migrations/2017-09-15-001128_init_schema/up.sql index c8cf866..c0c95ba 100644 --- a/migrations/2017-09-15-001128_init_schema/up.sql +++ b/migrations/2017-09-15-001128_init_schema/up.sql @@ -4,6 +4,7 @@ CREATE TABLE `episode` ( `uri` TEXT NOT NULL, `local_uri` TEXT, `description` TEXT, + `published_date` TEXT NOT NULL, `epoch` INTEGER NOT NULL DEFAULT 0, `length` INTEGER NOT NULL DEFAULT 0, `guid` TEXT, diff --git a/src/models.rs b/src/models.rs index 0fd7e62..b421821 100644 --- a/src/models.rs +++ b/src/models.rs @@ -11,9 +11,11 @@ pub struct Episode { uri: String, local_uri: Option, description: Option, + published_date: String, thumbnail: Option, - lenght: Option, + length: Option, guid: Option, + epoch: i32, podcast_id: i32, } @@ -36,14 +38,15 @@ pub struct Podcast { #[table_name = "episode"] #[derive(Debug, Clone)] pub struct NewEpisode<'a> { - title: &'a str, - uri: &'a str, - local_uri: Option<&'a str>, - description: Option<&'a str>, - thumbnail: Option<&'a str>, - lenght: Option, - guid: Option<&'a str>, - podcast_id: i32, + pub title: &'a str, + pub uri: &'a str, + pub local_uri: Option<&'a str>, + pub description: Option<&'a str>, + pub published_date: &'a str, + pub length: Option, + pub guid: Option<&'a str>, + pub epoch: i32, + pub podcast_id: i32, } #[derive(Insertable)] diff --git a/src/parse_feeds.rs b/src/parse_feeds.rs index c912d3a..afd52cf 100644 --- a/src/parse_feeds.rs +++ b/src/parse_feeds.rs @@ -1,4 +1,4 @@ -use rss::Channel; +use rss::{Channel, Item}; use models; pub fn parse_podcast<'a>(pd_chan: &'a Channel, uri: &'a str) -> models::NewPodcast<'a> { @@ -29,6 +29,40 @@ pub fn parse_podcast<'a>(pd_chan: &'a Channel, uri: &'a str) -> models::NewPodca } } +pub fn parse_episode<'a>(item: &'a Item, parent_id: i32) -> models::NewEpisode<'a> { + + let title = item.title().unwrap(); + + let description = item.description(); + let guid = Some(item.guid().unwrap().value()); + + let uri = item.enclosure().unwrap().url(); + + // FIXME: + // probably needs to be removed from NewEpisode, + // and have seperate logic to handle local_files + let local_uri = None; + + let pub_date = item.pub_date().unwrap(); + + // FIXME: parse pub_date to epoch later + let epoch = 0; + + let length = Some(item.enclosure().unwrap().length().parse().unwrap()); + + models::NewEpisode { + title, + uri, + local_uri, + description, + length, + published_date: pub_date, + epoch, + guid, + podcast_id: parent_id, + } +} + #[cfg(test)] mod tests { @@ -95,7 +129,28 @@ mod tests { assert_eq!(pd.description, Some(descr)); assert_eq!(pd.last_modified, None); assert_eq!(pd.http_etag, None); - assert_eq!(pd.image_uri, Some("https://assets.propublica.org/propublica-rss-logo.png")); + assert_eq!( + pd.image_uri, + Some("https://assets.propublica.org/propublica-rss-logo.png") + ); } + #[test] + fn test_parse_episode() { + let file = File::open("tests/feeds/Intercepted.xml").unwrap(); + let channel = Channel::read_from(BufReader::new(file)).unwrap(); + let firstitem = channel.items().first().unwrap(); + let descr = "NSA whistleblower Edward Snowden discusses the massive Equifax data breach and allegations of Russian interference in the US election. Commentator Shaun King explains his call for a boycott of the NFL and talks about his campaign to bring violent neo-Nazis to justice. Rapper Open Mike Eagle performs."; + + // println!("{:#?}", firstitem); + let it = parse_episode(&firstitem, 0); + + assert_eq!(it.title, "The Super Bowl of Racism"); + assert_eq!(it.uri, "http://traffic.megaphone.fm/PPY6458293736.mp3"); + assert_eq!(it.description, Some(descr)); + assert_eq!(it.length, Some(66738886)); + assert_eq!(it.guid, Some("7df4070a-9832-11e7-adac-cb37b05d5e24")); + assert_eq!(it.published_date, "Wed, 13 Sep 2017 10:00:00 -0000"); + + } } \ No newline at end of file diff --git a/src/schema.rs b/src/schema.rs index ac796be..97bc52d 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -5,6 +5,7 @@ table! { uri -> Text, local_uri -> Nullable, description -> Nullable, + published_date -> Text, epoch -> Integer, length -> Integer, guid -> Nullable,