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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
use super::actions::*;
use super::utils::*;
use crate::errors::*;
use std::collections::BTreeSet;
use std::fs::{self, DirBuilder, File};
use std::io::{self, BufReader, Read, Write};
use chrono::prelude::*;
use rayon::prelude::*;
use regex::Regex;
use reqwest;
use rss::{Channel, Item};
use serde_json;
use yaml_rust::YamlLoader;
#[cfg(target_os = "macos")]
static ESCAPE_REGEX: &str = r"/";
#[cfg(target_os = "linux")]
static ESCAPE_REGEX: &str = r"/";
#[cfg(target_os = "windows")]
static ESCAPE_REGEX: &str = r#"[\\/:*?"<>|]"#;
lazy_static! {
static ref FILENAME_ESCAPE: Regex = Regex::new(ESCAPE_REGEX).unwrap();
}
pub struct Config {
pub auto_download_limit: i64,
}
impl Config {
pub fn new() -> Result<Config> {
let mut path = get_podcast_dir()?;
let mut download_limit = 1;
path.push(".config");
if path.exists() {
let mut s = String::new();
File::open(&path)
.chain_err(|| UNABLE_TO_OPEN_FILE)?
.read_to_string(&mut s)
.chain_err(|| UNABLE_TO_READ_FILE_TO_STRING)?;
let config =
YamlLoader::load_from_str(&s).chain_err(|| "unable to load yaml from string")?;
if !config.is_empty() {
let doc = &config[0];
if let Some(val) = doc["auto_download_limit"].as_i64() {
download_limit = val;
}
}
} else {
let mut file = File::create(&path).chain_err(|| UNABLE_TO_CREATE_FILE)?;
file.write_all(b"auto_download_limit: 1")
.chain_err(|| UNABLE_TO_WRITE_FILE)?;
}
Ok(Config {
auto_download_limit: download_limit,
})
}
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Subscription {
pub title: String,
pub url: String,
pub num_episodes: usize,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct State {
pub version: String,
pub last_run_time: DateTime<Utc>,
pub subscriptions: Vec<Subscription>,
}
impl State {
pub fn new(version: &str) -> Result<State> {
let mut path = get_podcast_dir()?;
path.push(".subscriptions");
if path.exists() {
let mut s = String::new();
{
let mut file = File::open(&path).chain_err(|| UNABLE_TO_OPEN_FILE)?;
file.read_to_string(&mut s)
.chain_err(|| UNABLE_TO_READ_FILE_TO_STRING)?;
}
let mut state: State = match serde_json::from_str(&s) {
Ok(val) => val,
// This will happen if the struct has changed between versions
Err(_) => {
let v: serde_json::Value =
serde_json::from_str(&s).chain_err(|| "unable to read json from string")?;
State {
version: String::from(version),
last_run_time: Utc::now(),
subscriptions: match serde_json::from_value(v["subscriptions"].clone()) {
Ok(val) => val,
Err(_) => serde_json::from_value(v["subs"].clone())
.chain_err(|| "unable to parse value from json")?,
},
}
}
};
state.version = String::from(version);
// Check if a day has passed (86400 seconds) since last launch
if Utc::now()
.signed_duration_since(state.last_run_time)
.num_seconds()
> 86400
{
update_rss(&mut state);
check_for_update(&state.version)?;
}
state.last_run_time = Utc::now();
state.save()?;
Ok(state)
} else {
Ok(State {
version: String::from(version),
last_run_time: Utc::now(),
subscriptions: Vec::new(),
})
}
}
pub fn subscribe(&mut self, url: &str) -> Result<()> {
let mut set = BTreeSet::new();
for sub in self.subscriptions() {
set.insert(sub.title);
}
let podcast = Podcast::from(Channel::from_url(url).unwrap());
if !set.contains(podcast.title()) {
self.subscriptions.push(Subscription {
title: String::from(podcast.title()),
url: String::from(url),
num_episodes: podcast.episodes().len(),
});
}
self.save()
}
pub fn subscriptions(&self) -> Vec<Subscription> {
self.subscriptions.clone()
}
pub fn save(&self) -> Result<()> {
let mut path = get_podcast_dir()?;
path.push(".subscriptions.tmp");
let serialized = serde_json::to_string(self).chain_err(|| "unable to serialize state")?;
{
let mut file = File::create(&path).chain_err(|| UNABLE_TO_CREATE_FILE)?;
file.write_all(serialized.as_bytes())
.chain_err(|| UNABLE_TO_WRITE_FILE)?;
}
fs::rename(&path, get_sub_file()?).chain_err(|| "unable to rename file")?;
Ok(())
}
}
#[derive(Clone, Debug)]
pub struct Podcast(Channel);
#[derive(Clone, Debug)]
pub struct Episode(Item);
impl From<Channel> for Podcast {
fn from(channel: Channel) -> Podcast {
Podcast(channel)
}
}
impl From<Item> for Episode {
fn from(item: Item) -> Episode {
Episode(item)
}
}
impl Podcast {
pub fn title(&self) -> &str {
self.0.title()
}
#[allow(dead_code)]
pub fn url(&self) -> &str {
self.0.link()
}
#[allow(dead_code)]
pub fn from_url(url: &str) -> Result<Podcast> {
Ok(Podcast::from(
Channel::from_url(url).chain_err(|| UNABLE_TO_CREATE_CHANNEL_FROM_RESPONSE)?,
))
}
pub fn from_title(title: &str) -> Result<Podcast> {
let mut path = get_xml_dir()?;
let mut filename = String::from(title);
filename.push_str(".xml");
path.push(filename);
let file = File::open(&path).chain_err(|| UNABLE_TO_OPEN_FILE)?;
Ok(Podcast::from(
Channel::read_from(BufReader::new(file))
.chain_err(|| UNABLE_TO_CREATE_CHANNEL_FROM_FILE)?,
))
}
pub fn delete(title: &str) -> Result<()> {
let mut path = get_xml_dir()?;
let mut filename = String::from(title);
filename.push_str(".xml");
path.push(filename);
fs::remove_file(path).chain_err(|| UNABLE_TO_REMOVE_FILE)
}
pub fn delete_all() -> Result<()> {
let path = get_xml_dir()?;
fs::remove_dir_all(path).chain_err(|| UNABLE_TO_READ_DIRECTORY)
}
pub fn episodes(&self) -> Vec<Episode> {
let mut result = Vec::new();
for item in self.0.items().to_vec() {
result.push(Episode::from(item));
}
result
}
pub fn download(&self) -> Result<()> {
print!("You are about to download all episodes of {} (y/n): ", self.title());
io::stdout().flush().ok();
let mut input = String::new();
io::stdin()
.read_line(&mut input)
.chain_err(|| "unable to read stdin")?;
if input.to_lowercase().trim() != "y" {
return Ok(());
}
let mut path = get_podcast_dir()?;
path.push(self.title());
match already_downloaded(self.title()) {
Ok(downloaded) => {
self.episodes().par_iter().for_each(|i| {
if let Some(ep_title) = i.title() {
if !downloaded.contains(&ep_title) {
if let Err(err) = i.download(self.title()) {
eprintln!("{}", err);
}
}
}
});
}
Err(_) => {
self.episodes().par_iter().for_each(|i| {
if let Err(err) = i.download(self.title()) {
eprintln!("{}", err);
}
});
}
}
Ok(())
}
pub fn download_specific(&self, episode_numbers: &[usize]) -> Result<()> {
let mut path = get_podcast_dir()?;
path.push(self.title());
let downloaded = already_downloaded(self.title())?;
let episodes = self.episodes();
episode_numbers.par_iter().for_each(|ep_num| {
if let Some(ep_title) = episodes[episodes.len() - ep_num].title() {
if !downloaded.contains(&ep_title) {
if let Err(err) = episodes[episodes.len() - ep_num].download(self.title()) {
eprintln!("{}", err);
}
}
}
});
Ok(())
}
}
impl Episode {
pub fn title(&self) -> Option<String> {
Some(
FILENAME_ESCAPE
.replace_all(self.0.title()?, "_")
.to_string(),
)
}
pub fn url(&self) -> Option<&str> {
match self.0.enclosure() {
Some(val) => Some(val.url()),
None => None,
}
}
pub fn extension(&self) -> Option<&str> {
match self.0.enclosure()?.mime_type() {
"audio/mpeg" => Some(".mp3"),
"audio/mp4" => Some(".m4a"),
"audio/ogg" => Some(".ogg"),
_ => find_extension(self.url().unwrap()),
}
}
pub fn download(&self, podcast_name: &str) -> Result<()> {
let stdout = io::stdout();
let mut path = get_podcast_dir()?;
path.push(podcast_name);
DirBuilder::new()
.recursive(true)
.create(&path)
.chain_err(|| UNABLE_TO_CREATE_DIRECTORY)?;
if let Some(url) = self.url() {
if let Some(title) = self.title() {
let mut filename = title;
filename.push_str(
self.extension()
.chain_err(|| "unable to retrieve extension")?,
);
path.push(filename);
if !path.exists() {
{
let mut handle = stdout.lock();
writeln!(&mut handle, "Downloading: {}", path.to_str().unwrap()).ok();
}
let mut file = File::create(&path).chain_err(|| UNABLE_TO_CREATE_FILE)?;
let mut resp = reqwest::get(url).chain_err(|| UNABLE_TO_GET_HTTP_RESPONSE)?;
let mut content: Vec<u8> = Vec::new();
resp.read_to_end(&mut content)
.chain_err(|| UNABLE_TO_READ_RESPONSE_TO_END)?;
file.write_all(&content)
.chain_err(|| UNABLE_TO_WRITE_FILE)?;
} else {
let mut handle = stdout.lock();
writeln!(&mut handle, "File already exists: {}", path.to_str().chain_err(|| UNABLE_TO_CONVERT_TO_STR)?).ok();
}
}
}
Ok(())
}
}
|