From 0aa021259af2529d5d5dc53c0428d39854f77a9c Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Thu, 11 May 2023 14:36:07 +0200 Subject: [PATCH] feat: add site data api --- Cargo.lock | 200 ++++++++++++++++++++++++++++++++++- affluences-api/Cargo.toml | 2 + affluences-api/src/lib.rs | 13 ++- affluences-api/src/models.rs | 131 +++++++++++++++++++++++ bot/Cargo.toml | 1 + bot/src/main.rs | 11 +- 6 files changed, 353 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0cc1499..a517a24 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6,8 +6,19 @@ version = 3 name = "affluences-api" version = "0.1.0" dependencies = [ + "chrono", "reqwest", "serde", + "uuid", +] + +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", ] [[package]] @@ -33,6 +44,7 @@ name = "bot" version = "0.1.0" dependencies = [ "affluences-api", + "chrono", "tokio", ] @@ -60,6 +72,31 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "chrono" +version = "0.4.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e3c5919066adf22df73762e50cffcde3a758f2a848b113b586d1f86728b673b" +dependencies = [ + "iana-time-zone", + "js-sys", + "num-integer", + "num-traits", + "time", + "wasm-bindgen", + "winapi", +] + +[[package]] +name = "codespan-reporting" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3538270d33cc669650c4b093848450d380def10c331d38c768e34cac80576e6e" +dependencies = [ + "termcolor", + "unicode-width", +] + [[package]] name = "core-foundation" version = "0.9.3" @@ -76,6 +113,50 @@ version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa" +[[package]] +name = "cxx" +version = "1.0.94" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f61f1b6389c3fe1c316bf8a4dccc90a38208354b330925bce1f74a6c4756eb93" +dependencies = [ + "cc", + "cxxbridge-flags", + "cxxbridge-macro", + "link-cplusplus", +] + +[[package]] +name = "cxx-build" +version = "1.0.94" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12cee708e8962df2aeb38f594aae5d827c022b6460ac71a7a3e2c3c2aae5a07b" +dependencies = [ + "cc", + "codespan-reporting", + "once_cell", + "proc-macro2", + "quote", + "scratch", + "syn", +] + +[[package]] +name = "cxxbridge-flags" +version = "1.0.94" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7944172ae7e4068c533afbb984114a56c46e9ccddda550499caa222902c7f7bb" + +[[package]] +name = "cxxbridge-macro" +version = "1.0.94" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2345488264226bf682893e25de0769f3360aac9957980ec49361b083ddaa5bc5" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "encoding_rs" version = "0.8.32" @@ -295,6 +376,30 @@ dependencies = [ "tokio-native-tls", ] +[[package]] +name = "iana-time-zone" +version = "0.1.56" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0722cd7114b7de04316e7ea5456a0bbb20e4adb46fd27a3697adb812cff0f37c" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "windows", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0703ae284fc167426161c2e3f1da3ea71d94b21bedbcc9494e92b28e334e3dca" +dependencies = [ + "cxx", + "cxx-build", +] + [[package]] name = "idna" version = "0.3.0" @@ -368,6 +473,15 @@ version = "0.2.144" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b00cc1c228a6782d0f076e7b232802e0c5689d41bb5df366f2a6b6621cfdfe1" +[[package]] +name = "link-cplusplus" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ecd207c9c713c34f95a097a5b029ac2ce6010530c7b49d7fea24d977dede04f5" +dependencies = [ + "cc", +] + [[package]] name = "linux-raw-sys" version = "0.3.7" @@ -407,7 +521,7 @@ checksum = "5b9d9a46eff5b4ff64b45a9e316a6d1e0bc719ef429cbec4dc630684212bfdf9" dependencies = [ "libc", "log", - "wasi", + "wasi 0.11.0+wasi-snapshot-preview1", "windows-sys 0.45.0", ] @@ -429,6 +543,25 @@ dependencies = [ "tempfile", ] +[[package]] +name = "num-integer" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" +dependencies = [ + "autocfg", +] + [[package]] name = "num_cpus" version = "1.15.0" @@ -644,6 +777,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" +[[package]] +name = "scratch" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1792db035ce95be60c3f8853017b3999209281c24e2ba5bc8e59bf97a0c590c1" + [[package]] name = "security-framework" version = "2.8.2" @@ -768,6 +907,26 @@ dependencies = [ "windows-sys 0.45.0", ] +[[package]] +name = "termcolor" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be55cf8942feac5c765c2c993422806843c9a9a45d4d5c407ad6dd2ea95eb9b6" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "time" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b797afad3f312d1c66a56d11d0316f916356d11bd158fbc6ca6389ff6bf805a" +dependencies = [ + "libc", + "wasi 0.10.0+wasi-snapshot-preview1", + "winapi", +] + [[package]] name = "tinyvec" version = "1.6.0" @@ -890,6 +1049,12 @@ dependencies = [ "tinyvec", ] +[[package]] +name = "unicode-width" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b" + [[package]] name = "url" version = "2.3.1" @@ -901,6 +1066,15 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "uuid" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4dad5567ad0cf5b760e5665964bec1b47dfd077ba8a2544b513f3556d3d239a2" +dependencies = [ + "serde", +] + [[package]] name = "vcpkg" version = "0.2.15" @@ -917,6 +1091,12 @@ dependencies = [ "try-lock", ] +[[package]] +name = "wasi" +version = "0.10.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" @@ -1015,12 +1195,30 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e686886bc078bc1b0b600cac0147aadb815089b6e4da64016cbd754b6342700f" +dependencies = [ + "windows-targets 0.48.0", +] + [[package]] name = "windows-sys" version = "0.42.0" diff --git a/affluences-api/Cargo.toml b/affluences-api/Cargo.toml index 4aad6ac..0f8b8cd 100644 --- a/affluences-api/Cargo.toml +++ b/affluences-api/Cargo.toml @@ -6,5 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +chrono = "0.4.24" reqwest = { version = "0.11.17", features = ["json"] } serde = { version = "1.0.163", features = ["derive"] } +uuid = { version = "1.3.2", features = ["serde"] } diff --git a/affluences-api/src/lib.rs b/affluences-api/src/lib.rs index c3d6a06..ee97ab1 100644 --- a/affluences-api/src/lib.rs +++ b/affluences-api/src/lib.rs @@ -1,6 +1,7 @@ mod models; -use models::Resource; +use chrono::NaiveDate; +use models::{Resource, Data, SiteData}; const USER_AGENT: &str = "User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/112.0"; @@ -16,7 +17,13 @@ impl AffluencesClient { } } - pub async fn available(&mut self) -> reqwest::Result> { - self.client.get("https://reservation.affluences.com/api/resources/026b8caa-d310-464f-b714-4c21a2cf98ea/available?date=2023-05-11&type=1").send().await?.json::>().await + pub async fn available(&mut self, resource_uuid: &str, date: NaiveDate, resource_type: u32) -> reqwest::Result> { + let url = format!("https://reservation.affluences.com/api/resources/{}/available", resource_uuid); + self.client.get(url).query(&[("date", date.format("%Y-%m-%d").to_string()), ("type", resource_type.to_string())]).send().await?.json::>().await + } + + pub async fn site_data(&mut self, name: &str) -> reqwest::Result { + let url = format!("https://api.affluences.com/app/v3/sites/{}", name); + Ok(self.client.get(url).send().await?.json::>().await?.data) } } diff --git a/affluences-api/src/models.rs b/affluences-api/src/models.rs index c90b530..b66f73a 100644 --- a/affluences-api/src/models.rs +++ b/affluences-api/src/models.rs @@ -38,3 +38,134 @@ pub struct Resource { pub slots_state: u32, pub hours: Vec, } + +#[derive(Deserialize, Debug)] +pub struct Data { + pub data: T +} + +#[derive(Deserialize, Debug)] +pub struct SiteDataCategory { + pub id: u32, + pub name: String, + pub name_plural: String, +} + +#[derive(Deserialize, Debug)] +pub struct SiteDataLocationCoordinates { + pub latitude: f64, + pub longitude: f64, +} + +#[derive(Deserialize, Debug)] +pub struct SiteDataLocationAddress { + pub route: String, + pub city: String, + pub zip_code: String, + pub region: String, + pub country_code: String +} + +#[derive(Deserialize, Debug)] +pub struct SiteDataLocation { + pub coordinates: SiteDataLocationCoordinates, + pub address: SiteDataLocationAddress +} + +#[derive(Deserialize, Debug)] +pub struct SiteDataForecast { + pub opened: bool, + pub occupancy: u64, + // waiting_time + pub waiting_time_overflow: bool +} + +#[derive(Deserialize, Debug)] +pub struct SiteDataNotice { + message: String, + url: Option +} + +#[derive(Deserialize, Debug)] +pub struct SiteDataChild { + pub id: uuid::Uuid, + pub slug: String, + pub parent: Option, + pub primary_name: String, + pub secondary_name: String, + pub concat_name: String, + pub categories: Vec, + pub time_zone: String, + pub location: SiteDataLocation, + pub phone_number: String, + pub email: String, + pub url: String, + pub notices: Vec, + // messages + pub estimated_distance: f64, + pub current_forecast: SiteDataForecast, + pub today_forecasts: Vec, + // events + // children + // actions + // services + // infos + pub poster_image: String, + // images +} + +#[derive(Deserialize, Debug)] +pub struct SiteDataService { + pub id: u64, + pub name: String, +} + +#[derive(Deserialize, Debug)] +pub struct SiteDataInfo { + pub title: String, + pub description: String, + pub url: Option, +} + +#[derive(Deserialize, Debug)] +pub struct SiteDataStatus { + pub state: String, + pub text: String, + pub color: String, +} + +#[derive(Deserialize, Debug)] +pub struct SiteData { + pub id: uuid::Uuid, + pub slug: String, + parent: Option, + pub primary_name: String, + pub secondary_name: String, + pub concat_name: String, + pub categories: Vec, + pub time_zone: String, + pub location: SiteDataLocation, + pub phone_number: Option, + pub email: Option, + pub url: Option, + notices: Vec, + // messages + pub estimated_distance: f64, + pub current_forecast: SiteDataForecast, + pub today_forecasts: Vec, + // events + pub children: Vec, + // actions + pub services: Vec, + pub infos: Vec, + pub poster_image: String, + pub image: Option>, + // status + pub closed: bool, + pub booking_available: bool, + pub extended_forecasts: bool, + pub booking_url: Option, + pub validated: bool, + pub validationStatus: String, + pub publicationStatus: String +} diff --git a/bot/Cargo.toml b/bot/Cargo.toml index b4ba5ea..c78584f 100644 --- a/bot/Cargo.toml +++ b/bot/Cargo.toml @@ -8,3 +8,4 @@ edition = "2021" [dependencies] affluences-api = { path = "../affluences-api" } tokio = { version = "1.28.1", features = ["full"] } +chrono = "*" diff --git a/bot/src/main.rs b/bot/src/main.rs index f9e545c..c1cb509 100644 --- a/bot/src/main.rs +++ b/bot/src/main.rs @@ -1,8 +1,17 @@ use affluences_api::AffluencesClient; +use chrono::NaiveDate; #[tokio::main] async fn main() { + let site = "ghent-university"; + + // let uuid = "026b8caa-d310-464f-b714-4c21a2cf98ea"; let mut client = AffluencesClient::new(); - let res = client.available().await.unwrap(); + let res = client.site_data(site).await.unwrap(); + + // let res = client + // .available(uuid, NaiveDate::from_ymd_opt(2023, 5, 11).unwrap(), 1) + // .await + // .unwrap(); println!("{:?}", res); }