feat: adapt to db api's new line number format
This commit is contained in:
@@ -33,8 +33,12 @@ pub struct DeparturesMerklingen1 {
|
||||
pub struct DeparturesMerklingen4 {
|
||||
#[serde(rename = "linienNummer")]
|
||||
pub line_number: Option<String>,
|
||||
#[serde(rename = "kurzText")]
|
||||
pub product: Option<String>,
|
||||
// #[serde(rename = "kurzText")]
|
||||
// pub text_short: Option<String>,
|
||||
#[serde(rename = "mittelText")]
|
||||
pub text_medium: Option<String>,
|
||||
// #[serde(rename = "langText")]
|
||||
// pub text_long: Option<String>,
|
||||
}
|
||||
#[derive(Deserialize)]
|
||||
pub struct DeparturesMerklingen2 {
|
||||
@@ -55,12 +59,23 @@ pub async fn departures_merklingen() -> Result<DeparturesMerklingen, String> {
|
||||
let url = r#"https://www.bahn.de/web/api/reiseloesung/abfahrten?ortExtId=8003983&mitVias=true&maxVias=8&verkehrsmittel[]=ICE&verkehrsmittel[]=EC_IC&verkehrsmittel[]=IR&verkehrsmittel[]=REGIONAL&verkehrsmittel[]=SBAHN"#;
|
||||
match reqwest::get(url).await {
|
||||
Ok(response) => match response.text().await {
|
||||
Ok(response) => match serde_json::from_str::<DeparturesMerklingen>(&response) {
|
||||
Ok(response) => Ok(response),
|
||||
Err(e) => Err(format!(
|
||||
"Couldn't parse HTTP response from URL {url:?}: {e}\nResponse was (raw):\n{response}"
|
||||
))?,
|
||||
},
|
||||
Ok(response) => {
|
||||
#[cfg(debug_assertions)]
|
||||
eprintln!(
|
||||
"|> GET {url}\n| {}\n",
|
||||
serde_json::from_str::<serde_json::Value>(&response)
|
||||
.and_then(|v| serde_json::to_string_pretty(&v))
|
||||
.unwrap_or(response.clone())
|
||||
.trim()
|
||||
.replace("\n", "\n| ")
|
||||
);
|
||||
match serde_json::from_str::<DeparturesMerklingen>(&response) {
|
||||
Ok(response) => Ok(response),
|
||||
Err(e) => Err(format!(
|
||||
"Couldn't parse HTTP response from URL {url:?}: {e}\nResponse was (raw):\n{response}"
|
||||
))?,
|
||||
}
|
||||
}
|
||||
Err(e) => Err(format!("Couldn't get HTTP response from URL {url:?}: {e}"))?,
|
||||
},
|
||||
Err(e) => Err(format!("Couldn't make GET request to URL {url:?}: {e}"))?,
|
||||
|
||||
14
src/main.rs
14
src/main.rs
@@ -125,15 +125,13 @@ async fn index(
|
||||
.line_name
|
||||
.as_ref()
|
||||
.map(|v| {
|
||||
v.product.as_ref().map(|prod| {
|
||||
format!(
|
||||
"{prod} {}",
|
||||
v.line_number.as_ref().map(|v| v.as_str()).unwrap_or("?")
|
||||
)
|
||||
})
|
||||
v.text_medium
|
||||
.as_ref()
|
||||
.map(|v| v.as_str())
|
||||
.or(v.line_number.as_ref().map(|v| v.as_str()))
|
||||
})
|
||||
.flatten()
|
||||
.unwrap_or_else(|| "[RE ?]".to_owned());
|
||||
.unwrap_or("[RE ?]");
|
||||
let is_ulm = {
|
||||
if let Some(destination) = departure.destination.as_ref() {
|
||||
let name = destination.to_lowercase();
|
||||
@@ -152,7 +150,7 @@ async fn index(
|
||||
}
|
||||
}
|
||||
.or_else(|| {
|
||||
match departure.platform_schedule.as_ref().map(|v| v.as_str()) {
|
||||
match departure.platform_schedule.as_ref().map(|v| v.trim()) {
|
||||
Some("1") => Some(true),
|
||||
Some("4") => Some(false),
|
||||
_ => None,
|
||||
|
||||
Reference in New Issue
Block a user