Skip to content

Commit

Permalink
Pack weather data
Browse files Browse the repository at this point in the history
  • Loading branch information
bim9262 committed Oct 2, 2023
1 parent e510706 commit 2c173cf
Show file tree
Hide file tree
Showing 15 changed files with 102 additions and 23 deletions.
18 changes: 18 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ authors = [
edition = "2021"

[workspace]
members = [".", "xtask"]
members = [".", "xtask", "data"]

[features]
default = ["pulseaudio"]
Expand All @@ -28,6 +28,7 @@ rustdoc-args = ["--cfg", "docsrs"]
async-once-cell = "0.5"
async-trait = "0.1"
backon = "0.4.1"
bincode = "1.3.3"
calibright = { version = "0.1", features = ["watch"] }
chrono = { version = "0.4", default-features = false, features = ["clock", "unstable-locales"] }
chrono-tz = { version = "0.8", features = ["serde"] }
Expand Down Expand Up @@ -64,6 +65,7 @@ unicode-segmentation = "1.10.1"
wayrs-client = { version = "0.12", features = ["tokio"] }
wayrs-protocols = { version = "0.12", features = ["wlr-foreign-toplevel-management-unstable-v1"] }
zbus = { version = "3.14", default-features = false, features = ["tokio"] }
i3status-rs_data = {path = "data"}

[dependencies.tokio]
version = "1.12"
Expand All @@ -82,6 +84,11 @@ features = [
"time",
]

[build-dependencies]
bincode = "1.3.3"
serde_json = "1.0"
i3status-rs_data = {path = "data"}

[profile.release]
lto = "thin"

Expand Down
21 changes: 20 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
use std::process::Command;
use std::{env, fs::File, path::PathBuf, process::Command};

fn main() {
let met_no_legends_store: i3status_rs_data::weather::met_no::LegendsStore =
serde_json::from_str(include_str!("data/met_no_legends.json")).unwrap();

let buffer =
File::create(PathBuf::from(env::var("OUT_DIR").unwrap()).join("met_no_legends.bin"))
.unwrap();

bincode::serialize_into(buffer, &met_no_legends_store).unwrap();

let open_weather_map_cities_store: i3status_rs_data::weather::open_weather_map::CitiesStore =
serde_json::from_str(include_str!("data/open_weather_map_cities.json")).unwrap();

let buffer = File::create(
PathBuf::from(env::var("OUT_DIR").unwrap()).join("open_weather_map_cities.bin"),
)
.unwrap();

bincode::serialize_into(buffer, &open_weather_map_cities_store).unwrap();

let hash = Command::new("git")
.args(["rev-parse", "--short", "HEAD"])
.env("GIT_CONFIG_GLOBAL", "/dev/null")
Expand Down
2 changes: 1 addition & 1 deletion cspell.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ allowCompoundWords: true
ignorePaths:
- target/
- man/
- src/blocks/weather/met_no_legends.json
- data/met_no_legends.json
ignoreRegExpList:
# Ignore usernames that are @'d
- /@[\w-]*/g
Expand Down
8 changes: 8 additions & 0 deletions data/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "i3status-rs_data"
publish = false
version = "0.1.0"
edition = "2021"

[dependencies]
serde = { version = "1.0", features = ["derive", "rc"] }
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions data/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
extern crate serde;

mod prelude;
pub mod weather;
3 changes: 3 additions & 0 deletions data/src/prelude.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub use std::collections::HashMap;

pub use serde::{Deserialize, Serialize};
10 changes: 10 additions & 0 deletions data/src/weather/met_no.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use crate::prelude::*;

pub type LegendsStore = HashMap<String, LegendsResult>;

#[derive(Deserialize, Serialize)]
pub struct LegendsResult {
pub desc_en: String,
pub desc_nb: String,
pub desc_nn: String,
}
2 changes: 2 additions & 0 deletions data/src/weather/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod met_no;
pub mod open_weather_map;
9 changes: 9 additions & 0 deletions data/src/weather/open_weather_map.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use crate::prelude::*;

pub type CitiesStore = HashMap<String, City>;

#[derive(Deserialize, Serialize)]
pub struct City {
pub lat: f64,
pub lon: f64,
}
File renamed without changes.
19 changes: 9 additions & 10 deletions src/blocks/weather/met_no.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;

type LegendsStore = HashMap<String, LegendsResult>;
use i3status_rs_data::weather::met_no::LegendsStore;

#[derive(Deserialize, Debug)]
#[serde(tag = "name", rename_all = "lowercase")]
Expand All @@ -25,13 +25,6 @@ impl<'a> Service<'a> {
}
}

#[derive(Deserialize)]
struct LegendsResult {
desc_en: String,
desc_nb: String,
desc_nn: String,
}

#[derive(Deserialize, Debug, Clone, Default)]
pub(super) enum ApiLanguage {
#[serde(rename = "en")]
Expand Down Expand Up @@ -90,8 +83,14 @@ struct ForecastTimeInstant {
relative_humidity: Option<f64>,
}

static LEGENDS: Lazy<Option<LegendsStore>> =
Lazy::new(|| serde_json::from_str(include_str!("met_no_legends.json")).ok());
static LEGENDS: Lazy<Option<LegendsStore>> = Lazy::new(|| {
bincode::deserialize(include_bytes!(concat!(
env!("OUT_DIR"),
"/",
"met_no_legends.bin"
)))
.ok()
});

const FORECAST_URL: &str = "https://api.met.no/weatherapi/locationforecast/2.0/compact";

Expand Down
20 changes: 10 additions & 10 deletions src/blocks/weather/open_weather_map.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use super::*;
use chrono::Utc;

use i3status_rs_data::weather::open_weather_map::{CitiesStore, City};

pub(super) const URL: &str = "https://api.openweathermap.org/data/2.5/weather";
pub(super) const GEO_URL: &str = "https://api.openweathermap.org/geo/1.0";
pub(super) const API_KEY_ENV: &str = "OPENWEATHERMAP_API_KEY";
Expand Down Expand Up @@ -90,16 +92,14 @@ struct ApiWeather {
description: String,
}

type CitiesStore = HashMap<String, City>;

#[derive(Deserialize, Debug)]
struct City {
lat: f64,
lon: f64,
}

static CITIES: Lazy<Option<CitiesStore>> =
Lazy::new(|| serde_json::from_str(include_str!("open_weather_map_cities.json")).ok());
static CITIES: Lazy<Option<CitiesStore>> = Lazy::new(|| {
bincode::deserialize(include_bytes!(concat!(
env!("OUT_DIR"),
"/",
"open_weather_map_cities.bin"
)))
.ok()
});

static LOCATION_CACHE: Mutex<Option<String>> = Mutex::new(None);

Expand Down

0 comments on commit 2c173cf

Please sign in to comment.