From 1641277bce1b8463076070bc61a881c1b861f6ae Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Sat, 13 Mar 2021 21:58:58 +0100 Subject: [PATCH] Beginning ivago json calendar --- Cargo.lock | 214 +++++++++++++++++++++++++++++++- Cargo.toml | 2 +- src/ivago/controller/mod.rs | 43 ++++++- src/ivago/controller/structs.rs | 8 +- 4 files changed, 253 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e4c597b..85dbcb8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -73,6 +73,12 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" +[[package]] +name = "base-x" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4521f3e3d031370679b3b140beb36dfe4801b09ac77e30c61941f97df3ef28b" + [[package]] name = "base64" version = "0.9.3" @@ -155,6 +161,12 @@ dependencies = [ "generic-array", ] +[[package]] +name = "const_fn" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28b9d6de7f49e22cf97ad17fc4036ece69300032f45f78f30b4a4482cdc3f4a6" + [[package]] name = "cookie" version = "0.11.4" @@ -168,7 +180,34 @@ dependencies = [ "percent-encoding 2.1.0", "rand", "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]] @@ -259,6 +298,12 @@ dependencies = [ "generic-array", ] +[[package]] +name = "discard" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "212d0f5754cb6769937f4501cc0e67f4f4483c8d2c3e1e922ee9edbe4ab4c7c0" + [[package]] name = "encoding_rs" version = "0.8.28" @@ -530,7 +575,7 @@ dependencies = [ "log 0.3.9", "mime 0.2.6", "num_cpus", - "time", + "time 0.1.43", "traitobject", "typeable", "unicase", @@ -995,6 +1040,12 @@ version = "0.2.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" +[[package]] +name = "proc-macro-hack" +version = "0.5.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5" + [[package]] name = "proc-macro2" version = "0.4.30" @@ -1013,6 +1064,16 @@ dependencies = [ "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]] name = "quote" version = "0.6.13" @@ -1097,6 +1158,8 @@ checksum = "bf12057f289428dbf5c591c74bf10392e4a8003f993405a902f20117019022d4" dependencies = [ "base64 0.13.0", "bytes", + "cookie 0.14.4", + "cookie_store", "encoding_rs", "futures-core", "futures-util", @@ -1115,6 +1178,7 @@ dependencies = [ "serde", "serde_json", "serde_urlencoded", + "time 0.2.25", "tokio", "tokio-native-tls", "url 2.2.1", @@ -1139,7 +1203,7 @@ dependencies = [ "rocket_codegen", "rocket_http", "state", - "time", + "time 0.1.43", "toml", "version_check 0.9.2", "yansi", @@ -1179,14 +1243,14 @@ version = "0.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ce364100ed7a1bf39257b69ebd014c1d5b4979b0d365d8c9ab0aa9c79645493d" dependencies = [ - "cookie", + "cookie 0.11.4", "hyper 0.10.16", "indexmap", "pear", "percent-encoding 1.0.1", "smallvec", "state", - "time", + "time 0.1.43", "unicode-xid 0.1.0", ] @@ -1200,6 +1264,15 @@ dependencies = [ "serde", ] +[[package]] +name = "rustc_version" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" +dependencies = [ + "semver", +] + [[package]] name = "ryu" version = "1.0.5" @@ -1254,11 +1327,40 @@ dependencies = [ "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]] name = "serde" version = "1.0.124" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "serde_json" @@ -1283,6 +1385,12 @@ dependencies = [ "serde", ] +[[package]] +name = "sha1" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2579985fda508104f7587689507983eadd6a6e84dd35d6d115361f530916fa0d" + [[package]] name = "sha2" version = "0.9.3" @@ -1319,12 +1427,70 @@ dependencies = [ "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]] name = "state" version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "subtle" version = "2.4.0" @@ -1377,6 +1543,44 @@ dependencies = [ "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]] name = "tinyvec" version = "1.1.1" diff --git a/Cargo.toml b/Cargo.toml index a9c8c70..c994ff9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ serde = "1.0.124" [dependencies.reqwest] version = "0.11.2" default-features = true -features = ["blocking", "json"] +features = ["blocking", "json", "cookies"] [dependencies.rocket_contrib] version = "0.4.7" diff --git a/src/ivago/controller/mod.rs b/src/ivago/controller/mod.rs index b7242ca..382d3b3 100644 --- a/src/ivago/controller/mod.rs +++ b/src/ivago/controller/mod.rs @@ -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 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, Box output.push(street), Err(_) => continue, } @@ -49,6 +56,34 @@ pub fn search_streets(street_name: &String) -> Result, Box Vec { - Vec::new() +pub fn get_pickup_times(street: Street, number: u32) -> Result, Box> { + // 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" + } } diff --git a/src/ivago/controller/structs.rs b/src/ivago/controller/structs.rs index 19a18f3..a5931d2 100644 --- a/src/ivago/controller/structs.rs +++ b/src/ivago/controller/structs.rs @@ -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 for Street { type Error = (); - fn try_from(value: &String) -> Result { + fn try_from(value: String) -> Result { if let Some(index) = value.find('(') { Ok(Street { name: (value[0 .. index - 1].trim()).to_string(),