Skip to content

Commit

Permalink
cache results for 10 minutes
Browse files Browse the repository at this point in the history
  • Loading branch information
Yo'av Moshe committed Dec 30, 2023
1 parent a375d35 commit 40a68d0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wttrbar"
version = "0.7.0"
version = "0.7.1"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
50 changes: 37 additions & 13 deletions main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use core::time;
use std::collections::HashMap;
use std::fs::{metadata, read_to_string, File};
use std::io::Write;
use std::thread;
use std::time::{Duration, SystemTime};

use chrono::prelude::*;
use clap::Parser;
Expand Down Expand Up @@ -127,28 +130,49 @@ fn main() {

let mut data = HashMap::new();

let weather_url = format!(
"https://wttr.in/{}?format=j1",
args.location.unwrap_or(String::new())
);
let location = args.location.unwrap_or(String::new());
let weather_url = format!("https://wttr.in/{}?format=j1", location);
let cachefile = format!("/tmp/wttrbar-{}.json", location);

let mut iterations = 0;
let threshold = 20;

let is_cache_file_recent = if let Ok(metadata) = metadata(&cachefile) {
let ten_minutes_ago = SystemTime::now() - Duration::from_secs(600);
metadata
.modified()
.map_or(false, |mod_time| mod_time > ten_minutes_ago)
} else {
false
};

let client = Client::new();
let weather = loop {
match client.get(&weather_url).send() {
Ok(response) => break response.json::<Value>().unwrap(),
Err(_) => {
iterations += 1;
thread::sleep(time::Duration::from_millis(500 * iterations));

if iterations == threshold {
panic!("No response from endpoint!");
let weather = if is_cache_file_recent {
let json_str = read_to_string(&cachefile).unwrap();
serde_json::from_str::<serde_json::Value>(&json_str).unwrap()
} else {
loop {
match client.get(&weather_url).send() {
Ok(response) => break response.json::<Value>().unwrap(),
Err(_) => {
iterations += 1;
thread::sleep(time::Duration::from_millis(500 * iterations));

if iterations == threshold {
panic!("No response from endpoint!");
}
}
}
}
};

if !is_cache_file_recent {
let mut file = File::create(&cachefile)
.expect(format!("Unable to create cache file at {}", cachefile).as_str());

file.write_all(serde_json::to_string_pretty(&weather).unwrap().as_bytes())
.expect(format!("Unable to write cache file at {}", cachefile).as_str());
}
let current_condition = &weather["current_condition"][0];
let feels_like = if args.fahrenheit {
current_condition["FeelsLikeF"].as_str().unwrap()
Expand Down

0 comments on commit 40a68d0

Please sign in to comment.