Switched to logic-based file structure
parent
a12ce07136
commit
b42ea850cd
|
@ -1,89 +1,41 @@
|
||||||
use reqwest::blocking as reqwest;
|
mod search;
|
||||||
use std::collections::HashMap;
|
pub use search::{Street, search_streets};
|
||||||
use std::convert::{TryFrom, From};
|
|
||||||
use rocket::http::Status;
|
|
||||||
use std::error::Error;
|
|
||||||
|
|
||||||
pub mod structs;
|
|
||||||
use structs::{Street, Date, PickupTime};
|
|
||||||
|
|
||||||
|
|
||||||
/// The base URL where their API starts
|
///// Return the known pickup times for the given street and/or city
|
||||||
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";
|
///// # Arguments
|
||||||
|
/////
|
||||||
|
///// * `street` - name of the street
|
||||||
|
///// * `city` - city the street is in
|
||||||
|
//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"),
|
||||||
|
// ];
|
||||||
|
|
||||||
impl From<Street> for String {
|
// // This request just serves to populate the cookies
|
||||||
fn from(street: Street) -> String {
|
// client.post(BASE_URL)
|
||||||
format!("{} ({})", street.name, street.city)
|
// .form(&form)
|
||||||
}
|
// .send()?;
|
||||||
}
|
|
||||||
|
|
||||||
|
// let params = [
|
||||||
|
// ("_format", "json"),
|
||||||
|
// ("type", ""),
|
||||||
|
|
||||||
/// Searches the Ivago API for streets in the given city
|
// ]
|
||||||
///
|
|
||||||
/// # Arguments
|
|
||||||
///
|
|
||||||
/// * `street` - name of the street
|
|
||||||
/// * `city` - city the street is in
|
|
||||||
// TODO find out how to do this async
|
|
||||||
pub fn search_streets(street_name: &String) -> Result<Vec<Street>, Box<dyn Error>> {
|
|
||||||
let client = reqwest::Client::new();
|
|
||||||
let response = client.get(SEARCH_URL)
|
|
||||||
.query(&[("q", street_name)])
|
|
||||||
.send()?;
|
|
||||||
let data: Vec<HashMap<String, String>> = response.json()?;
|
|
||||||
|
|
||||||
let mut output: Vec<Street> = Vec::new();
|
//r2 = s.get("https://www.ivago.be/nl/particulier/garbage/pick-up/pickups?",
|
||||||
|
// params={
|
||||||
// We iterate over every item and extract the needed data
|
// "_format": "json",
|
||||||
for map in data.iter() {
|
// "type": "",
|
||||||
if let Some(value) = map.get("value") {
|
// "start": "1622332800",
|
||||||
match Street::try_from(*value) {
|
// "end": "163861328100"
|
||||||
Ok(street) => output.push(street),
|
// }
|
||||||
Err(_) => continue,
|
//}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(output)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Return the known pickup times for the given street and/or city
|
|
||||||
///
|
|
||||||
/// # Arguments
|
|
||||||
///
|
|
||||||
/// * `street` - name of the street
|
|
||||||
/// * `city` - city the street is in
|
|
||||||
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"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -0,0 +1,19 @@
|
||||||
|
const BASE_URL: &str = "https://www.ivago.be/nl/particulier/afval/ophaling";
|
||||||
|
|
||||||
|
|
||||||
|
/// Represents a timezoneless date
|
||||||
|
pub struct Date {
|
||||||
|
day: u8,
|
||||||
|
month: u8,
|
||||||
|
year: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Represents a pickup time instance. All fields are a direct map of the
|
||||||
|
/// original API
|
||||||
|
pub struct PickupTime {
|
||||||
|
date: Date,
|
||||||
|
label: String,
|
||||||
|
classes: Vec<String>,
|
||||||
|
url: String
|
||||||
|
}
|
|
@ -0,0 +1,83 @@
|
||||||
|
use reqwest::blocking as reqwest;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::convert::TryFrom;
|
||||||
|
use serde::ser::{Serialize, Serializer, SerializeStruct};
|
||||||
|
use std::error::Error;
|
||||||
|
|
||||||
|
|
||||||
|
/// Endpoint for the search feature
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl Serialize for Street {
|
||||||
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
S: Serializer,
|
||||||
|
{
|
||||||
|
let mut s = serializer.serialize_struct("Street", 2)?;
|
||||||
|
s.serialize_field("name", &self.name)?;
|
||||||
|
s.serialize_field("city", &self.city)?;
|
||||||
|
s.end()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl TryFrom<String> for Street {
|
||||||
|
type 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(),
|
||||||
|
city: (value[index + 1 .. value.len() - 1].trim()).to_string(),
|
||||||
|
})
|
||||||
|
|
||||||
|
}else {
|
||||||
|
Err(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Represents a street
|
||||||
|
pub struct Street {
|
||||||
|
pub name: String,
|
||||||
|
pub city: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Searches the Ivago API for streets in the given city
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * `street` - name of the street
|
||||||
|
/// * `city` - city the street is in
|
||||||
|
// TODO find out how to do this async
|
||||||
|
pub fn search_streets(street_name: &String) -> Result<Vec<Street>, Box<dyn Error>> {
|
||||||
|
let client = reqwest::Client::new();
|
||||||
|
let response = client.get(SEARCH_URL)
|
||||||
|
.query(&[("q", street_name)])
|
||||||
|
.send()?;
|
||||||
|
let data: Vec<HashMap<String, String>> = response.json()?;
|
||||||
|
|
||||||
|
let mut output: Vec<Street> = Vec::new();
|
||||||
|
|
||||||
|
// 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.clone()) {
|
||||||
|
Ok(street) => output.push(street),
|
||||||
|
Err(_) => continue,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(output)
|
||||||
|
}
|
|
@ -1,55 +0,0 @@
|
||||||
use std::convert::TryFrom;
|
|
||||||
use serde::ser::{Serialize, Serializer, SerializeStruct};
|
|
||||||
|
|
||||||
|
|
||||||
/// Represents a street
|
|
||||||
pub struct Street {
|
|
||||||
pub name: String,
|
|
||||||
pub city: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Serialize for Street {
|
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
||||||
where
|
|
||||||
S: Serializer,
|
|
||||||
{
|
|
||||||
let mut s = serializer.serialize_struct("Street", 2)?;
|
|
||||||
s.serialize_field("name", &self.name)?;
|
|
||||||
s.serialize_field("city", &self.city)?;
|
|
||||||
s.end()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TryFrom<String> for Street {
|
|
||||||
type 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(),
|
|
||||||
city: (value[index + 1 .. value.len() - 1].trim()).to_string(),
|
|
||||||
})
|
|
||||||
|
|
||||||
}else {
|
|
||||||
Err(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Represents a timezoneless date
|
|
||||||
pub struct Date {
|
|
||||||
day: u8,
|
|
||||||
month: u8,
|
|
||||||
year: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Represents a pickup time instance. All fields are a direct map of the
|
|
||||||
/// original API
|
|
||||||
pub struct PickupTime {
|
|
||||||
date: Date,
|
|
||||||
label: String,
|
|
||||||
classes: Vec<String>,
|
|
||||||
url: String
|
|
||||||
}
|
|
|
@ -1,14 +1,11 @@
|
||||||
#[cfg(test)] mod tests;
|
#[cfg(test)] mod tests;
|
||||||
|
mod controller;
|
||||||
|
|
||||||
use rocket_contrib::json::Json;
|
use rocket_contrib::json::Json;
|
||||||
|
|
||||||
mod controller;
|
|
||||||
use controller as ctrl;
|
|
||||||
use ctrl::structs::Street;
|
|
||||||
|
|
||||||
pub fn routes() -> Vec<rocket::Route> {
|
pub fn routes() -> Vec<rocket::Route> {
|
||||||
routes![
|
routes![
|
||||||
search_streets,
|
search_streets_json,
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,8 +13,8 @@ pub fn routes() -> Vec<rocket::Route> {
|
||||||
// TODO make this async
|
// TODO make this async
|
||||||
// TODO change this so it can return errors instead of empty json
|
// TODO change this so it can return errors instead of empty json
|
||||||
#[get("/search?<street>", format="json")]
|
#[get("/search?<street>", format="json")]
|
||||||
pub fn search_streets(street: String) -> Json<Vec<Street>> {
|
pub fn search_streets_json(street: String) -> Json<Vec<controller::Street>> {
|
||||||
match ctrl::search_streets(&street) {
|
match controller::search_streets(&street) {
|
||||||
Ok(streets) => Json(streets),
|
Ok(streets) => Json(streets),
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
println!("{:?}", err);
|
println!("{:?}", err);
|
||||||
|
|
Loading…
Reference in New Issue