| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
 | #![recursion_limit = "1024"]
extern crate chrono;
extern crate clap;
#[macro_use]
extern crate error_chain;
#[macro_use]
extern crate lazy_static;
extern crate rayon;
extern crate regex;
extern crate reqwest;
extern crate rss;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
extern crate toml;
extern crate yaml_rust;
mod actions;
mod structs;
mod utils;
mod errors {
    // Create the Error, ErrorKind, ResultExt, and Result types
    error_chain!{}
}
use actions::*;
use errors::*;
use structs::*;
use utils::*;
use clap::{App, Arg, SubCommand};
const VERSION: &str = "0.5.8";
fn main() -> Result<()> {
    create_directories().chain_err(|| "unable to create directories")?;
    let mut state = State::new(VERSION).chain_err(|| "unable to load state")?;
    let config = Config::new()?;
    let matches = App::new("podcast")
        .version(VERSION)
        .author("Nathan J. <njaremko@gmail.com>")
        .about("A command line podcast manager")
        .subcommand(
            SubCommand::with_name("download")
                .about("download episodes of podcast")
                .arg(
                    Arg::with_name("PODCAST")
                        .help("Regex for subscribed podcast")
                        .required(true)
                        .index(1),
                )
                .arg(Arg::with_name("EPISODE").help("Episode index").index(2)),
        )
        .subcommand(
            SubCommand::with_name("ls")
                .about("list episodes of podcast")
                .arg(
                    Arg::with_name("PODCAST")
                        .help("Regex for subscribed podcast")
                        .index(1),
                ),
        )
        .subcommand(
            SubCommand::with_name("list")
                .about("list episodes of podcast")
                .arg(
                    Arg::with_name("PODCAST")
                        .help("Regex for subscribed podcast")
                        .index(1),
                ),
        )
        .subcommand(
            SubCommand::with_name("play")
                .about("play an episode")
                .arg(
                    Arg::with_name("PODCAST")
                        .help("Regex for subscribed podcast")
                        .required(true)
                        .index(1),
                )
                .arg(
                    Arg::with_name("EPISODE")
                        .help("Episode index")
                        .required(false)
                        .index(2),
                ),
        )
        .subcommand(
            SubCommand::with_name("search")
                .about("searches for podcasts")
                .arg(
                    Arg::with_name("debug")
                        .short("d")
                        .help("print debug information verbosely"),
                ),
        )
        .subcommand(
            SubCommand::with_name("subscribe")
                .about("subscribes to a podcast RSS feed")
                .arg(
                    Arg::with_name("URL")
                        .help("URL to RSS feed")
                        .required(true)
                        .index(1),
                )
                .arg(
                    Arg::with_name("download")
                        .short("d")
                        .long("download")
                        .help("auto download based on config"),
                ),
        )
        .subcommand(SubCommand::with_name("refresh").about("refresh subscribed podcasts"))
        .subcommand(SubCommand::with_name("update").about("check for updates"))
        .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() {
        Some("download") => {
            let download_matches = matches
                .subcommand_matches("download")
                .chain_err(|| "unable to find subcommand matches")?;
            let podcast = download_matches
                .value_of("PODCAST")
                .chain_err(|| "unable to find subcommand match")?;
            match download_matches.value_of("EPISODE") {
                Some(ep) => if String::from(ep).contains(|c| c == '-' || c == ',') {
                    download_range(&state, podcast, ep)?
                } else {
                    download_episode(&state, podcast, ep)?
                },
                None => download_all(&state, podcast)?,
            }
        }
        Some("ls") | Some("list") => {
            let list_matches = matches
                .subcommand_matches("ls")
                .or(matches.subcommand_matches("list"))
                .chain_err(|| "unable to find subcommand matches")?;
            match list_matches.value_of("PODCAST") {
                Some(regex) => list_episodes(regex)?,
                None => list_subscriptions(&state)?,
            }
        }
        Some("play") => {
            let play_matches = matches
                .subcommand_matches("play")
                .chain_err(|| "unable to find subcommand matches")?;
            let podcast = play_matches
                .value_of("PODCAST")
                .chain_err(|| "unable to find subcommand match")?;
            match play_matches.value_of("EPISODE") {
                Some(episode) => play_episode(&state, podcast, episode)?,
                None => play_latest(&state, podcast)?,
            }
        }
        Some("subscribe") => {
            let subscribe_matches = matches
                .subcommand_matches("subscribe")
                .chain_err(|| "unable to find subcommand matches")?;
            let url = subscribe_matches
                .value_of("URL")
                .chain_err(|| "unable to find subcommand match")?;
            state.subscribe(url).chain_err(|| "unable to subscribe")?;
            if subscribe_matches.occurrences_of("download") > 0 {
                download_rss(&config, url)?;
            } else {
                subscribe_rss(url)?;
            }
        }
        Some("search") => println!("This feature is coming soon..."),
        Some("rm") => {
            let rm_matches = matches
                .subcommand_matches("rm")
                .chain_err(|| "unable to find subcommand matches")?;
            let regex = rm_matches.value_of("PODCAST").chain_err(|| "")?;
            remove_podcast(&mut state, regex)?
        }
        Some("completion") => {
            let matches = matches
                .subcommand_matches("completion")
                .chain_err(|| "unable to find subcommand matches")?;
            match matches.value_of("SHELL") {
                Some(shell) => print_completion(shell),
                None => print_completion(""),
            }
        }
        Some("refresh") => update_rss(&mut state),
        Some("update") => check_for_update(VERSION)?,
        _ => (),
    }
    state.save().chain_err(|| "unable to save state")
}
 |