diff --git a/src/ivago/controller/pickup_times.rs b/src/ivago/controller/pickup_times.rs index e4ce656..174e538 100644 --- a/src/ivago/controller/pickup_times.rs +++ b/src/ivago/controller/pickup_times.rs @@ -1,11 +1,17 @@ use super::search::Street; +use chrono::{FixedOffset, TimeZone}; use regex::Regex; +use reqwest::blocking as reqwest; use rocket::http::RawStr; use rocket::request::FromFormValue; -use serde::ser::{Serialize, SerializeStruct, Serializer}; +use serde::ser::{SerializeStruct, Serializer}; +use serde::{Deserialize, Serialize}; +use std::collections::HashMap; +use std::convert::From; use std::error::Error; const BASE_URL: &str = "https://www.ivago.be/nl/particulier/afval/ophaling"; +const CAL_URL: &str = "https://www.ivago.be/nl/particulier/garbage/pick-up/pickups"; /// Represents a very simple Timezoneless date. Considering the timezone will /// always be CEST (aka Belgium's timezone), this is good enough. @@ -49,6 +55,16 @@ impl Serialize for BasicDate { } } +impl From for i64 { + fn from(date: BasicDate) -> i64 { + // Timezone of Brussels is UTC + 2 hours in the western hemisphere + FixedOffset::west(7_200) + .ymd(date.year as i32, date.month as u32, date.day as u32) + .and_hms(0, 0, 0) + .timestamp() + } +} + impl Serialize for PickupTime { fn serialize(&self, serializer: S) -> Result where @@ -66,6 +82,7 @@ impl Serialize for PickupTime { /// Represents a pickup time instance. All fields are a direct map of the /// original API +#[derive(Deserialize)] pub struct PickupTime { date: BasicDate, label: String, @@ -79,5 +96,35 @@ pub fn get_pickup_times( start_date: BasicDate, end_date: BasicDate, ) -> Result, Box> { - Ok(Vec::new()) + let client = reqwest::Client::builder().cookie_store(true).build()?; + + // This populates the cookies with the necessary values + client + .post(BASE_URL) + .form(&[ + ("garbage_type", ""), + ("ivago_street", &String::from(street)), + ("number", &number.to_string()), + ("form_id", "garbage_address_form"), + ]) + .send()?; + + let response = client + .get(CAL_URL) + .query(&[ + ("_format", "json"), + ("type", ""), + ("start", &i64::from(start_date).to_string()), + ("end", &i64::from(end_date).to_string()), + ]) + .send()?; + let data: Vec> = response.json()?; + + let mut output: Vec = Vec::new(); + + for map in data.iter() { + if let Some(value) = map.get("value") {} + } + + Ok() }