From 45ee07ea88539617342ca5ab77cc2dc03ae15015 Mon Sep 17 00:00:00 2001 From: "flyingscorpio@arch-desktop" Date: Wed, 6 May 2020 12:45:59 +0200 Subject: [PATCH] Add dates to graph --- dotfiles/i3/scripts/corona | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/dotfiles/i3/scripts/corona b/dotfiles/i3/scripts/corona index 85bc0d7..771dddc 100755 --- a/dotfiles/i3/scripts/corona +++ b/dotfiles/i3/scripts/corona @@ -5,7 +5,7 @@ Author: flyingscorpio """ -from datetime import datetime +from datetime import datetime, timedelta import json import locale import os @@ -87,18 +87,21 @@ def print_to_output(confirmed: int, today_confirmed_int: int, def generate_graph(data: List[int]) -> None: """Generate a small graph based on the evolution of numbers in the list.""" - X = range(len(data)) - plt.plot(X, [data[i] for i in X]) + plt.plot( + [ + datetime.today().date() - timedelta(days=i) + for i in reversed(range(len(data))) + ], + data + ) plt.show() -def main() -> None: - """Grab the data, select the region, split the values.""" - - corona_file = Path("~/.cache/corona").expanduser() +def is_up_to_date(path: Path) -> bool: + """Stat the downloaded file and return if it is up to date.""" stat_cmd = subprocess.run( - f"stat -c %y {corona_file} | cut -d' ' -f1", + f"stat -c %y {path} | cut -d' ' -f1", capture_output=True, check=False, shell=True, @@ -108,9 +111,16 @@ def main() -> None: current_date = datetime.now() today = f"{current_date.year}-{current_date.month:02d}-{current_date.day:02d}" - if stat_date == today: - data = read_json_from_file(corona_file) - else: + return stat_date == today + + +def main() -> None: + """Grab the data, select the region, split the values.""" + + corona_file = Path("~/.cache/corona").expanduser() + + if not is_up_to_date(corona_file): + print("Downloading data to corona file") curl_data = grab_json_with_curl() write_curl_to_file(curl_data, corona_file)