[#4] Added BasicDate type conversions

master
Jef Roosens 2021-04-04 11:39:40 +02:00
parent c89841ad38
commit 01276004e1
Signed by: Jef Roosens
GPG Key ID: B580B976584B5F30
1 changed files with 49 additions and 2 deletions

View File

@ -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<BasicDate> 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<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
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<Vec<PickupTime>, Box<dyn Error>> {
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<HashMap<String, String>> = response.json()?;
let mut output: Vec<PickupTime> = Vec::new();
for map in data.iter() {
if let Some(value) = map.get("value") {}
}
Ok()
}