fej/src/ivago/controller/pickup_times.rs

157 lines
4.6 KiB
Rust

use super::search::Street;
use crate::errors::FejError;
use chrono::{FixedOffset, TimeZone};
use regex::Regex;
use reqwest::blocking as reqwest;
use rocket::http::RawStr;
use rocket::request::FromFormValue;
use serde::ser::{SerializeStruct, Serializer};
use serde::Serialize;
use std::collections::HashMap;
use std::convert::{From, TryFrom};
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. I use this
/// instead of a NaiveDate to avoid E0117.
pub struct BasicDate {
year: u32,
month: u8,
day: u8,
}
/// This allows us to use BasicDate as a query parameter in our routes.
impl<'v> FromFormValue<'v> for BasicDate {
type Error = &'v RawStr;
fn from_form_value(form_value: &'v RawStr) -> Result<BasicDate, Self::Error> {
match BasicDate::try_from(form_value.as_str()) {
Err(_) => Err(form_value),
Ok(date) => Ok(date),
}
}
}
/// We need this when deserializing BasicDate.
impl ToString for BasicDate {
fn to_string(&self) -> String {
format!("{}-{}-{}", self.year, self.month, self.day)
}
}
/// This is used to serialize BasicDate.
impl TryFrom<&str> for BasicDate {
type Error = ();
fn try_from(s: &str) -> Result<BasicDate, Self::Error> {
let re = Regex::new(r"^(\d{4})-(\d{2})-(\d{2})$").unwrap();
match re.captures(s) {
None => Err(()),
Some(caps) => Ok(BasicDate {
year: caps.get(1).unwrap().as_str().parse().unwrap(),
month: caps.get(2).unwrap().as_str().parse().unwrap(),
day: caps.get(3).unwrap().as_str().parse().unwrap(),
}),
}
}
}
impl Serialize for BasicDate {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&self.to_string())
}
}
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)
// For some reason, I couldn't get .timestamp() to work on a Date
// without a time component, even though the docs seemed to
// indicate this was possible
.and_hms(0, 0, 0)
.timestamp()
}
}
impl Serialize for PickupTime {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut s = serializer.serialize_struct("PickupTime", 4)?;
s.serialize_field("date", &self.date)?;
s.serialize_field("label", &self.label)?;
s.serialize_field("classes", &self.classes)?;
s.serialize_field("url", &self.url)?;
s.end()
}
}
/// Represents a pickup time instance. All fields are a direct map of the
/// original API
pub struct PickupTime {
date: BasicDate,
label: String,
classes: Vec<String>,
url: String,
}
pub fn get_pickup_times(
street: Street,
number: u32,
start_date: BasicDate,
end_date: BasicDate,
) -> Result<Vec<PickupTime>, FejError> {
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() {
output.push(PickupTime {
// TODO should I check here if the parsing worked?
date: BasicDate::try_from(map.get("date").unwrap().as_str()).unwrap(),
label: map.get("label").unwrap().to_string(),
classes: map
.get("classes")
.unwrap()
.split_whitespace()
.map(|x| String::from(x))
.collect(),
url: map.get("url").unwrap().to_string(),
})
}
Ok(output)
}