diff options
Diffstat (limited to '')
-rw-r--r-- | src/main.rs | 59 |
1 files changed, 31 insertions, 28 deletions
diff --git a/src/main.rs b/src/main.rs index 2f3a34e..3c5fc5f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,18 +1,15 @@ -use chrono::{DateTime, Utc}; +use chrono::NaiveDateTime; use clap::{Parser, Subcommand}; use colored::*; +use railway_provider_hafas::client::HafasClient; +use railway_provider_hafas::profile::db::DbProfile; use serde::Deserialize; use serde_json::json; use traveltext::onboard::{choose_api, OnBoardAPI}; use traveltext::{traits::*, travelynx::*, types::*}; -use hafas_rs::api::journeys::*; -use hafas_rs::api::locations::*; -use hafas_rs::client::HafasClient; -use hafas_rs::profile::db::DbProfile; -use hafas_rs::requester::hyper::HyperRustlsRequester; -use hafas_rs::{Place, ProductsSelection}; +use railway_core::{HyperRustlsRequesterBuilder, IntermediateLocation, JourneysOptions, LocationsOptions, Place, ProductsSelection, Provider}; #[derive(Parser)] struct Cli { @@ -54,7 +51,7 @@ enum Command { /// destination station's name (fuzzy-matched) to_name: String, /// departure time; takes the next available connection - time: Option<DateTime<Utc>>, + time: Option<NaiveDateTime>, /// set the dryRun flag to 'true' when importing to travelynx #[clap(short = 'd', long = "dry-run")] dry_run: bool, @@ -188,7 +185,7 @@ async fn main() -> Result<(), ureq::Error> { &format!("{}/api/v1/travel", cli.baseurl), Action::CheckIn { train: train.into(), - to_station: status.to_station.map(|s| s.ds100), + to_station: status.to_station.map(|s| s.ds100).flatten(), token: config.token_travel, comment: None, from_station: from @@ -283,11 +280,11 @@ async fn main() -> Result<(), ureq::Error> { dry_run, import } => { - let c = HafasClient::new(DbProfile, HyperRustlsRequester::new()); + let c = HafasClient::new(DbProfile, HyperRustlsRequesterBuilder::default()); let from = &c .locations(LocationsOptions { query: from_name, - results: None, + results: 0, // TODO: respect locale set language: Some("de".to_string()) }) @@ -297,19 +294,19 @@ async fn main() -> Result<(), ureq::Error> { let to = &c .locations(LocationsOptions { query: to_name, - results: None, + results: 0, language: None }) .await .unwrap()[0]; + let tz_str = iana_time_zone::get_timezone().unwrap(); + let tz: chrono_tz::Tz = tz_str.parse().unwrap(); + let opts = JourneysOptions { - products: ProductsSelection { - bus: Some(false), - ..ProductsSelection::default() - }, - departure: time.map(|t| t.timestamp()), //Some(1650536340); - stopovers: Some(true), + products: ProductsSelection::all(), + departure: time.map(|naive| naive.and_local_timezone(tz).unwrap()), //Some(1650536340); + stopovers: true, language: Some("de".to_string()), ..JourneysOptions::default() }; @@ -321,13 +318,16 @@ async fn main() -> Result<(), ureq::Error> { .journeys[0] .legs[0]; - let stops = journey.stopovers.as_ref().unwrap(); + let stops = journey.intermediate_locations + .iter() + .filter_map(|l| match l { IntermediateLocation::Stop(s) => Some(s), _ => None }) + .collect::<Vec<_>>(); println!("{}: found this trip:", traveltext); - for stop in stops { - match &stop.stop { - Place::Stop(station) => { + for stop in &stops { + match &stop.place { + Place::Station(station) => { println!("{}", station.name.as_ref().unwrap()) } _ => panic!("this train stops somewhere that's not a station??") @@ -340,15 +340,15 @@ async fn main() -> Result<(), ureq::Error> { let travelynx = json!({ "token": config.token_import.unwrap(), "dryRun": dry_run, - "fromStation": match &stops[0].stop { - Place::Stop (station) => json!({ + "fromStation": match &stops[0].place { + Place::Station (station) => json!({ "name":station.name, "scheduledTime":stops[0].departure.unwrap().timestamp() }), _ => panic!("this trip lacks a first station?") }, - "toStation": match &stops[stops.len()-1].stop { - Place::Stop (station) => json!({ + "toStation": match &stops[stops.len()-1].place { + Place::Station (station) => json!({ "name": station.name, "scheduledTime": stops[stops.len()-1].arrival.unwrap().timestamp() }), @@ -364,12 +364,15 @@ async fn main() -> Result<(), ureq::Error> { }, "intermediateStops": &stops[1..stops.len()-1] .iter() - .filter_map(|s| match &s.stop { - Place::Stop (station) => station.name.as_ref(), + .filter_map(|s| match &s.place { + Place::Station (station) => station.name.as_ref(), _ => None }) .collect::<Vec<&String>>() }); + if cli.debug { + println!("{}", serde_json::to_string(&travelynx).unwrap()); + } let resp: serde_json::Value = exiting_post_request( &format!("{}/api/v1/import", cli.baseurl), travelynx, |