aboutsummaryrefslogtreecommitdiffstats
path: root/src/search.rs
blob: f13d9d1fc4d331c98aeb0ebd98c8b2bf54e325d2 (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
use failure::Error;
use percent_encoding::{utf8_percent_encode, DEFAULT_ENCODE_SET};
use reqwest;
use serde_json;

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Response {
    error: Option<String>,
    found: Vec<String>,
}

impl Response {
    pub fn error(&self) -> &Option<String> {
        &self.error
    }

    pub fn found(&self) -> &[String] {
        &self.found
    }
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct RssResponse {
    error: Option<String>,
    url: Option<String>,
}

impl RssResponse {
    pub fn error(&self) -> &Option<String> {
        &self.error
    }

    pub fn url(&self) -> &Option<String> {
        &self.url
    }
}

const BASE: &str = "https://podcastapi.ca";

pub fn search_for_podcast(podcast: &str) -> Result<Response, Error> {
    let encoded = utf8_percent_encode(podcast, DEFAULT_ENCODE_SET).to_string();
    let url = BASE.to_string() + "/query/" + &encoded;
    let resp = reqwest::get(&url)?;
    let r: Response = serde_json::from_reader(resp)?;
    Ok(r)
}

pub fn search_for_episode(podcast: &str, ep: &str) -> Result<Response, Error> {
    let podcast_encoded = utf8_percent_encode(podcast, DEFAULT_ENCODE_SET).to_string();
    let ep_encoded = utf8_percent_encode(ep, DEFAULT_ENCODE_SET).to_string();
    let url = BASE.to_string() + "/query/" + &podcast_encoded + "/episode/" + &ep_encoded;
    let resp = reqwest::get(&url)?;
    let r: Response = serde_json::from_reader(resp)?;
    Ok(r)
}

pub fn retrieve_rss(podcast: &str) -> Result<RssResponse, Error> {
    let encoded = utf8_percent_encode(podcast, DEFAULT_ENCODE_SET).to_string();
    let url = BASE.to_string() + "/rss/" + &encoded;
    let resp = reqwest::get(&url)?;
    let r: RssResponse = serde_json::from_reader(resp)?;
    Ok(r)
}