From 3483b857cf687fe3d1568b82ca7bb8ffec938a7c Mon Sep 17 00:00:00 2001 From: njaremko Date: Mon, 17 Jul 2017 00:28:18 -0400 Subject: First commit --- .gitignore | 2 ++ Cargo.toml | 7 +++++++ src/main.rs | 26 ++++++++++++++++++++++++++ src/structs.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 src/main.rs create mode 100644 src/structs.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..eccd7b4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target/ +**/*.rs.bk diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..e538ec4 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "podcast" +version = "0.1.0" +authors = ["njaremko "] + +[dependencies] +rss = "0.7" diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..8c0ec89 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,26 @@ +extern crate rss; + +mod structs; + +use std::fs::File; +use std::io::BufReader; +use rss::{Channel, Item}; +use structs::*; + +fn main() { + let file = File::open("rss.xml").unwrap(); + let channel = Channel::read_from(BufReader::new(file)).unwrap(); + let podcast = Podcast::from(channel); + + for title in podcast.list_titles() { + println!("{}", title); + } + let ep = &podcast.episodes()[0]; + println!( + "{}", + match ep.download_url() { + Some(val) => val, + None => "", + } + ); +} diff --git a/src/structs.rs b/src/structs.rs new file mode 100644 index 0000000..626072b --- /dev/null +++ b/src/structs.rs @@ -0,0 +1,54 @@ +use std::fs::File; +use std::io::BufReader; +use rss::{Channel, Item}; + +pub struct Podcast(Channel); + +pub struct Episode(Item); + +impl From for Podcast { + fn from(channel: Channel) -> Podcast { + Podcast(channel) + } +} + +impl From for Episode { + fn from(item: Item) -> Episode { + Episode(item) + } +} + +impl Podcast { + pub fn episodes(&self) -> Vec { + let mut result = Vec::new(); + + let items = self.0.items().to_vec(); + for item in items { + result.push(Episode::from(item)); + } + result + } + + + pub fn list_titles(&self) -> Vec<&str> { + let mut result = Vec::new(); + + let items = self.0.items(); + for item in items { + match item.title() { + Some(val) => result.push(val), + None => (), + } + } + result + } +} + +impl Episode { + pub fn download_url(&self) -> Option<&str> { + match self.0.enclosure() { + Some(val) => Some(val.url()), + None => None, + } + } +} -- cgit v1.2.3