aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorNathan Jaremko2017-11-30 00:29:16 -0500
committerNathan Jaremko2017-11-30 00:29:16 -0500
commitbdcd9a9e0739796bc568de9c4296026a9ea147a6 (patch)
tree7da742ad5db93fce8d86caac98c5ad6477ca7bc7 /src
parent3313491352b1f0b2a62ae12328bd99ac3b727a66 (diff)
downloadpodcast-bdcd9a9e0739796bc568de9c4296026a9ea147a6.tar.bz2
add ability to unsubscribe and some more stuff
Diffstat (limited to 'src')
-rw-r--r--src/actions.rs96
-rw-r--r--src/main.rs41
-rw-r--r--src/structs.rs16
3 files changed, 142 insertions, 11 deletions
diff --git a/src/actions.rs b/src/actions.rs
index 7d554b5..31e0e6c 100644
--- a/src/actions.rs
+++ b/src/actions.rs
@@ -8,6 +8,7 @@ use std::io::{self, BufReader, Read, Write};
use std::process::Command;
use structs::*;
use utils::*;
+use toml;
pub fn list_episodes(search: &str) {
let re = Regex::new(&format!("(?i){}", &search)).expect("Failed to parse regex");
@@ -102,7 +103,7 @@ pub fn list_subscriptions(state: &State) {
}
pub fn download_range(state: &State, p_search: &str, e_search: &str) {
- let re_pod = Regex::new(&format!("(?i){}", &p_search)).expect("Failed to parse regex");;
+ let re_pod = Regex::new(&format!("(?i){}", &p_search)).expect("Failed to parse regex");
for subscription in &state.subs {
if re_pod.is_match(&subscription.title) {
@@ -196,8 +197,24 @@ pub fn play_episode(state: &State, p_search: &str, ep_num_string: &str) {
}
}
-pub fn check_for_update(state: &mut State) {
+pub fn check_for_update(version: &str) {
println!("Checking for updates...");
+ let resp: String = reqwest::get(
+ "https://raw.githubusercontent.com/njaremko/podcast/master/Cargo.toml",
+ ).unwrap()
+ .text()
+ .unwrap();
+
+ //println!("{}", resp);
+ match resp.parse::<toml::Value>() {
+ Ok(config) => {
+ let latest = config["package"]["version"].as_str().unwrap();
+ if version != latest {
+ println!("New version avaliable: {}", latest);
+ }
+ },
+ Err(err) => eprintln!("{}", err),
+ }
}
fn launch_player(url: &str) {
@@ -206,7 +223,7 @@ fn launch_player(url: &str) {
}
}
-fn launch_mpv(url: &str) -> Result<(), io::Error> {
+fn launch_mpv(url: &str) -> io::Result<()> {
if let Err(err) = Command::new("mpv")
.args(&["--audio-display=no", "--ytdl=no", url])
.status()
@@ -232,3 +249,76 @@ fn launch_vlc(url: &str) {
}
}
}
+
+
+pub fn remove_podcast(state: &mut State, p_search: &str) {
+ if p_search == "*" {
+ match Podcast::delete_all() {
+ Ok(_) => println!("Success"),
+ Err(err) => eprintln!("Error: {}", err),
+ }
+ return;
+ }
+
+ let re_pod = Regex::new(&format!("(?i){}", &p_search)).expect("Failed to parse regex");
+
+ for subscription in 0..state.subs.len() {
+ let title = state.subs[subscription].title.clone();
+ if re_pod.is_match(&title) {
+ state.subs.remove(subscription);
+ match Podcast::delete(&title) {
+ Ok(_) => println!("Success"),
+ Err(err) => eprintln!("Error: {}", err),
+ }
+ break;
+ }
+ }
+}
+
+pub fn print_completion(arg: &str) {
+ let zsh = r#"#compdef podcast
+#autoload
+
+# Copyright (C) 2017:
+# Nathan Jaremko <njaremko@gmail.com>
+# All Rights Reserved.
+# This file is licensed under the GPLv2+. Please see COPYING for more information.
+
+_podcast() {
+ local ret=1
+ _arguments -C \
+ '1: :_podcast_cmds' \
+ && ret=0
+}
+
+_podcast_cmds () {
+ local subcommands;
+ subcommands=(
+ "download:Download episodes of podcast"
+ "help:Prints this message or the help of the given subcommand(s)"
+ "ls:List podcasts or episodes of a podcast"
+ "play:Play episodes of a podcast"
+ "refresh:Refreshes subscribed podcasts"
+ "rm:Unsubscribe from a podcast"
+ "completion:Shell Completions"
+ "search:Searches for podcasts"
+ "subscribe:Subscribe to a podcast RSS feed"
+ "update:check for updates"
+ )
+ _describe -t commands 'podcast' subcommands
+ _arguments : \
+ "--version[Output version information]" \
+ "--help[Output help message]"
+}
+
+_podcast"#;
+
+ //let bash = "";
+ //let sh = "";
+ match arg {
+ "zsh" => println!("{}", zsh),
+ //"bash" => println!("{}", bash),
+ //"sh" => println!("{}", sh),
+ _ => println!("Only options avaliable are: zsh"),
+ }
+}
diff --git a/src/main.rs b/src/main.rs
index 82961dc..fe5cabe 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -7,6 +7,7 @@ extern crate rss;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
+extern crate toml;
extern crate yaml_rust;
mod actions;
@@ -18,6 +19,8 @@ use clap::{App, Arg, SubCommand};
use structs::*;
use utils::*;
+const VERSION: &str = "0.4";
+
fn main() {
if let Err(err) = create_directories() {
eprintln!("{}", err);
@@ -32,7 +35,7 @@ fn main() {
};
let config = Config::new();
let matches = App::new("podcast")
- .version("0.4")
+ .version(VERSION)
.author("Nathan J. <njaremko@gmail.com>")
.about("A command line podcast manager")
.subcommand(
@@ -92,8 +95,20 @@ fn main() {
)
.subcommand(SubCommand::with_name("refresh").about("refresh subscribed podcasts"))
.subcommand(SubCommand::with_name("update").about("check for updates"))
- .subcommand(SubCommand::with_name("rm").about("delete podcast"))
- .subcommand(SubCommand::with_name("completions").about("install shell completions"))
+ .subcommand(
+ SubCommand::with_name("rm")
+ .about("unsubscribe from a podcast")
+ .arg(Arg::with_name("PODCAST").help("Podcast to delete").index(1)),
+ )
+ .subcommand(
+ SubCommand::with_name("completion")
+ .about("install shell completion")
+ .arg(
+ Arg::with_name("SHELL")
+ .help("Shell to print completion for")
+ .index(1),
+ ),
+ )
.get_matches();
match matches.subcommand_name() {
@@ -130,11 +145,23 @@ fn main() {
.unwrap(),
&config,
),
- Some("search") => (),
- Some("rm") => (),
- Some("completions") => (),
+ Some("search") => println!("This feature is coming soon..."),
+ Some("rm") => {
+ let rm_matches = matches.subcommand_matches("rm").unwrap();
+ match rm_matches.value_of("PODCAST") {
+ Some(regex) => remove_podcast(&mut state, regex),
+ None => println!(""),
+ }
+ }
+ Some("completion") => {
+ let matches = matches.subcommand_matches("completion").unwrap();
+ match matches.value_of("SHELL") {
+ Some(shell) => print_completion(shell),
+ None => print_completion(""),
+ }
+ }
Some("refresh") => update_rss(&mut state),
- Some("update") => check_for_update(&mut state),
+ Some("update") => check_for_update(VERSION),
_ => (),
}
if let Err(err) = state.save() {
diff --git a/src/structs.rs b/src/structs.rs
index b33da2e..885d6cd 100644
--- a/src/structs.rs
+++ b/src/structs.rs
@@ -5,7 +5,7 @@ use reqwest;
use rss::{self, Channel, Item};
use serde_json;
use std::collections::BTreeSet;
-use std::fs::{self, DirBuilder, File};
+use std::fs::{self, remove_dir_all, remove_file, DirBuilder, File};
use std::io::{self, BufReader, Read, Write};
use utils::*;
use yaml_rust::YamlLoader;
@@ -185,6 +185,20 @@ impl Podcast {
}
}
+ pub fn delete(title: &str) -> io::Result<()> {
+ let mut path = get_xml_dir();
+ let mut filename = String::from(title);
+ filename.push_str(".xml");
+ path.push(filename);
+
+ return remove_file(path);
+ }
+
+ pub fn delete_all() -> io::Result<()> {
+ let path = get_xml_dir();
+ return remove_dir_all(path);
+ }
+
pub fn episodes(&self) -> Vec<Episode> {
let mut result = Vec::new();