aboutsummaryrefslogtreecommitdiffstats
path: root/src/actions.rs
blob: 4a6c122e7a73dcd4329629af46dad3e6c651577e (plain)
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
use regex::Regex;
use std::process::Command;
use structs::*;
use utils::*;

pub fn list_episodes(state: State, search: &str) {
    let re = Regex::new(&search).unwrap();
    for podcast in state.subscriptions() {
        if re.is_match(&podcast.name) {
            println!("Episodes for {}:", &podcast.name);
            match Podcast::from_url(&podcast.url) {
                Ok(podcast) => {
                    let episodes = podcast.episodes();
                    for (index, episode) in episodes.iter().enumerate() {
                        println!("({}) {}", episodes.len() - index, episode.title().unwrap());
                    }
                }
                Err(err) => println!("{}", err),
            }

        }
    }
}

pub fn list_subscriptions(state: State) {
    for podcast in state.subscriptions() {
        println!("{}", podcast.name);
    }
}

pub fn download_episode(state: State, p_search: &str, e_search: &str) {
    let re_pod = Regex::new(&p_search).unwrap();
    let ep_num = e_search.parse::<usize>().unwrap();

    for subscription in state.subscriptions() {
        if re_pod.is_match(&subscription.name) {
            let podcast = Podcast::from_url(&subscription.url).unwrap();
            let episodes = podcast.episodes();
            match episodes[episodes.len() - ep_num].download(podcast.title()) {
                Err(err) => println!("{}", err),
                _ => (),
            }
        }
    }
}

pub fn download_all(state: State, p_search: &str) {
    let re_pod = Regex::new(&p_search).unwrap();

    for subscription in state.subscriptions() {
        if re_pod.is_match(&subscription.name) {
            let podcast = Podcast::from_url(&subscription.url).unwrap();
            podcast.download();
        }
    }
}

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();
            path.push(podcast.title());
            let episodes = podcast.episodes();
            let episode = episodes[episodes.len() - ep_num].clone();

            let mut filename = String::from(episode.title().unwrap());
            filename.push_str(episode.extension().unwrap());
            path.push(filename);
            match path.exists() {
                true => launch_mpv(path.to_str().unwrap()),
                false => launch_mpv(episode.url().unwrap()),
            }
        }
    }
}

fn launch_mpv(url: &str) {
    Command::new("mpv")
        .args(&["--audio-display=no", url])
        .status()
        .expect("failed to execute process");
}