summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/main.rs56
-rw-r--r--src/onboard/iceportal.rs4
-rw-r--r--src/onboard/zugportal.rs4
-rw-r--r--src/traits.rs30
-rw-r--r--src/types.rs10
5 files changed, 57 insertions, 47 deletions
diff --git a/src/main.rs b/src/main.rs
index 0a8fc54..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,8 +364,8 @@ 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>>()
diff --git a/src/onboard/iceportal.rs b/src/onboard/iceportal.rs
index bde0b1f..55b35e7 100644
--- a/src/onboard/iceportal.rs
+++ b/src/onboard/iceportal.rs
@@ -75,8 +75,8 @@ impl IsStation for Stop {
self.timetable.scheduled_arrival_time.as_ref()
}
- fn ds100(&self) -> &str {
- "??"
+ fn ds100(&self) -> Option<&str> {
+ None
}
}
diff --git a/src/onboard/zugportal.rs b/src/onboard/zugportal.rs
index c649107..fe083ef 100644
--- a/src/onboard/zugportal.rs
+++ b/src/onboard/zugportal.rs
@@ -67,8 +67,8 @@ impl IsStation for Stop {
self.arrival_time.as_ref().map(|t| &t.predicted)
}
- fn ds100(&self) -> &str {
- "??"
+ fn ds100(&self) -> Option<&str> {
+ None
}
}
diff --git a/src/traits.rs b/src/traits.rs
index 091709a..b5785be 100644
--- a/src/traits.rs
+++ b/src/traits.rs
@@ -6,22 +6,32 @@ pub trait IsStation {
fn name(&self) -> &str;
fn scheduled_arrival(&self) -> Option<&DateTime<Utc>>;
fn real_arrival(&self) -> Option<&DateTime<Utc>>;
- fn ds100(&self) -> &str;
+ fn ds100(&self) -> Option<&str>;
fn to_fancy_string(&self) -> String {
- // travelynx literally sends this entire precise date in case of an
- // unknown time instead of, like, a null
+ // travelynx used to send this entire precise date in case of an
+ // unknown time instead of null. I'm not sure it still does this
+ // (i have since seen it return null) but keeping this here just
+ // in case
let epoch = "1970-01-01T00:00:00Z".parse::<DateTime<Local>>().unwrap();
+
+ let format_time = |time: &DateTime<Utc>| -> String {
+ if time.eq(&epoch) {
+ "??:??:?? ".to_owned()
+ } else { // chrono's API for timezones is expressive, but reads like c++ …
+ <DateTime<Local>>::from(*time).time().format("%T").to_string()
+ }
+ };
+
format!(
"{} {} – {} ({})",
self
.real_arrival()
- .map(|t| if t.eq(&epoch) {
- "unknown ".to_owned()
- } else { // chrono's API for timezones is expressive, but reads like c++ …
- <DateTime<Local>>::from(*t).time().format("%T").to_string()
- })
- .unwrap_or("<format error>".to_string())
+ .map(format_time)
+ .unwrap_or_else(||
+ self.scheduled_arrival()
+ .map(format_time).unwrap_or_else(|| "??:??:??".to_string())
+ )
.blue(),
{
let delay = match (self.real_arrival(), self.scheduled_arrival()) {
@@ -35,7 +45,7 @@ pub trait IsStation {
text.green()
}
},
- self.ds100().red(),
+ self.ds100().unwrap_or("??").red(),
self.name()
)
}
diff --git a/src/types.rs b/src/types.rs
index 257f857..b698898 100644
--- a/src/types.rs
+++ b/src/types.rs
@@ -10,7 +10,7 @@ use crate::traits::IsStation;
#[serde(rename_all = "camelCase")]
pub struct Station {
name: String,
- pub ds100: String,
+ pub ds100: Option<String>,
uic: u64,
latitude: f64,
longitude: f64,
@@ -56,8 +56,8 @@ impl IsStation for Station {
Some(&self.real_time)
}
- fn ds100(&self) -> &str {
- &self.ds100
+ fn ds100(&self) -> Option<&str> {
+ self.ds100.as_deref()
}
}
@@ -72,8 +72,8 @@ impl IsStation for Stop {
self.real_arrival.as_ref()
}
- fn ds100(&self) -> &str {
- "[??]"
+ fn ds100(&self) -> Option<&str> {
+ None
}
}