~humaid/hstatus

c226ad8c0a8c191e1bc222e70839bc5c9f718156 — Humaid AlQassimi 1 year, 11 months ago e6db2ea
Remove COVID-19 stats
2 files changed, 0 insertions(+), 59 deletions(-)

M Cargo.toml
M src/main.rs
M Cargo.toml => Cargo.toml +0 -2
@@ 9,7 9,5 @@ edition = "2018"
[dependencies]
x11 = { version = "2.18.2", features = ["xlib"] }
chrono = "0.4"
cached = "0.12.0"
libc = "0.2.68"
curl = "0.4.28"
json = "0.12.4"

M src/main.rs => src/main.rs +0 -57
@@ 1,14 1,9 @@
#[macro_use]
extern crate cached;
extern crate chrono;
extern crate curl;
extern crate json;
extern crate libc;
extern crate x11;

use cached::TimedCache;
use chrono::prelude::*;
use curl::easy::Easy;
use libc::{c_int, getloadavg};
use std::ffi::CString;
use std::fs::File;


@@ 20,8 15,6 @@ pub struct DesktopStatus {
    disp: *mut x11::xlib::Display,
}

static COVID19_COUNTRY: &str = "ae";

impl DesktopStatus {
    pub fn new() -> Self {
        DesktopStatus {


@@ 51,15 44,6 @@ fn main() {
    loop {
        println!("Updating status");
        let mut stat = String::new();
        // covid19
        match get_covid19_stats() {
            Ok(covid) => {
                stat.push_str(covid.as_str());
                stat.push('|');
            }
            Err(why) => println!("Cannot get COVID19 stats: {}", why),
        }

        // load
        let load_res = get_load();
        match load_res {


@@ 124,47 108,6 @@ fn get_load() -> Result<string::String, &'static str> {
    }
}

cached! {
    GET_COVID19_STATS: TimedCache<(), Result<string::String, &'static str>> = TimedCache::with_lifespan(6 * 3600);
    fn get_covid19_stats() -> Result<string::String, &'static str> = {
        println!("Getting COVID19 stats...");
        let mut data = Vec::new();
        let mut handle = Easy::new();
        match handle.url(&("https://api.covid19api.com/live/country/".to_owned()+COVID19_COUNTRY)) {
            Ok(_) => {
                {
                    let mut transfer = handle.transfer();
                    transfer
                        .write_function(|new_data| {
                            data.extend_from_slice(new_data);
                            Ok(new_data.len())
                        })
                        .unwrap();
                    let res = transfer.perform();
                    if let Err(_) = res {
                        return Err("Error performing curl");
                    }
                }
                match json::parse(&String::from_utf8_lossy(&data).to_string()) {
                    Ok(parsed) => {
                       let latest = parsed.len()-1;
                        Ok(String::from(format!(
                        "CON:{confirm} REC:{recover} DED:{deaths} ACT:{active}",
                        confirm=parsed[latest]["Confirmed"],
                        recover=parsed[latest]["Recovered"],
                        deaths=parsed[latest]["Deaths"],
                        active=parsed[latest]["Active"],
                    )))

                    },
                    Err(_) => Err("Error parsing json"),
                }
            }
            Err(_) => Err("Error in curl URL"),
        }
    }
}

fn get_battery() -> std::io::Result<string::String> {
    let present = read_file("/sys/class/power_supply/BAT0/present")?;
    assert_eq!(present, "1", "Battery not present");