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
use chrono::{Datelike, Utc};
use reqwest::header::USER_AGENT;
use serde::{Serialize, Deserialize};

#[derive(Debug)]
pub struct FeedOpts {
    pub base: Option<String>,
    pub endpoint: Option<String>,
    pub page: Option<usize>,
    pub search: Option<String>,
}

impl Feed {
    pub fn a(&self) -> String {
        self.a.clone()
    }
    pub fn t(&self) -> String {
        self.t.clone()
    }
}

#[derive(Serialize, Deserialize)]
pub struct Feed {
    t: String,
    a: String
}

pub struct ScheduleOpts {
    pub url: Option<String>,
    pub repo: Option<String>,
    pub branch: Option<String>,
    pub year: Option<i32>,
    pub quarter: Option<usize>
}


pub fn get_feed(opts: FeedOpts) -> Vec<Feed> {

    let base = if opts.base.is_none() {
        "http://eu.ohys.net/t".to_string()
    } else { opts.base.unwrap().clone() };

    let endpoint = if opts.endpoint.is_none() {
        "/json.php".to_string()
    } else { opts.endpoint.unwrap() };

    let search = if opts.search.is_none() {
        "".to_string()
    } else { format!("&q={}", opts.search.unwrap()) };

    let page = if opts.page.is_none() {
        1
    } else { opts.page.unwrap() };

    let dir = "disk".to_string();

    let url = format!("{}{}?dir={}&p={}{}", base, endpoint, dir, page, search);

    let client = reqwest::blocking::Client::new();
    let res = client.get(url)
        .header(USER_AGENT, format!("AkiaCode/ohys-api v{} (https://github.com/AkiaCode)", get_version()))
        .send().unwrap().text().unwrap();

    let text = res.trim_matches(|x|  x == '\u{feff}');
    let json: Vec<Feed> = serde_json::from_str(&text).unwrap();

    let mut cute: Vec<Feed> = Vec::new();

    for feed in json {
        cute.push(Feed {
            t: feed.t,
            a: format!("{}/{}", base, feed.a)
        })
    }

    return cute;
}

pub fn get_schedule(opts: ScheduleOpts) -> String {

    let url = if opts.url.is_none() {
        "https://raw.githubusercontent.com/".to_string()
    } else { opts.url.unwrap() };

    let repo = if opts.repo.is_none() {
        "ohyongslck/annie".to_string()
    } else { opts.repo.unwrap() };

    let branch = if opts.branch.is_none() {
        "master".to_string()
    } else { opts.branch.unwrap() };

    let year = if opts.year.is_none() {
        Utc::today().year()
    } else { opts.year.unwrap() };

    let quarter = if opts.quarter.is_none() {
        1
    } else { opts.quarter.unwrap() };

    let url = format!("{}{}/{}/{}@{}", url, repo, branch, year, quarter);

    let client = reqwest::blocking::Client::new();
    let res = client.get(url)
        .header(USER_AGENT, format!("AkiaCode/ohys-api v{} (https://github.com/AkiaCode)", get_version()))
        .send().unwrap().text().unwrap();

    return res;
}

fn get_version() -> String {
    let file = std::fs::read_to_string("./Cargo.toml").unwrap();
    let version: toml::Value = toml::from_str(&file).unwrap();
    return version["package"]["version"].to_string()
}

mod tests {
    use crate::{FeedOpts, get_feed};

    #[test]
    fn get_feed_test() {
        let opts = FeedOpts { base: None, endpoint: None, page: None, search: None  };
        let feed = get_feed(opts);
        assert!(!feed.is_empty());
    }
}