JSON can be parsed

This commit is contained in:
Ollo 2023-08-19 00:56:54 +02:00
parent a5d9043b07
commit 787fd29df1
2 changed files with 14 additions and 12 deletions

View File

@ -278,7 +278,8 @@ fn main() {
let mut last_data = Option::None; let mut last_data = Option::None;
// Test Webcrawler for public transportataion // Test Webcrawler for public transportataion
straba::fetch_data(); let name = straba::fetch_data();
println!("Name: {:?}", name);
loop { loop {
let delay = time::Duration::from_millis(10000); let delay = time::Duration::from_millis(10000);

View File

@ -1,24 +1,24 @@
/* @file straba.rs /* @file straba.rs
* @brief fetch next depature of light rail vehicle * @brief fetch next depature of light rail vehicle
*/ */
use serde::Deserialize;
use serde_json::Value; use serde_json::Value;
use serde::Deserialize;
const STATION_URL:&str = "https://www.rnv-online.de/rest/departure/2494"; const STATION_URL:&str = "https://www.rnv-online.de/rest/departure/2494";
/* ******************** JSON Description ****************************** */
#[derive(Default, Debug, Clone, PartialEq, Deserialize)] #[derive(Default, Debug, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct Station { pub struct Station {
pub id: i64, pub id: String,
pub name: String, pub name: String,
pub lines: Vec<Line>,
pub journeys: Vec<Journey>, pub journeys: Vec<Journey>,
} }
#[derive(Default, Debug, Clone, PartialEq, Deserialize)] #[derive(Default, Debug, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct Line { pub struct Line {
pub id: i64, pub id: String,
pub text: String, pub text: String,
pub icon_outlined: i64, pub icon_outlined: i64,
pub icon_color: String, pub icon_color: String,
@ -32,20 +32,21 @@ pub struct Journey {
pub destination: String, pub destination: String,
pub barrier_level: String, pub barrier_level: String,
pub loads_forecast_type: String, pub loads_forecast_type: String,
pub realtime_departure: i64, pub realtime_departure: Option<i64>,
pub scheduled_departure: i64, pub scheduled_departure: i64,
pub difference: i64, pub difference: Option<i64>,
pub canceled: bool, pub canceled: bool,
} }
// Return value
pub struct NextDeparture { pub struct NextDeparture {
pub rheinau: i64, pub rheinau: i64,
pub schoenau: i64, pub schoenau: i64,
} }
pub fn fetch_data() -> Option<String> { pub fn fetch_data() -> Option<String> {
let result = reqwest::blocking::get(STATION_URL); let result = reqwest::blocking::get(STATION_URL);
println!("Start Straba Crawler");
if result.is_err() { if result.is_err() {
println!("Could not read station response {:?}", result.err()); println!("Could not read station response {:?}", result.err());
@ -58,7 +59,7 @@ pub fn fetch_data() -> Option<String> {
} }
let body: std::result::Result<Value, serde_json::Error> = serde_json::from_str(&text.unwrap()); let body: std::result::Result<Station, serde_json::Error> = serde_json::from_str(&text.unwrap());
if body.is_err() { if body.is_err() {
println!("Could not parse json {:?}", body.err()); println!("Could not parse json {:?}", body.err());
@ -66,6 +67,6 @@ pub fn fetch_data() -> Option<String> {
} }
let json = body.unwrap(); let json = body.unwrap();
let date = json["id"].to_string(); let tmp = (json.journeys[0].destination).to_string();
Some(date) Some(tmp)
} }