-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathget_data.py
More file actions
executable file
·103 lines (74 loc) · 2.69 KB
/
get_data.py
File metadata and controls
executable file
·103 lines (74 loc) · 2.69 KB
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
"""
Get COVID-19 data from Spain.
Created by Jesús Jiménez Sánchez.
"""
import re
import json
import logging
import requests
from bs4 import BeautifulSoup
URL_CASES = 'https://www.worldometers.info/coronavirus/country/spain/'
URL_VACCINES = 'https://covid-vacuna.app/data/latest.json'
URL_BOOSTER = 'https://covid.ourworldindata.org/data/owid-covid-data.json'
TOTAL_JSON_POSITION = 21
logging.basicConfig(filename='covid.log', level=logging.DEBUG)
def get_vaccines():
'''
Get data about vaccination in Spain.
Returns
-------
distributed : str
Number of vaccines distributed to Spain.
administered : str
Number of vaccines administered in Spain.
completed : str
Number of second doses administered in Spain.
'''
json_vaccines = requests.get(URL_VACCINES, timeout=60)
with open('latest.json', 'wb') as json_file:
json_file.write(json_vaccines.content)
with open('latest.json', 'r') as json_file:
json_info = json.load(json_file)
distributed = str(json_info[TOTAL_JSON_POSITION]['dosisEntregadas'])
administered = str(json_info[TOTAL_JSON_POSITION]['dosisPrimeraDosis'])
completed = str(json_info[TOTAL_JSON_POSITION]['dosisPautaCompletada'])
return distributed, administered, completed
def get_boosters():
'''
Get data about boooster vaccines in Spain.
Returns
-------
booster_doses : int
Number of booster doses administered in Spain.
'''
json_booster = requests.get(URL_BOOSTER, timeout=60)
i = 0
with open('owid-covid-data.json', 'wb') as booster_file:
booster_file.write(json_booster.content)
with open('owid-covid-data.json', 'r') as json_file:
json_info = json.load(json_file)
while "total_boosters" not in json_info['ESP']['data'][-1 - i]:
i += 1
booster_doses = json_info['ESP']['data'][-1 - i]["total_boosters"]
return booster_doses
def get_cases():
'''
Get data about new cases and deaths in Spain.
Returns
-------
new_cases : str
Number of new cases of COVID-19 in Spain.
new_deaths : str
Number of new deaths due to COVID-19 in Spain.
'''
page_cases = requests.get(URL_CASES, timeout=60)
soup_cases = BeautifulSoup(page_cases.content, 'html.parser')
results_cases = soup_cases.find(id='news_block')
latest_cases = results_cases.find_all('li', class_='news_li')[0]
new_cases_re = r'.*<strong>(.*) new cases</strong> and <strong>(.*) new deaths</strong>.*'
match = re.match(new_cases_re, str(latest_cases))
if match:
new_cases = match.group(1)
new_deaths = match.group(2)
return new_cases.replace(',', '.'), new_deaths
return None