diff options
| author | Alex <90010930+ajschofield@users.noreply.github.com> | 2020-07-27 15:37:55 +0100 |
|---|---|---|
| committer | Alex <90010930+ajschofield@users.noreply.github.com> | 2020-07-27 15:37:55 +0100 |
| commit | 52330e5a31a2ad8adccb5716d2032c2cb8531926 (patch) | |
| tree | bc05e8d0ac0214e849aae3ff55d734ece133a498 | |
| parent | 26fed2bab4b4e6a086ab7b3e652940ef7d3d60ca (diff) | |
| download | covid-tracker-2-52330e5a31a2ad8adccb5716d2032c2cb8531926.tar.gz covid-tracker-2-52330e5a31a2ad8adccb5716d2032c2cb8531926.zip | |
Added requirements.txt and updated main.py
Now, there is a fully functioning progress indicator that uses the yaspin module.
Previously, the script would check if the local data stored was already today's date. However, if the data was updated through the day and the source file was updated, the script would not download the newest version.
I have removed this for simplicity purposes.
I'm not sure if its the conventional method of writing a requirements file, but it serves to show what modules the user may need to install.
| -rw-r--r-- | main.py | 177 | ||||
| -rw-r--r-- | requirements.txt | 6 |
2 files changed, 73 insertions, 110 deletions
@@ -3,61 +3,11 @@ import os from pathlib import Path import pandas as pd import requests -import traceback from datetime import datetime from plotnine import * -import sys -import threading -import itertools +from yaspin import yaspin import time -class Spinner: - - def __init__(self, message, delay=0.1): - self.spinner = itertools.cycle(['-', '/', '|', '\\']) - self.delay = delay - self.busy = False - self.spinner_visible = False - sys.stdout.write(message) - - def write_next(self): - with self._screen_lock: - if not self.spinner_visible: - sys.stdout.write(next(self.spinner)) - self.spinner_visible = True - sys.stdout.flush() - - def remove_spinner(self, cleanup=False): - with self._screen_lock: - if self.spinner_visible: - sys.stdout.write('\b') - self.spinner_visible = False - if cleanup: - sys.stdout.write(' ') # overwrite spinner with blank - sys.stdout.write('\r') # move to next line - sys.stdout.flush() - - def spinner_task(self): - while self.busy: - self.write_next() - time.sleep(self.delay) - self.remove_spinner() - - def __enter__(self): - if sys.stdout.isatty(): - self._screen_lock = threading.Lock() - self.busy = True - self.thread = threading.Thread(target=self.spinner_task) - self.thread.start() - - def __exit__(self, exception, value, tb): - if sys.stdout.isatty(): - self.busy = False - self.remove_spinner(cleanup=True) - else: - sys.stdout.write('\r') - - # VARIABLES AND INITIALISATION url_cases = 'https://coronavirus.data.gov.uk/downloads/csv/coronavirus-cases_latest.csv' url_deaths = 'https://coronavirus.data.gov.uk/downloads/csv/coronavirus-deaths_latest.csv' @@ -68,50 +18,36 @@ def getStats(): path = Path("stats/") path_to_stats = cwd / path - print("[*] Checking if 'stats' folder is present...") - if not os.path.exists(path_to_stats): - print("[*] Does not exist. Creating 'stats' folder...") - os.makedirs(path_to_stats) - else: + with yaspin(text=" Checking if 'stats' folder is present...", color="yellow") as spinner: + time.sleep(1) # time consuming code + + if not os.path.exists(path_to_stats): + spinner.fail("✘") + os.makedirs(path_to_stats) + else: + spinner.ok("✔") + + with yaspin(text=" Downloading COVID-19 cases...", color="yellow") as spinner: + time.sleep(2) try: - unix_datetime_cases = datetime.utcfromtimestamp( - os.path.getmtime(os.path.join( - path_to_stats, "covid-cases.csv"))) - unix_datetime_deaths = datetime.utcfromtimestamp(os.path.getmtime( - os.path.join(path_to_stats, "covid-deaths.csv"))) - print("[@] 'covid-cases.csv' last modified: {}".format( - unix_datetime_cases)) - print("[@] 'covid-deaths.csv' last modified: {}".format( - unix_datetime_deaths)) - - if unix_datetime_cases.date() and unix_datetime_deaths.date() == datetime.today().date(): - print("[@] Local data is up-to-date. Skipping download.") - return None - else: - pass + response = requests.get(url_cases) + with open(os.path.join(path_to_stats, "covid-cases.csv"), 'wb') as f: + f.write(response.content) except Exception: - print("[/] It appears that the data has not been downloaded.") - - print("[*] Attempting to download COVID-19 cases...") - try: - response = requests.get(url_cases) - with open(os.path.join(path_to_stats, "covid-cases.csv"), 'wb') as f: - f.write(response.content) - except Exception: - traceback.print_exc() - print("[/] Failed to download COVID-19 cases.") - else: - print("[@] Successfully downloaded COVID-19 cases!") + spinner.fail("✘") + else: + spinner.ok("✔") - print("[*] Attempting to download COVID-19 deaths...") - try: - response = requests.get(url_deaths) - with open(os.path.join(path_to_stats, "covid-deaths.csv"), 'wb') as f: - f.write(response.content) - except Exception: - print("[/] Failed to download COVID-19 deaths!") - else: - print("[@] Successfully downloaded COVID-19 deaths!") + with yaspin(text=" Downloading COVID-19 deaths...", color="yellow") as spinner: + time.sleep(2) + try: + response = requests.get(url_deaths) + with open(os.path.join(path_to_stats, "covid-deaths.csv"), 'wb') as f: + f.write(response.content) + except Exception: + spinner.fail("✘") + else: + spinner.ok("✔") def csv_parser(): @@ -158,24 +94,45 @@ cwd = os.path.dirname(os.path.realpath(__file__)) path_2 = Path("graphs/") path_to_graphs = cwd / path_2 -print("[*] Checking if 'graphs' folder is present...") -if not os.path.exists(path_to_graphs): - print("[*] Does not exist. Creating 'graphs' folder...") - os.makedirs(path_to_graphs) - - -with Spinner("Creating and saving the daily cases plot... "): - p1 = ggplot(cases, aes(x="Specimen date", y="Daily lab-confirmed cases", group = 1)) + geom_point() + geom_line() + labs(title = "Daily COVID-19 Cases") + scale_x_date(date_breaks = "3 days") + stat_smooth(method='mavg', method_args={'window': 3}, color='cyan', show_legend=True) + stat_smooth(method='mavg', method_args={'window': 7}, color='blue') + theme(axis_text_x=element_text(rotation=45, hjust=1)) - p1.save(filename=('cases_daily_' + str(date_today)),path=path_to_graphs,height=5, width=20, units = 'in', dpi=1000, verbose = False) +with yaspin(text=" Checking if 'graphs' folder is present...", color="yellow") as spinner: + time.sleep(1) + if not os.path.exists(path_to_graphs): + spinner.fail("✘") + os.makedirs(path_to_graphs) + else: + spinner.ok("✔") -with Spinner("Creating and saving the daily change in deaths plot... "): - p2 = ggplot(deaths, aes(x="Reporting date", y="Daily change in deaths", group = 1)) + geom_point() + geom_line() + labs(title = "Daily Change in Deaths") + scale_x_date(date_breaks = "3 days") + stat_smooth(method='mavg', method_args={'window': 3}, color='cyan') + stat_smooth(method='mavg', method_args={'window': 7}, color='blue') + theme(axis_text_x=element_text(rotation=45, hjust=1)) - p2.save(filename = ('deaths_change_daily_' + str(date_today)),path=path_to_graphs,height=5, width=20, units = 'in', dpi=1000, verbose = False) -with Spinner("Creating and saving the cumulative cases plot... "): - p3 = ggplot(cases, aes(x="Specimen date", y="Cumulative lab-confirmed cases", group = 1)) + geom_point() + geom_line() + labs(title = "Cumulative Cases") + scale_x_date(date_breaks = "3 days") + theme(axis_text_x=element_text(rotation=45, hjust=1)) - p3.save(filename = ('cumulative_cases_' + str(date_today)),path=path_to_graphs,height=5, width=20, units = 'in', dpi=1000, verbose = False) +with yaspin(text=" Creating and saving the daily cases plot...", color="yellow") as spinner: + try: + p1 = ggplot(cases, aes(x="Specimen date", y="Daily lab-confirmed cases", group = 1)) + geom_point() + geom_line() + labs(title = "Daily COVID-19 Cases") + scale_x_date(date_breaks = "3 days") + stat_smooth(method='mavg', method_args={'window': 3}, color='cyan', show_legend=True) + stat_smooth(method='mavg', method_args={'window': 7}, color='blue') + theme(axis_text_x=element_text(rotation=45, hjust=1)) + p1.save(filename=('cases_daily_' + str(date_today)),path=path_to_graphs,height=5, width=20, units = 'in', dpi=1000, verbose = False) + except Exception: + spinner.fail("✘") + else: + spinner.ok("✔") -with Spinner("Creating and saving the cumulative deaths plot... "): - p4 = ggplot(deaths, aes(x="Reporting date", y="Cumulative deaths", group = 1)) + geom_point() + geom_line() + labs(title = "Cumulative Deaths") + scale_x_date(date_breaks = "3 days") + theme(axis_text_x=element_text(rotation=45, hjust=1)) - p4.save(filename = ('cumulative_deaths_' + str(date_today)),path=path_to_graphs,height=5, width=20, units = 'in', dpi=1000, verbose = False)
\ No newline at end of file +with yaspin(text=" Creating and saving the daily change in deaths plot... ", color="yellow") as spinner: + try: + p2 = ggplot(deaths, aes(x="Reporting date", y="Daily change in deaths", group = 1)) + geom_point() + geom_line() + labs(title = "Daily Change in Deaths") + scale_x_date(date_breaks = "3 days") + stat_smooth(method='mavg', method_args={'window': 3}, color='cyan') + stat_smooth(method='mavg', method_args={'window': 7}, color='blue') + theme(axis_text_x=element_text(rotation=45, hjust=1)) + p2.save(filename = ('deaths_change_daily_' + str(date_today)),path=path_to_graphs,height=5, width=20, units = 'in', dpi=1000, verbose = False) + except Exception: + spinner.fail("✘") + else: + spinner.ok("✔") +with yaspin(text=" Creating and saving the cumulative cases plot...", color="yellow") as spinner: + try: + p3 = ggplot(cases, aes(x="Specimen date", y="Cumulative lab-confirmed cases", group = 1)) + geom_point() + geom_line() + labs(title = "Cumulative Cases") + scale_x_date(date_breaks = "3 days") + theme(axis_text_x=element_text(rotation=45, hjust=1)) + p3.save(filename = ('cumulative_cases_' + str(date_today)),path=path_to_graphs,height=5, width=20, units = 'in', dpi=1000, verbose = False) + except Exception: + spinner.fail("✘") + else: + spinner.ok("✔") +with yaspin(text=" Creating and saving the cumulative deaths plot...", color="yellow") as spinner: + try: + p4 = ggplot(deaths, aes(x="Reporting date", y="Cumulative deaths", group = 1)) + geom_point() + geom_line() + labs(title = "Cumulative Deaths") + scale_x_date(date_breaks = "3 days") + theme(axis_text_x=element_text(rotation=45, hjust=1)) + p4.save(filename = ('cumulative_deaths_' + str(date_today)),path=path_to_graphs,height=5, width=20, units = 'in', dpi=1000, verbose = False) + except Exception: + spinner.fail("✘") + else: + spinner.ok("✔")
\ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..94e58e7 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,6 @@ +plotnine==0.7.0 +requests==2.24.0 +pandas==1.0.5 +ggplot==0.11.5 +numpy==1.19.0 +yaspin==0.18.0 |
