aboutsummaryrefslogtreecommitdiffstats
path: root/src/dsb/mod.rs
blob: 846b01738a1b46a78ee4ba8ea805ac7b2ce0def5 (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 crate::types::*;
use crate::Error;

use html5ever::parse_document;
//use html5ever::rcdom::RcDom;
use markup5ever_rcdom::RcDom;
use html5ever::tendril::TendrilSink;

mod html;

#[cfg(feature = "scraper")]
use hyper::Client;

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct DSB {
    pub school: String,
    pub year: String,
    pub valid_from: chrono::NaiveDate,
    pub updated_at: chrono::NaiveDateTime,
    pub date: chrono::NaiveDate,
    pub week: Week,
    pub free_lessons: Option<String>,
    pub missing_teachers: Vec<Teacher>,
    pub blocked_rooms: Vec<Room>,
    pub affected_classes: Vec<Class>,
    pub entries: Vec<DsbEntry>,
}

impl DSB {
    /// parse dsb content
    pub fn parse(html: &str) -> Result<Vec<DSB>, Error> {
        let html = html.replace("&nbsp;", " "); // FIXME: ???
        let dom = parse_document(RcDom::default(), Default::default())
            .from_utf8()
            .read_from(&mut html.as_bytes())?;


        html::parse(&dom.document)
    }
}

/// Default DSB url
#[cfg(feature = "scraper")]
pub const DSB_BASE_URL: &'static str = "https://dsbmobile.de/";

/// Config struct for dsb
#[cfg(feature = "scraper")]
pub struct DsbConfig<'a> {
    pub user_id: &'a str,
    pub password: &'a str,
    pub cookie: &'a str,

    pub base_url: &'static str,

    // TODO: "scraper" time behind a feature
    #[cfg(feature = "hyper")] //TODO
    pub hyper: &'a Client,
}

#[cfg(feature = "scraper")]
impl<'a> DsbConfig<'a> {
    pub fn new(user_id: &'a str, password: &'a str, cookie: &'a str) -> Self {
        Self {
            user_id,
            password,
            cookie,

            base_url: DSB_BASE_URL,
        }
    }
}

#[cfg(test)]
mod test {
    use super::DSB;
    #[test]
    fn read_dsb() {
        let html = std::fs::read_to_string("./src/dsb/mod.rs").unwrap();
        let dsb = DSB::parse(&html).unwrap();

        assert_eq!(dsb.len(), 3);

        println!("{:?}", dsb);
    }
}