aboutsummaryrefslogtreecommitdiffstats
path: root/main.py
blob: 8b240f42a3d42deae005a285bd34c795348d734a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import os
from pathlib import Path
import pandas as pd
import requests
from datetime import datetime
from plotnine import *
from yaspin import yaspin
import time

# VARIABLES AND INITIALISATION
global error_count
error_count = 0
url = "https://api.coronavirus.data.gov.uk/v2/data"

def getStats(metric):
  # Define the query parameters
  params = {
    "areaType": "nation",
    "areaName": "England",
    "metric": metric,
    "format": "json"
    }

  response = requests.get(url,params=params)
  if response.status_code == 200:
    data = response.json()

    date_metric = {}

    for item in data["body"]:
      date = item["date"]
      metric_data = item[metric]
      date_metric[date] = metric_data
    
    return date_metric
  
  else:
    print(f"Error: Unable to fetch data. Status code: {response.status_code}")
    return None

path = Path("stats/")

with yaspin(text=" Checking if 'stats' folder is present...", color="yellow") as spinner:
  time.sleep(1)
  if not os.path.exists(path):
      spinner.fail("✘")
      os.makedirs(path)
  else:
      spinner.ok("✔")

with yaspin(text=" Downloading COVID-19 daily cases...", color="yellow") as spinner:
  time.sleep(2)
  try:
    cases = getStats("newCasesBySpecimenDate")
  except Exception:
    spinner.fail("✘")
    error_count += 1
  else:
    spinner.ok("✔")

with yaspin(text=" Downloading COVID-19 daily deaths...", color="yellow") as spinner:
  try:
    deaths = getStats("newDailyNsoDeathsByDeathDate")
  except Exception:
    spinner.fail("✘")
    error_count += 1
  else:
    spinner.ok("✔")

path_2 = Path("graphs/")

with yaspin(text=" Checking if 'graphs' folder is present...", color="yellow") as spinner:
  time.sleep(1)
  if not os.path.exists(path_2):
      spinner.fail("✘")
      os.makedirs(path_2)
  else:
      spinner.ok("✔")

# Create DataFrames for deaths and cases
data_deaths = pd.DataFrame(deaths.items(), columns=["date", "newDailyNsoDeathsByDeathDate"])
data_deaths["date"] = pd.to_datetime(data_deaths["date"])
data_cases = pd.DataFrame(cases.items(), columns=["date", "newCasesBySpecimenDate"])
data_cases["date"] = pd.to_datetime(data_cases["date"])

date_today = datetime.now().strftime('%d-%m-%Y')

# Create the ggplot graph for cases
graph_cases = (
    ggplot(data_cases, aes(x="date", y="newCasesBySpecimenDate"))
    + geom_line()
    + labs(title=f"England COVID-19 Cases by Date", x="Date", y="Cases")
    + theme_minimal()
    + theme(axis_text_x=element_text(rotation=45, hjust=1))
    + scale_x_date(date_breaks = "21 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))
)

# Save the graph to a file
graph_cases.save(filename=('cases_daily_' + str(date_today)),path=path_2,height=6, width=25, units = 'in', dpi=1000, verbose = False)

# Create the ggplot graph for deaths
graph_deaths = (
    ggplot(data_deaths, aes(x="date", y="newDailyNsoDeathsByDeathDate"))
    + geom_line()
    + labs(title=f"England COVID-19 Deaths by Date", x="Date", y="Deaths")
    + theme_minimal()
    + theme(axis_text_x=element_text(rotation=45, hjust=1))
    + scale_x_date(date_breaks = "21 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))
)

# Save the graph to a file
graph_deaths.save(filename=('deaths_daily_' + str(date_today)),path=path_2,height=6, width=25, units = 'in', dpi=1000, verbose = False)
git.ajschof.me — hosted by ajschofield — powered by cgit