-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdbcron.py
77 lines (66 loc) · 2.51 KB
/
dbcron.py
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
import requests
from dbmodels import GlobalStats, CountryStats
import datetime
import config
import time
def global_stats():
now = datetime.datetime.now()
response = requests.get("https://disease.sh/v3/covid-19/all")
data = response.json()
GlobalStats.delete().execute()
GlobalStats.create(
cases=data['cases'],
todayCases=data['todayCases'],
deaths=data['deaths'],
todayDeaths=data['todayDeaths'],
recovered=data['recovered'],
todayRecovered=data['todayRecovered'],
active=data['active'],
updated=convert_updated(data['updated'])
)
print(f'General data updated: {now.strftime("%Y-%m-%d %H:%M:%S")}')
def all_countries():
response = requests.get("https://disease.sh/v3/covid-19/countries?sort=cases")
data = response.json()
CountryStats.delete().execute()
for country in data:
if "'" in country["country"]:
country["country"] = country["country"].replace("'", "''")
CountryStats.create(
country=country["country"],
cases=country["cases"],
todayCases=country["todayCases"],
deaths=country["deaths"],
todayDeaths=country["todayDeaths"],
recovered=country["recovered"],
todayRecovered=country["todayRecovered"],
critical=country["critical"],
active=country["active"],
casesPerOneMillion=country["casesPerOneMillion"],
deathsPerOneMillion=country["deathsPerOneMillion"],
tests=country["tests"],
updated=convert_updated(country["updated"])
)
now = datetime.datetime.now()
print(f'Data for countries updated: {now.strftime("%Y-%m-%d %H:%M:%S")}')
# def vaccines():
# response = requests.get("https://disease.sh/v3/covid-19/vaccine")
# data = response.json()
# VaccineData.delete().execute()
# for vaccinedata in data['data']:
# VaccineData.create(
# candidate=vaccinedata["candidate"],
# mechanism=vaccinedata["mechanism"],
# sponsors=vaccinedata["sponsors"],
# trialPhase=vaccinedata["trialPhase"],
# institutions=vaccinedata["institutions"],
# )
# now = datetime.datetime.now()
# print(f'Data for vaccines updated: {now.strftime("%Y-%m-%d %H:%M:%S")}')
def convert_updated(milliseconds):
seconds, _ = divmod(milliseconds, 1000)
timedate = datetime.datetime.fromtimestamp(seconds)
return timedate
global_stats()
time.sleep(2)
all_countries()