Beginning ivago json calendar

master
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

214
Cargo.lock generated
View File

@ -73,6 +73,12 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
[[package]]
name = "base-x"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4521f3e3d031370679b3b140beb36dfe4801b09ac77e30c61941f97df3ef28b"
[[package]] [[package]]
name = "base64" name = "base64"
version = "0.9.3" version = "0.9.3"
@ -155,6 +161,12 @@ dependencies = [
"generic-array", "generic-array",
] ]
[[package]]
name = "const_fn"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28b9d6de7f49e22cf97ad17fc4036ece69300032f45f78f30b4a4482cdc3f4a6"
[[package]] [[package]]
name = "cookie" name = "cookie"
version = "0.11.4" version = "0.11.4"
@ -168,7 +180,34 @@ dependencies = [
"percent-encoding 2.1.0", "percent-encoding 2.1.0",
"rand", "rand",
"sha2", "sha2",
"time", "time 0.1.43",
]
[[package]]
name = "cookie"
version = "0.14.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "03a5d7b21829bc7b4bf4754a978a241ae54ea55a40f92bb20216e54096f4b951"
dependencies = [
"percent-encoding 2.1.0",
"time 0.2.25",
"version_check 0.9.2",
]
[[package]]
name = "cookie_store"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3818dfca4b0cb5211a659bbcbb94225b7127407b2b135e650d717bfb78ab10d3"
dependencies = [
"cookie 0.14.4",
"idna 0.2.2",
"log 0.4.14",
"publicsuffix",
"serde",
"serde_json",
"time 0.2.25",
"url 2.2.1",
] ]
[[package]] [[package]]
@ -259,6 +298,12 @@ dependencies = [
"generic-array", "generic-array",
] ]
[[package]]
name = "discard"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "212d0f5754cb6769937f4501cc0e67f4f4483c8d2c3e1e922ee9edbe4ab4c7c0"
[[package]] [[package]]
name = "encoding_rs" name = "encoding_rs"
version = "0.8.28" version = "0.8.28"
@ -530,7 +575,7 @@ dependencies = [
"log 0.3.9", "log 0.3.9",
"mime 0.2.6", "mime 0.2.6",
"num_cpus", "num_cpus",
"time", "time 0.1.43",
"traitobject", "traitobject",
"typeable", "typeable",
"unicase", "unicase",
@ -995,6 +1040,12 @@ version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857"
[[package]]
name = "proc-macro-hack"
version = "0.5.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5"
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "0.4.30" version = "0.4.30"
@ -1013,6 +1064,16 @@ dependencies = [
"unicode-xid 0.2.1", "unicode-xid 0.2.1",
] ]
[[package]]
name = "publicsuffix"
version = "1.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95b4ce31ff0a27d93c8de1849cf58162283752f065a90d508f1105fa6c9a213f"
dependencies = [
"idna 0.2.2",
"url 2.2.1",
]
[[package]] [[package]]
name = "quote" name = "quote"
version = "0.6.13" version = "0.6.13"
@ -1097,6 +1158,8 @@ checksum = "bf12057f289428dbf5c591c74bf10392e4a8003f993405a902f20117019022d4"
dependencies = [ dependencies = [
"base64 0.13.0", "base64 0.13.0",
"bytes", "bytes",
"cookie 0.14.4",
"cookie_store",
"encoding_rs", "encoding_rs",
"futures-core", "futures-core",
"futures-util", "futures-util",
@ -1115,6 +1178,7 @@ dependencies = [
"serde", "serde",
"serde_json", "serde_json",
"serde_urlencoded", "serde_urlencoded",
"time 0.2.25",
"tokio", "tokio",
"tokio-native-tls", "tokio-native-tls",
"url 2.2.1", "url 2.2.1",
@ -1139,7 +1203,7 @@ dependencies = [
"rocket_codegen", "rocket_codegen",
"rocket_http", "rocket_http",
"state", "state",
"time", "time 0.1.43",
"toml", "toml",
"version_check 0.9.2", "version_check 0.9.2",
"yansi", "yansi",
@ -1179,14 +1243,14 @@ version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce364100ed7a1bf39257b69ebd014c1d5b4979b0d365d8c9ab0aa9c79645493d" checksum = "ce364100ed7a1bf39257b69ebd014c1d5b4979b0d365d8c9ab0aa9c79645493d"
dependencies = [ dependencies = [
"cookie", "cookie 0.11.4",
"hyper 0.10.16", "hyper 0.10.16",
"indexmap", "indexmap",
"pear", "pear",
"percent-encoding 1.0.1", "percent-encoding 1.0.1",
"smallvec", "smallvec",
"state", "state",
"time", "time 0.1.43",
"unicode-xid 0.1.0", "unicode-xid 0.1.0",
] ]
@ -1200,6 +1264,15 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "rustc_version"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a"
dependencies = [
"semver",
]
[[package]] [[package]]
name = "ryu" name = "ryu"
version = "1.0.5" version = "1.0.5"
@ -1254,11 +1327,40 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "semver"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403"
dependencies = [
"semver-parser",
]
[[package]]
name = "semver-parser"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.124" version = "1.0.124"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd761ff957cb2a45fbb9ab3da6512de9de55872866160b23c25f1a841e99d29f" checksum = "bd761ff957cb2a45fbb9ab3da6512de9de55872866160b23c25f1a841e99d29f"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.124"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1800f7693e94e186f5e25a28291ae1570da908aff7d97a095dec1e56ff99069b"
dependencies = [
"proc-macro2 1.0.24",
"quote 1.0.9",
"syn 1.0.63",
]
[[package]] [[package]]
name = "serde_json" name = "serde_json"
@ -1283,6 +1385,12 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "sha1"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2579985fda508104f7587689507983eadd6a6e84dd35d6d115361f530916fa0d"
[[package]] [[package]]
name = "sha2" name = "sha2"
version = "0.9.3" version = "0.9.3"
@ -1319,12 +1427,70 @@ dependencies = [
"winapi 0.3.9", "winapi 0.3.9",
] ]
[[package]]
name = "standback"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2beb4d1860a61f571530b3f855a1b538d0200f7871c63331ecd6f17b1f014f8"
dependencies = [
"version_check 0.9.2",
]
[[package]] [[package]]
name = "state" name = "state"
version = "0.4.2" version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3015a7d0a5fd5105c91c3710d42f9ccf0abfb287d62206484dcc67f9569a6483" checksum = "3015a7d0a5fd5105c91c3710d42f9ccf0abfb287d62206484dcc67f9569a6483"
[[package]]
name = "stdweb"
version = "0.4.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d022496b16281348b52d0e30ae99e01a73d737b2f45d38fed4edf79f9325a1d5"
dependencies = [
"discard",
"rustc_version",
"stdweb-derive",
"stdweb-internal-macros",
"stdweb-internal-runtime",
"wasm-bindgen",
]
[[package]]
name = "stdweb-derive"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c87a60a40fccc84bef0652345bbbbbe20a605bf5d0ce81719fc476f5c03b50ef"
dependencies = [
"proc-macro2 1.0.24",
"quote 1.0.9",
"serde",
"serde_derive",
"syn 1.0.63",
]
[[package]]
name = "stdweb-internal-macros"
version = "0.2.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "58fa5ff6ad0d98d1ffa8cb115892b6e69d67799f6763e162a1c9db421dc22e11"
dependencies = [
"base-x",
"proc-macro2 1.0.24",
"quote 1.0.9",
"serde",
"serde_derive",
"serde_json",
"sha1",
"syn 1.0.63",
]
[[package]]
name = "stdweb-internal-runtime"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "213701ba3370744dcd1a12960caa4843b3d68b4d1c0a5d575e0d65b2ee9d16c0"
[[package]] [[package]]
name = "subtle" name = "subtle"
version = "2.4.0" version = "2.4.0"
@ -1377,6 +1543,44 @@ dependencies = [
"winapi 0.3.9", "winapi 0.3.9",
] ]
[[package]]
name = "time"
version = "0.2.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1195b046942c221454c2539395f85413b33383a067449d78aab2b7b052a142f7"
dependencies = [
"const_fn",
"libc",
"standback",
"stdweb",
"time-macros",
"version_check 0.9.2",
"winapi 0.3.9",
]
[[package]]
name = "time-macros"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "957e9c6e26f12cb6d0dd7fc776bb67a706312e7299aed74c8dd5b17ebb27e2f1"
dependencies = [
"proc-macro-hack",
"time-macros-impl",
]
[[package]]
name = "time-macros-impl"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5c3be1edfad6027c69f5491cf4cb310d1a71ecd6af742788c6ff8bced86b8fa"
dependencies = [
"proc-macro-hack",
"proc-macro2 1.0.24",
"quote 1.0.9",
"standback",
"syn 1.0.63",
]
[[package]] [[package]]
name = "tinyvec" name = "tinyvec"
version = "1.1.1" version = "1.1.1"

View File

@ -13,7 +13,7 @@ serde = "1.0.124"
[dependencies.reqwest] [dependencies.reqwest]
version = "0.11.2" version = "0.11.2"
default-features = true default-features = true
features = ["blocking", "json"] features = ["blocking", "json", "cookies"]
[dependencies.rocket_contrib] [dependencies.rocket_contrib]
version = "0.4.7" version = "0.4.7"

View File

@ -1,6 +1,6 @@
use reqwest::blocking as reqwest; use reqwest::blocking as reqwest;
use std::collections::HashMap; use std::collections::HashMap;
use std::convert::TryFrom; use std::convert::{TryFrom, From};
use rocket::http::Status; use rocket::http::Status;
use std::error::Error; 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"; 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 /// Searches the Ivago API for streets in the given city
/// ///
/// # Arguments /// # 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 // We iterate over every item and extract the needed data
for map in data.iter() { for map in data.iter() {
if let Some(value) = map.get("value") { if let Some(value) = map.get("value") {
match Street::try_from(value) { match Street::try_from(*value) {
Ok(street) => output.push(street), Ok(street) => output.push(street),
Err(_) => continue, Err(_) => continue,
} }
@ -49,6 +56,34 @@ pub fn search_streets(street_name: &String) -> Result<Vec<Street>, Box<dyn Error
/// ///
/// * `street` - name of the street /// * `street` - name of the street
/// * `city` - city the street is in /// * `city` - city the street is in
pub fn get_pickup_times(street: Street) -> Vec<PickupTime> { pub fn get_pickup_times(street: Street, number: u32) -> Result<Vec<PickupTime>, Box<dyn Error>> {
Vec::new() // 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 /// Represents a street
pub struct Street { pub struct Street {
name: String, pub name: String,
city: String, pub city: String,
} }
impl Serialize for Street { impl Serialize for Street {
@ -20,10 +20,10 @@ impl Serialize for Street {
} }
} }
impl TryFrom<&String> for Street { impl TryFrom<String> for Street {
type Error = (); 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('(') { if let Some(index) = value.find('(') {
Ok(Street { Ok(Street {
name: (value[0 .. index - 1].trim()).to_string(), name: (value[0 .. index - 1].trim()).to_string(),