Skip to content

Commit

Permalink
Add struct support for trip data
Browse files Browse the repository at this point in the history
  • Loading branch information
kylerchin committed Oct 10, 2024
1 parent da82b89 commit 32336e5
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 4 deletions.
38 changes: 37 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
58 changes: 55 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
#[serde(rename = "RouteID")]
route_id: i32,
#[serde(rename = "RouteStopID")]
route_stop_id: i32,
#[serde(rename = "ScheduledTimes")]
scheduled_times: Vec<RawScheduledTimes>,
#[serde(rename = "ShowDefaultedOnMap")]
show_defaulted_on_map: bool,
#[serde(rename = "ShowEstimatesOnMap")]
show_estimates_on_map: bool,
#[serde(rename = "StopDescription")]
stop_description: Option<String>,
#[serde(rename = "StopId")]
stop_id: i32,
#[serde(rename = "VehicleEstimates")]
vehicle_estimates: Vec<RawVehicleEstimates>,
}

#[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<gtfs_realtime::FeedMessage, Box<dyn std::error::Error + Send + Sync>>
{
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<String> {
Expand Down Expand Up @@ -68,9 +119,10 @@ pub fn get_trip_id(route_id: i32) -> Option<String> {
* 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<gtfs_realtime::FeedMessage, Box<dyn std::error::Error + Send + Sync>> {
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<gtfs_realtime::FeedEntity> = Vec::new();
Expand Down

0 comments on commit 32336e5

Please sign in to comment.