From: Jakob Cornell Date: Sat, 21 Sep 2024 05:20:38 +0000 (-0500) Subject: Update table utility to request full 60 minutes of data X-Git-Url: https://jcornell.net/gitweb/gitweb.cgi?a=commitdiff_plain;h=refs%2Fheads%2Fmain;p=mta.git Update table utility to request full 60 minutes of data --- diff --git a/Cargo.lock b/Cargo.lock index 24f7797..bb8e94a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -234,6 +234,7 @@ name = "dump_stop" version = "0.1.0" dependencies = [ "ascii_table", + "chrono", "clap", "mta_live_client", ] diff --git a/dump_stop/Cargo.toml b/dump_stop/Cargo.toml index 4eb2150..a21d98c 100644 --- a/dump_stop/Cargo.toml +++ b/dump_stop/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" edition = "2021" [dependencies] -mta_live_client = { path = "../mta_live_client" } -clap = { version = "4", features = ["derive"] } ascii_table = { version = "4" } +chrono = { version = "0" } +clap = { version = "4", features = ["derive"] } +mta_live_client = { path = "../mta_live_client" } diff --git a/dump_stop/src/main.rs b/dump_stop/src/main.rs index c7fcdf5..316f436 100644 --- a/dump_stop/src/main.rs +++ b/dump_stop/src/main.rs @@ -2,6 +2,7 @@ use std::collections::HashSet; use std::fmt::Display; use ascii_table::AsciiTable; +use chrono::TimeDelta; use clap::Parser; /// Dump upcoming train ETAs at a given stop @@ -19,7 +20,11 @@ struct Args { fn main() { let args = Args::parse(); - let stop_responses = mta_live_client::get_nearby(&HashSet::from([&args.stop_id])).unwrap(); + let stop_responses = mta_live_client::get_nearby( + &HashSet::from([&args.stop_id]), + TimeDelta::minutes(60), // maximum window size supported by API + ) + .unwrap(); let stop_response = match stop_responses.len() { 1 => stop_responses.into_iter().next().unwrap(), length => panic!("expected 1 stop response from API, got {length}"), diff --git a/mta_live_client/Cargo.toml b/mta_live_client/Cargo.toml index cc43fb4..3e5576e 100644 --- a/mta_live_client/Cargo.toml +++ b/mta_live_client/Cargo.toml @@ -4,8 +4,8 @@ version = "0.1.0" edition = "2021" [dependencies] -reqwest = { version = "0.12", features = ["blocking", "json"] } -serde_json = { version = "1" } -serde = { version = "1", features = ["derive"] } chrono = { version = "0" } clap = { version = "4", features = ["derive"] } +reqwest = { version = "0.12", features = ["blocking", "json"] } +serde = { version = "1", features = ["derive"] } +serde_json = { version = "1" } diff --git a/mta_live_client/src/lib.rs b/mta_live_client/src/lib.rs index 651e3e8..e012a55 100644 --- a/mta_live_client/src/lib.rs +++ b/mta_live_client/src/lib.rs @@ -1,7 +1,7 @@ use std::collections::HashSet; use chrono::offset::FixedOffset; -use chrono::DateTime; +use chrono::{DateTime, TimeDelta}; use reqwest::Url; use serde::Deserialize; @@ -58,17 +58,25 @@ pub struct StopResponse { pub fn get_nearby( stop_ids: &HashSet>, + window_size: TimeDelta, ) -> Result, reqwest::Error> { + assert!( + window_size >= TimeDelta::zero(), + "expected non-negative window size" + ); let url = Url::parse_with_params( "https://otp-mta-prod.camsys-apps.com/otp/routers/default/nearby", - &[( - "stops", - stop_ids - .iter() - .map(AsRef::as_ref) - .collect::>() - .join(","), - )], + &[ + ( + "stops", + stop_ids + .iter() + .map(AsRef::as_ref) + .collect::>() + .join(","), + ), + ("timeRange", window_size.num_seconds().to_string()), + ], ) .unwrap(); reqwest::blocking::get(url)?.json()