From 32336e5906085bc0762d461f8f3ffd11b693d4c3 Mon Sep 17 00:00:00 2001 From: Kyler Chin <7539174+kylerchin@users.noreply.github.com> Date: Wed, 9 Oct 2024 22:21:03 -0700 Subject: [PATCH] Add struct support for trip data --- Cargo.lock | 38 ++++++++++++++++++++++++++++++++++- Cargo.toml | 1 + src/lib.rs | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 93 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ada41fc..67ce305 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -251,6 +251,15 @@ dependencies = [ "pkg-config", ] +[[package]] +name = "castaway" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0abae9be0aaf9ea96a3b1b8b1b55c602ca751eba1b1500220cea4ecbafe7c0d5" +dependencies = [ + "rustversion", +] + [[package]] name = "cc" version = "1.1.28" @@ -340,6 +349,20 @@ dependencies = [ "tracing-error", ] +[[package]] +name = "compact_str" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6050c3a16ddab2e412160b31f2c871015704239bca62f72f6e5f0be631d3f644" +dependencies = [ + "castaway", + "cfg-if", + "itoa", + "rustversion", + "ryu", + "static_assertions", +] + [[package]] name = "constant_time_eq" version = "0.1.5" @@ -1939,6 +1962,12 @@ dependencies = [ "untrusted", ] +[[package]] +name = "rustversion" +version = "1.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "955d28af4278de8121b7ebeb796b6a45735dc01436d898801014aced2773a3d6" + [[package]] name = "ryu" version = "1.0.18" @@ -2128,6 +2157,12 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" +[[package]] +name = "static_assertions" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" + [[package]] name = "subtle" version = "2.6.1" @@ -2828,11 +2863,12 @@ dependencies = [ [[package]] name = "zotgtfs" -version = "0.3.6" +version = "0.3.7" dependencies = [ "chrono", "chrono-tz", "color-eyre", + "compact_str", "csv", "geo", "geo-types", diff --git a/Cargo.toml b/Cargo.toml index ec51755..dbd6f5c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ non_ascii_idents = "deny" chrono = "0.4.31" chrono-tz = "0.10.0" color-eyre = "0.6.2" +compact_str = "0.8.0" csv = "1.3.0" geo = "0.28.0" geo-types = "0.7.11" diff --git a/src/lib.rs b/src/lib.rs index 7e59103..8bf3186 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,18 +9,69 @@ use std::error::Error; use chrono::Datelike; use chrono_tz::Tz; use std::time::{SystemTime, UNIX_EPOCH}; +use compact_str::CompactString; +#[derive(Deserialize)] +struct RawTranslocArrival { + #[serde(rename = "RouteDescription")] + route_description: Option, + #[serde(rename = "RouteID")] + route_id: i32, + #[serde(rename = "RouteStopID")] + route_stop_id: i32, + #[serde(rename = "ScheduledTimes")] + scheduled_times: Vec, + #[serde(rename = "ShowDefaultedOnMap")] + show_defaulted_on_map: bool, + #[serde(rename = "ShowEstimatesOnMap")] + show_estimates_on_map: bool, + #[serde(rename = "StopDescription")] + stop_description: Option, + #[serde(rename = "StopId")] + stop_id: i32, + #[serde(rename = "VehicleEstimates")] + vehicle_estimates: Vec, +} + +#[derive(Deserialize)] +struct RawVehicleEstimates { + #[serde(rename = "Block")] + block: String, + #[serde(rename = "OnRoute")] + on_route: bool, + #[serde(rename = "SecondsToStop")] + seconds_to_stop: i32, + #[serde(rename = "VehicleID")] + vehicle_id: u16, +} + +#[derive(Deserialize)] +struct RawScheduledTimes { + #[serde(rename = "ArrivalTimeUTC")] + arrival_time_utc: String, + #[serde(rename = "DepartureTimeUTC")] + departure_time_utc: String, + #[serde(rename = "Block")] + block: String, + #[serde(rename = "AssignedVehicleId")] + assigned_vehicle_id: String, +} /** * Fetches jsonp data from ucirvine's transit feed and converts it into gtfs_rt */ pub async fn get_gtfs_rt() -> Result> { + let trip_data = reqwest::get("https://ucirvine.transloc.com/Services/JSONPRelay.svc/GetRouteStopArrivals?TimesPerStopString=100&ApiKey=8882812681&_=1728535266772") + .await? + .text() + .await?; + let data = reqwest::get("https://ucirvine.transloc.com/Services/JSONPRelay.svc/GetMapVehiclePoints?_=1712182850877") .await? .text() .await?; - gtfs_rt_from_string(data) + gtfs_rt_from_string(data, trip_data) } pub fn get_trip_id(route_id: i32) -> Option { @@ -68,9 +119,10 @@ pub fn get_trip_id(route_id: i32) -> Option { * Function creates gtfs from a string, called by get_gtfs_rt and used for testing. */ fn gtfs_rt_from_string( - data: String, + vehicle_data: String, + trip_data: String ) -> Result> { - let data = parse_data(data)?; + let data = parse_data(vehicle_data)?; // if data parsed is empty (at night for example) returns an empty gtfs_rt feed. if data.len() == 0 { let empty_entity: Vec = Vec::new();