aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Cargo.toml4
-rw-r--r--src/actions.rs44
-rw-r--r--src/structs.rs30
-rw-r--r--src/utils.rs70
4 files changed, 91 insertions, 57 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 9d47ce4..f7d2d97 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,9 +1,11 @@
[package]
name = "podcast"
-version = "0.3.0"
+version = "0.3.1"
authors = ["njaremko <njaremko@gmail.com>"]
description = "A command line podcast player"
license = "GPL-3.0"
+readme = "README.md"
+keywords = ["audio", "cli", "podcast", "music"]
repository = "https://github.com/njaremko/podcast"
[dependencies]
diff --git a/src/actions.rs b/src/actions.rs
index 6f3cfc2..3428f79 100644
--- a/src/actions.rs
+++ b/src/actions.rs
@@ -104,12 +104,19 @@ pub fn download_range(state: &State, p_search: &str, e_search: &str) {
for subscription in &state.subs {
if re_pod.is_match(&subscription.title) {
- let podcast = Podcast::from_url(&subscription.url).unwrap();
- match parse_download_episodes(e_search) {
- Ok(episodes_to_download) => podcast.download_specific(episodes_to_download),
- Err(err) => eprintln!("{}", err),
+ match Podcast::from_title(&subscription.title) {
+ Ok(podcast) => {
+ match parse_download_episodes(e_search) {
+ Ok(episodes_to_download) => {
+ if let Err(err) = podcast.download_specific(episodes_to_download) {
+ eprintln!("Error: {}", err);
+ }
+ }
+ Err(err) => eprintln!("Error: {}", err),
+ }
+ }
+ Err(err) => eprintln!("Error: {}", err),
}
- return;
}
}
}
@@ -120,10 +127,14 @@ pub fn download_episode(state: &State, p_search: &str, e_search: &str) {
for subscription in &state.subs {
if re_pod.is_match(&subscription.title) {
- let podcast = Podcast::from_url(&subscription.url).unwrap();
- let episodes = podcast.episodes();
- if let Err(err) = episodes[episodes.len() - ep_num].download(podcast.title()) {
- eprintln!("{}", err);
+ match Podcast::from_title(&subscription.title) {
+ Ok(podcast) => {
+ let episodes = podcast.episodes();
+ if let Err(err) = episodes[episodes.len() - ep_num].download(podcast.title()) {
+ eprintln!("{}", err);
+ }
+ }
+ Err(err) => eprintln!("Error: {}", err),
}
}
}
@@ -134,8 +145,14 @@ pub fn download_all(state: &State, p_search: &str) {
for subscription in &state.subs {
if re_pod.is_match(&subscription.title) {
- let podcast = Podcast::from_url(&subscription.url).unwrap();
- podcast.download();
+ match Podcast::from_title(&subscription.title) {
+ Ok(podcast) => {
+ if let Err(err) = podcast.download() {
+ eprintln!("{}", err);
+ }
+ }
+ Err(err) => eprintln!("Error: {}", err),
+ }
}
}
}
@@ -143,8 +160,7 @@ 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();
- path.push(".rss");
+ let mut path = get_xml_dir();
if let Err(err) = DirBuilder::new().recursive(true).create(&path) {
eprintln!(
"Couldn't create directory: {}\nReason: {}",
@@ -184,7 +200,7 @@ pub fn play_episode(state: &State, p_search: &str, ep_num_string: &str) {
fn launch_player(url: &str) {
if let Err(_) = launch_mpv(&url) {
- launch_vlc(url)
+ launch_vlc(&url)
}
}
diff --git a/src/structs.rs b/src/structs.rs
index 730aab4..a7e82ee 100644
--- a/src/structs.rs
+++ b/src/structs.rs
@@ -6,7 +6,7 @@ use rss::{self, Channel, Item};
use serde_json;
use std::collections::BTreeSet;
use std::fs::{self, DirBuilder, File};
-use std::io::{self, Read, Write};
+use std::io::{self, BufReader, Read, Write};
use utils::*;
use yaml_rust::YamlLoader;
@@ -162,6 +162,7 @@ impl Podcast {
self.0.link()
}
+ #[allow(dead_code)]
pub fn from_url(url: &str) -> Result<Podcast, rss::Error> {
match Channel::from_url(url) {
Ok(val) => Ok(Podcast::from(val)),
@@ -169,6 +170,23 @@ impl Podcast {
}
}
+ pub fn from_title(title: &str) -> Result<Podcast, String> {
+ let mut path = get_xml_dir();
+ let mut filename = String::from(title);
+ filename.push_str(".xml");
+ path.push(filename);
+
+ match File::open(&path) {
+ Ok(file) => {
+ match Channel::read_from(BufReader::new(file)) {
+ Ok(podcast) => return Ok(Podcast::from(podcast)),
+ Err(err) => return Err(format!("Error: {}", err)),
+ }
+ }
+ Err(err) => return Err(format!("Error: {}", err)),
+ }
+ }
+
pub fn episodes(&self) -> Vec<Episode> {
let mut result = Vec::new();
@@ -179,11 +197,11 @@ impl Podcast {
result
}
- pub fn download(&self) {
+ pub fn download(&self) -> Result<(), io::Error> {
let mut path = get_podcast_dir();
path.push(self.title());
- let downloaded = already_downloaded(self.title());
+ let downloaded = already_downloaded(self.title())?;
self.episodes().par_iter().for_each(
|ref i| if let Some(ep_title) =
@@ -196,13 +214,14 @@ impl Podcast {
}
},
);
+ Ok(())
}
- pub fn download_specific(&self, episode_numbers: Vec<usize>) {
+ pub fn download_specific(&self, episode_numbers: Vec<usize>) -> Result<(), io::Error> {
let mut path = get_podcast_dir();
path.push(self.title());
- let downloaded = already_downloaded(self.title());
+ let downloaded = already_downloaded(self.title())?;
let episodes = self.episodes();
episode_numbers.par_iter().for_each(
@@ -216,6 +235,7 @@ impl Podcast {
}
},
);
+ Ok(())
}
}
diff --git a/src/utils.rs b/src/utils.rs
index 113517e..ab66574 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -1,14 +1,16 @@
use std::collections::HashSet;
use std::env;
-use std::fs::DirBuilder;
-use std::fs;
use std::num::ParseIntError;
use std::path::PathBuf;
+use std::fs::{self, DirBuilder};
+use std::io;
-pub fn trim_extension(filename: &str) -> String {
+pub fn trim_extension(filename: &str) -> Option<String> {
let name = String::from(filename);
- let index = name.rfind('.').unwrap();
- String::from(&name[0..index])
+ match name.rfind('.') {
+ Some(index) => Some(String::from(&name[0..index])),
+ None => None,
+ }
}
pub fn find_extension(input: &str) -> Option<&str> {
@@ -39,39 +41,37 @@ pub fn create_directories() -> Result<(), String> {
Ok(())
}
-pub fn already_downloaded(dir: &str) -> HashSet<String> {
+pub fn already_downloaded(dir: &str) -> Result<HashSet<String>, io::Error> {
let mut result = HashSet::new();
let mut path = get_podcast_dir();
path.push(dir);
- if let Ok(entries) = fs::read_dir(path) {
- for entry in entries {
- if let Ok(entry) = entry {
- match entry.file_name().into_string() {
- Ok(val) => {
- let name = String::from(val);
- let index = name.find('.').unwrap();
- result.insert(String::from(&name[0..index]));
- }
- Err(_) => {
- println!(
- "OsString: {:?} couldn't be converted to String",
- entry.file_name()
- );
- }
- }
+ let entries = fs::read_dir(path)?;
+ for entry in entries {
+ let entry = entry?;
+ match entry.file_name().into_string() {
+ Ok(val) => {
+ let name = String::from(val);
+ let index = name.find('.').unwrap();
+ result.insert(String::from(&name[0..index]));
+ }
+ Err(_) => {
+ println!(
+ "OsString: {:?} couldn't be converted to String",
+ entry.file_name()
+ );
}
}
}
- result
+ Ok(result)
}
pub fn get_podcast_dir() -> PathBuf {
match env::var_os("PODCAST") {
Some(val) => PathBuf::from(val),
None => {
- let mut path = env::home_dir().unwrap();
+ let mut path = env::home_dir().expect("Couldn't find your home directory");
path.push("Podcasts");
path
}
@@ -79,19 +79,15 @@ pub fn get_podcast_dir() -> PathBuf {
}
pub fn get_sub_file() -> PathBuf {
- match env::var_os("PODCAST") {
- Some(val) => {
- let mut path = PathBuf::from(val);
- path.push(".subscriptions");
- path
- }
- None => {
- let mut path = env::home_dir().unwrap();
- path.push("Podcasts");
- path.push(".subscriptions");
- path
- }
- }
+ let mut path = get_podcast_dir();
+ path.push(".subscriptions");
+ path
+}
+
+pub fn get_xml_dir() -> PathBuf {
+ let mut path = get_podcast_dir();
+ path.push(".rss");
+ path
}
pub fn parse_download_episodes(e_search: &str) -> Result<Vec<usize>, ParseIntError> {