add parsing for incidents
This commit is contained in:
parent
63516bf7e6
commit
19a99f626d
87
src/main.rs
87
src/main.rs
@ -1,49 +1,86 @@
|
|||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use std::{thread, time};
|
use serde_json::Value;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::env;
|
||||||
|
|
||||||
extern crate reqwest;
|
extern crate reqwest;
|
||||||
|
|
||||||
const API_URL: &str = "https://status.robertsspaceindustries.com/";
|
const STATUS_URL: &str =
|
||||||
|
"https://status.robertsspaceindustries.com/static/content/api/v0/systems.en.json";
|
||||||
const STATUS_URL: &str = "https://status.robertsspaceindustries.com/static/content/api/v0/systems.en.json";
|
const INCIDENTS_URL: &str =
|
||||||
const INCIDENTS_URL: &str = "https://status.robertsspaceindustries.com/static/content/api/v0/incidents/timeline.en.json";
|
"https://status.robertsspaceindustries.com/static/content/api/v0/incidents/timeline.en.json";
|
||||||
|
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug)]
|
||||||
struct Status {
|
struct Status {
|
||||||
name: String,
|
name: String,
|
||||||
status: String,
|
status: String,
|
||||||
order: u8,
|
order: u8,
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_incidents() -> Result<(),reqwest::Error> {
|
#[derive(Deserialize, Debug)]
|
||||||
// TODO add parsing for the data this returns
|
struct Incidents {
|
||||||
|
count: u16,
|
||||||
|
days: Vec<HashMap<String, String>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_incidents() -> Result<(), reqwest::Error> {
|
||||||
let res = reqwest::blocking::get(INCIDENTS_URL)?;
|
let res = reqwest::blocking::get(INCIDENTS_URL)?;
|
||||||
let data = res.text()?;
|
let data: Value = res.json()?;
|
||||||
// let pu = &data[1];
|
let mut day;
|
||||||
// let ea = &data[2];
|
for i in 0..5 {
|
||||||
//println!("Persistent Universe: {}\nEA: {}", pu.status, ea.status);
|
day = &data["days"][i];
|
||||||
dbg!(data);
|
let date = day["date"]
|
||||||
|
.as_str()
|
||||||
|
.unwrap()
|
||||||
|
.split("T")
|
||||||
|
.collect::<Vec<&str>>()[0];
|
||||||
|
let incidents = day["incidents"].as_array();
|
||||||
|
println!("Date: {}", date);
|
||||||
|
println!("Number of incidents: {}", day["count"]);
|
||||||
|
for incident in incidents {
|
||||||
|
for inc in incident {
|
||||||
|
println!("Title: {}", inc["title"]);
|
||||||
|
println!("Severity: {}", inc["severity"]);
|
||||||
|
println!("Resolved: {}", inc["resolved"]);
|
||||||
|
println!("Content: {}\n", inc["content"]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
println!("\n");
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_status() -> Result<(),reqwest::Error> {
|
fn get_status() -> Result<(), reqwest::Error> {
|
||||||
let res = reqwest::blocking::get(STATUS_URL)?;
|
let res = reqwest::blocking::get(STATUS_URL)?;
|
||||||
let data = res.json::<Vec<Status>>()?;
|
let data = res.json::<Vec<Status>>()?;
|
||||||
let pu = &data[1];
|
let pu = &data[1];
|
||||||
let ea = &data[2];
|
let ea = &data[2];
|
||||||
println!("Persistent Universe: {}\nEA: {}", pu.status, ea.status);
|
println!("Persistent Universe: {}\nEA: {}", pu.status, ea.status);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn help() {
|
||||||
match get_status(){
|
println!(
|
||||||
Err(e) => println!("{:?}", e),
|
"sc_status conveniently checks the Star Citizen Status API.\n\
|
||||||
_ => ()
|
Call with '-i' to get detailed information about incidents"
|
||||||
}
|
);
|
||||||
// match get_incidents(){
|
}
|
||||||
// Err(e) => println!("{:?}", e),
|
|
||||||
// _ => ()
|
fn main() {
|
||||||
// }
|
let args: Vec<String> = env::args().collect();
|
||||||
|
match args.len() {
|
||||||
|
1 => match get_status() {
|
||||||
|
Err(e) => println!("{:?}", e),
|
||||||
|
_ => (),
|
||||||
|
},
|
||||||
|
2 => match args[1].as_ref() {
|
||||||
|
"-i" => match get_incidents() {
|
||||||
|
Err(e) => println!("{:?}", e),
|
||||||
|
_ => (),
|
||||||
|
},
|
||||||
|
_ => help(),
|
||||||
|
},
|
||||||
|
_ => help(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user