Beginning ivago json calendar

This commit is contained in:
Jef Roosens 2021-03-13 21:58:58 +01:00
parent ad5bbc81be
commit 1641277bce
Signed by: Jef Roosens
GPG key ID: 955C0660072F691F
4 changed files with 253 additions and 14 deletions

View file

@ -1,6 +1,6 @@
use reqwest::blocking as reqwest;
use std::collections::HashMap;
use std::convert::TryFrom;
use std::convert::{TryFrom, From};
use rocket::http::Status;
use std::error::Error;
@ -13,6 +13,13 @@ const BASE_URL: &str = "https://www.ivago.be/nl/particulier/afval/ophaling";
const SEARCH_URL: &str ="https://www.ivago.be/nl/particulier/autocomplete/garbage/streets";
impl From<Street> for String {
fn from(street: Street) -> String {
format!("{} ({})", street.name, street.city)
}
}
/// Searches the Ivago API for streets in the given city
///
/// # Arguments
@ -32,7 +39,7 @@ pub fn search_streets(street_name: &String) -> Result<Vec<Street>, Box<dyn Error
// We iterate over every item and extract the needed data
for map in data.iter() {
if let Some(value) = map.get("value") {
match Street::try_from(value) {
match Street::try_from(*value) {
Ok(street) => output.push(street),
Err(_) => continue,
}
@ -49,6 +56,34 @@ pub fn search_streets(street_name: &String) -> Result<Vec<Street>, Box<dyn Error
///
/// * `street` - name of the street
/// * `city` - city the street is in
pub fn get_pickup_times(street: Street) -> Vec<PickupTime> {
Vec::new()
pub fn get_pickup_times(street: Street, number: u32) -> Result<Vec<PickupTime>, Box<dyn Error>> {
// The client needs to store cookies for the requests to work
let client = reqwest::Client::builder().cookie_store(true).build()?;
// Create post data
let form = [
("garbage_type", ""),
("ivago_street", String::from(street).as_str()),
("number", format!("{}", number).as_str()),
("form_id", "garbage_address_form"),
];
// This request just serves to populate the cookies
client.post(BASE_URL)
.form(&form)
.send()?;
let params = [
("_format", "json"),
("type", ""),
]
r2 = s.get("https://www.ivago.be/nl/particulier/garbage/pick-up/pickups?",
params={
"_format": "json",
"type": "",
"start": "1622332800",
"end": "163861328100"
}
}

View file

@ -4,8 +4,8 @@ use serde::ser::{Serialize, Serializer, SerializeStruct};
/// Represents a street
pub struct Street {
name: String,
city: String,
pub name: String,
pub city: String,
}
impl Serialize for Street {
@ -20,10 +20,10 @@ impl Serialize for Street {
}
}
impl TryFrom<&String> for Street {
impl TryFrom<String> for Street {
type Error = ();
fn try_from(value: &String) -> Result<Self, Self::Error> {
fn try_from(value: String) -> Result<Self, Self::Error> {
if let Some(index) = value.find('(') {
Ok(Street {
name: (value[0 .. index - 1].trim()).to_string(),