Structure of JSON generated

This commit is contained in:
Ollo 2023-08-18 23:06:17 +02:00
parent 57d8cdfd23
commit bccfc22770
2 changed files with 60 additions and 6 deletions

View File

@ -17,3 +17,7 @@ datetime = "0.5.2"
# Necessary for web crawler # Necessary for web crawler
openssl = { version = "0.10", features = ["vendored"] } openssl = { version = "0.10", features = ["vendored"] }
reqwest = { version = "0.11", features = ["blocking", "json"] } reqwest = { version = "0.11", features = ["blocking", "json"] }
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
# end of web stuff

View File

@ -2,12 +2,62 @@
* @brief fetch next depature of light rail vehicle * @brief fetch next depature of light rail vehicle
*/ */
use std::error::Error; use std::error::Error;
use serde::Deserialize;
const stationURL:&str = "https://www.rnv-online.de/rest/departure/2494/1"; const stationURL:&str = "https://www.rnv-online.de/rest/departure/2494";
pub fn fetchData() -> Result<(), Box<dyn Error>> { #[derive(Default, Debug, Clone, PartialEq, Deserialize)]
println!("Test RNV API"); #[serde(rename_all = "camelCase")]
let resp = reqwest::blocking::get(stationURL)?.text()?; pub struct Station {
println!("{:#?}", resp); pub id: i64,
Ok(()) pub name: String,
pub lines: Vec<Line>,
pub journeys: Vec<Journey>,
}
#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Line {
pub id: i64,
pub text: String,
pub iconOutlined: i64,
pub iconColor: String,
pub iconTextColor: i64,
}
#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Journey {
pub line: Line,
pub destination: String,
pub barrierLevel: String,
pub loadsForecastType: String,
pub realtimeDeparture: i64,
pub scheduledDeparture: i64,
pub difference: i64,
pub canceled: bool,
}
pub struct NextDeparture {
pub rheinau: i64,
pub schoenau: i64,
}
pub fn fetchData() -> Result<String, reqwest::Error> {
let result = reqwest::blocking::get(stationURL);
let response = match result {
Ok(res) => res,
Err(err) => return reqwest::Error(err),
};
let body = serde_json::from_str(&text);
let json = match body {
Ok(json) => json,
Err(err) => return reqwest::Error(err),
};
let date = json["id"].to_string();
Ok(date)
} }