From a78f00505c765c6f9d0bd819d4c0db1ba743afac Mon Sep 17 00:00:00 2001 From: Kyler Chin <7539174+kylerchin@users.noreply.github.com> Date: Sat, 10 Aug 2024 22:37:39 -0700 Subject: [PATCH] Update dependencies, switch gtfs_rt to gtfs_realtime --- Cargo.toml | 9 ++++---- src/lib.rs | 58 ++++++++++++++++++++++++----------------------- src/stop_times.rs | 1 - 3 files changed, 35 insertions(+), 33 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 427b49b..04faf14 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "amtrak-gtfs-rt" -version = "0.2.6" +version = "0.3.0" license = "AGPL-3.0" description = "Converts Amtrak Track-A-Train to valid GTFS-rt vehicle and trip information" edition = "2021" @@ -15,16 +15,17 @@ non_ascii_idents = "deny" [dependencies] amtk = "0.1.0" -amtrak-api = "0.1.0" -base64 = "0.21" +amtrak-api = "0.2.0" +base64 = "0.22" chrono = "0.4" chrono-tz = "0.9" futures = "0.3.30" geojson = "0.24" +gtfs-realtime = "0.1.0" gtfs-rt = "0.5" gtfs-structures = "0.41.2" openssl = "0.10" -prost = "0.11" +prost = "0.13" reqwest = {version = "0.12", features = ["gzip","brotli","rustls-tls","deflate","cookies"]} rust-crypto = "0.2" serde = "1.0" diff --git a/src/lib.rs b/src/lib.rs index 23814c1..acf8587 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -32,7 +32,7 @@ //! Note that the Metropolitan Transportation Commission also publishes Capital Corridor in their own feed. //! https://511.org/open-data/transit provides Capital Corridor as "CC". This data refreshes more often (and is closer in location & time), and shows locomotive numbers. //! For this reason, you may wish to remove Capital Corridor from this feed. -//! Thus, we've included a function `filter_capital_corridor()` which takes in any `gtfs_rt::FeedMessage` and removes CC vehicles and trips. +//! Thus, we've included a function `filter_capital_corridor()` which takes in any `FeedMessage` and removes CC vehicles and trips. use chrono::{DateTime, Datelike, NaiveDate, NaiveDateTime, TimeZone, Weekday}; @@ -42,12 +42,14 @@ use std::collections::HashMap; use std::time::SystemTime; pub mod stop_times; use crate::stop_times::RootTripData; +use gtfs_realtime::FeedMessage; +use gtfs_realtime::FeedEntity; //Written by Kyler Chin - Catenary Transit Initiatives. -pub fn filter_capital_corridor(input: gtfs_rt::FeedMessage) -> gtfs_rt::FeedMessage { +pub fn filter_capital_corridor(input: FeedMessage) -> FeedMessage { let cc_route_id = "84"; - gtfs_rt::FeedMessage { + FeedMessage { entity: input .entity .into_iter() @@ -79,20 +81,20 @@ pub fn filter_capital_corridor(input: gtfs_rt::FeedMessage) -> gtfs_rt::FeedMess true }) - .collect::>(), + .collect::>(), header: input.header, } } #[derive(Clone, Debug)] pub struct GtfsAmtrakResults { - pub trip_updates: gtfs_rt::FeedMessage, - pub vehicle_positions: gtfs_rt::FeedMessage, + pub trip_updates: FeedMessage, + pub vehicle_positions: FeedMessage, } #[derive(Clone, Debug)] pub struct GtfsAmtrakResultsJoined { - pub unified_feed: gtfs_rt::FeedMessage, + pub unified_feed: FeedMessage, } #[derive(serde::Deserialize)] @@ -184,7 +186,7 @@ fn get_bearing(feature: &geojson::Feature) -> Option { } } -fn feature_to_gtfs_unified(gtfs: &Gtfs, feature: &geojson::Feature) -> gtfs_rt::FeedEntity { +fn feature_to_gtfs_unified(gtfs: &Gtfs, feature: &geojson::Feature) -> FeedEntity { let geometry = feature.geometry.as_ref().unwrap(); let point: Option = match geometry.value.clone() { geojson::Value::Point(x) => Some(x), @@ -252,20 +254,20 @@ fn feature_to_gtfs_unified(gtfs: &Gtfs, feature: &geojson::Feature) -> gtfs_rt:: } .unwrap(); - let arrivals: Vec = + let arrivals: Vec = feature_to_amtrak_arrival_structs(feature) .iter() - .map(|feature| gtfs_rt::trip_update::StopTimeUpdate { + .map(|feature| gtfs_realtime::trip_update::StopTimeUpdate { stop_sequence: None, stop_id: Some(feature.code.clone()), arrival: match &feature.postarr { - Some(postarr) => Some(gtfs_rt::trip_update::StopTimeEvent { + Some(postarr) => Some(gtfs_realtime::trip_update::StopTimeEvent { delay: None, time: Some(time_and_tz_to_unix(postarr, feature.tz)), uncertainty: None, }), None => match &feature.estarr { - Some(estarr) => Some(gtfs_rt::trip_update::StopTimeEvent { + Some(estarr) => Some(gtfs_realtime::trip_update::StopTimeEvent { delay: None, time: Some(time_and_tz_to_unix(&estarr, feature.tz)), uncertainty: None, @@ -274,13 +276,13 @@ fn feature_to_gtfs_unified(gtfs: &Gtfs, feature: &geojson::Feature) -> gtfs_rt:: } }, departure: match &feature.postdep { - Some(postdep) => Some(gtfs_rt::trip_update::StopTimeEvent { + Some(postdep) => Some(gtfs_realtime::trip_update::StopTimeEvent { delay: None, time: Some(time_and_tz_to_unix(postdep, feature.tz)), uncertainty: None, }), None => match &feature.estdep { - Some(estdep) => Some(gtfs_rt::trip_update::StopTimeEvent { + Some(estdep) => Some(gtfs_realtime::trip_update::StopTimeEvent { delay: None, time: Some(time_and_tz_to_unix(&estdep, feature.tz)), uncertainty: None, @@ -291,7 +293,7 @@ fn feature_to_gtfs_unified(gtfs: &Gtfs, feature: &geojson::Feature) -> gtfs_rt:: schedule_relationship: None, stop_time_properties: None, }) - .collect::>(); + .collect::>(); let origin_local_time = origin_departure(&origin_time_string, origin_tz); @@ -369,7 +371,7 @@ fn feature_to_gtfs_unified(gtfs: &Gtfs, feature: &geojson::Feature) -> gtfs_rt:: let bearing: Option = get_bearing(feature); - let trip_desc = gtfs_rt::TripDescriptor { + let trip_desc = gtfs_realtime::TripDescriptor { trip_id, route_id, direction_id: None, @@ -379,14 +381,14 @@ fn feature_to_gtfs_unified(gtfs: &Gtfs, feature: &geojson::Feature) -> gtfs_rt:: schedule_relationship: None, }; - gtfs_rt::FeedEntity { + FeedEntity { alert: None, id: id.unwrap(), is_deleted: Some(false), trip_modifications: None, stop: None, shape: None, - trip_update: Some(gtfs_rt::TripUpdate { + trip_update: Some(gtfs_realtime::TripUpdate { vehicle: None, trip: trip_desc.clone(), timestamp, @@ -394,7 +396,7 @@ fn feature_to_gtfs_unified(gtfs: &Gtfs, feature: &geojson::Feature) -> gtfs_rt:: stop_time_update: arrivals, trip_properties: None, }), - vehicle: Some(gtfs_rt::VehiclePosition { + vehicle: Some(gtfs_realtime::VehiclePosition { stop_id: None, current_status: None, timestamp, @@ -405,7 +407,7 @@ fn feature_to_gtfs_unified(gtfs: &Gtfs, feature: &geojson::Feature) -> gtfs_rt:: current_stop_sequence: None, vehicle: None, trip: Some(trip_desc.clone()), - position: Some(gtfs_rt::Position { + position: Some(gtfs_realtime::Position { speed, odometer: None, bearing, @@ -416,8 +418,8 @@ fn feature_to_gtfs_unified(gtfs: &Gtfs, feature: &geojson::Feature) -> gtfs_rt:: } } -pub fn make_gtfs_header() -> gtfs_rt::FeedHeader { - gtfs_rt::FeedHeader { +pub fn make_gtfs_header() -> gtfs_realtime::FeedHeader { + gtfs_realtime::FeedHeader { gtfs_realtime_version: String::from("2.0"), incrementality: None, timestamp: Some( @@ -483,8 +485,8 @@ pub async fn fetch_amtrak_gtfs_rt( ) -> Result> { let joined_res = fetch_amtrak_gtfs_rt_joined(gtfs, client).await; - let mut vehicles: Vec = vec![]; - let mut trips: Vec = vec![]; + let mut vehicles: Vec = vec![]; + let mut trips: Vec = vec![]; match joined_res { Ok(joined_res) => { @@ -494,11 +496,11 @@ pub async fn fetch_amtrak_gtfs_rt( } Ok(GtfsAmtrakResults { - trip_updates: gtfs_rt::FeedMessage { + trip_updates: FeedMessage { entity: trips, header: joined_res.unified_feed.header.clone(), }, - vehicle_positions: gtfs_rt::FeedMessage { + vehicle_positions: FeedMessage { entity: vehicles, header: joined_res.unified_feed.header.clone(), }, @@ -570,12 +572,12 @@ pub async fn fetch_amtrak_gtfs_rt_joined( //println!("Successfully decrypted"); //println!("{}", decrypted_string); Ok(GtfsAmtrakResultsJoined { - unified_feed: gtfs_rt::FeedMessage { + unified_feed: FeedMessage { entity: features_collection .features .iter() .map(|feature: &geojson::Feature| feature_to_gtfs_unified(>fs, feature)) - .collect::>(), + .collect::>(), header: make_gtfs_header(), }, }) diff --git a/src/stop_times.rs b/src/stop_times.rs index fd51af4..ad9b030 100644 --- a/src/stop_times.rs +++ b/src/stop_times.rs @@ -122,7 +122,6 @@ pub struct StatusInfo { pub as_of: String, } - pub async fn query_all_trips_simultaniously(train_numbers: &Vec<(String,NaiveDate)>) -> HashMap<(String, NaiveDate), RootTripData> { let client = reqwest::Client::new();