aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authornjaremko2017-07-17 23:16:48 -0400
committernjaremko2017-07-17 23:16:48 -0400
commit80f4314bedc4ec6c727287000d3f3dd775b894f5 (patch)
treefda3602de5e1f0c54142c74d546b5a12d341a143 /src
parent9a7d334dd698e450851bf30b432de64f1a7ef748 (diff)
downloadpodcast-80f4314bedc4ec6c727287000d3f3dd775b894f5.tar.bz2
Work offline
Diffstat (limited to 'src')
-rw-r--r--src/actions.rs41
-rw-r--r--src/main.rs2
-rw-r--r--src/structs.rs2
3 files changed, 41 insertions, 4 deletions
diff --git a/src/actions.rs b/src/actions.rs
index 56e2325..1319cd6 100644
--- a/src/actions.rs
+++ b/src/actions.rs
@@ -1,7 +1,11 @@
use regex::Regex;
use std::process::Command;
use structs::*;
+use reqwest;
use utils::*;
+use std::io::{Read, Write};
+use std::fs::{DirBuilder, File};
+use rss::Channel;
pub fn list_episodes(state: State, search: &str) {
let re = Regex::new(&search).unwrap();
@@ -22,6 +26,24 @@ pub fn list_episodes(state: State, search: &str) {
}
}
+pub fn update_rss(state: State) {
+ let subs = state.subscriptions();
+ for sub in subs {
+ let mut path = get_podcast_dir();
+ path.push(".rss");
+ DirBuilder::new().recursive(true).create(&path).unwrap();
+ let channel = Channel::from_url(&sub.url).unwrap();
+ let mut filename = String::from(channel.title());
+ filename.push_str(".xml");
+ path.push(filename);
+ let mut file = File::create(&path).unwrap();
+ let mut resp = reqwest::get(&sub.url).unwrap();
+ let mut content: Vec<u8> = Vec::new();
+ resp.read_to_end(&mut content).unwrap();
+ file.write_all(&content).unwrap();
+ }
+}
+
pub fn list_subscriptions(state: State) {
for podcast in state.subscriptions() {
println!("{}", podcast.name);
@@ -58,14 +80,27 @@ pub fn download_all(state: State, p_search: &str) {
pub fn play_episode(state: State, p_search: &str, ep_num_string: &str) {
let re_pod = Regex::new(&p_search).unwrap();
let ep_num = ep_num_string.parse::<usize>().unwrap();
- let mut path = get_podcast_dir();
for subscription in state.subscriptions() {
if re_pod.is_match(&subscription.name) {
- let podcast = Podcast::from_url(&subscription.url).unwrap();
+ let mut path = get_podcast_dir();
+ path.push(".rss");
+ DirBuilder::new().recursive(true).create(&path).unwrap();
+
+ let mut filename = String::from(subscription.name);
+ filename.push_str(".xml");
+ path.push(filename);
+
+ let mut file = File::open(&path).unwrap();
+ let mut content: Vec<u8> = Vec::new();
+ file.read_to_end(&mut content).unwrap();
+
+ let podcast = Podcast::from(Channel::read_from(content.as_slice()).unwrap());
let episodes = podcast.episodes();
let episode = episodes[episodes.len() - ep_num].clone();
- let mut filename = String::from(episode.title().unwrap());
+
+ filename = String::from(episode.title().unwrap());
filename.push_str(episode.extension().unwrap());
+ path = get_podcast_dir();
path.push(podcast.title());
path.push(filename);
match path.exists() {
diff --git a/src/main.rs b/src/main.rs
index 2f93899..4134edb 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -114,7 +114,7 @@ fn main() {
)
}
Some("search") => (),
- Some("update") => (),
+ Some("update") => update_rss(state),
_ => (),
}
}
diff --git a/src/structs.rs b/src/structs.rs
index c304832..2546139 100644
--- a/src/structs.rs
+++ b/src/structs.rs
@@ -5,6 +5,7 @@ use std::collections::BTreeSet;
use std::fs::{DirBuilder, File};
use std::io::{self, Read, Write};
use utils::*;
+use actions::*;
#[derive(Serialize, Deserialize, Clone)]
pub struct Subscription {
@@ -44,6 +45,7 @@ impl State {
Err(err) => println!("{}", err),
_ => (),
}
+ update_rss(self.clone());
}
pub fn subscriptions(&self) -> Vec<Subscription> {