Skip to content

Commit 27caeaa

Browse files
Update covid.py
1 parent f3f4db6 commit 27caeaa

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

consuming-apis-python/covid.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
1-
from datetime import date, timedelta
21
import requests
32

3+
# The datetime package is useful to manipulate dates.
4+
from datetime import date, timedelta
5+
6+
# To fetch the latest total confirmed cases number you need to pass a 24h window to the API.
7+
# That's why you need to pass both yesterday's date and today's.
48
today = date.today()
59
yesterday = today - timedelta(days=1)
10+
11+
# Pick a country slug from the list: https://api.covid19api.com/countries
612
country = "germany"
7-
endpoint = "https://api.covid19api.com/country/%s/status/confirmed" % country
13+
14+
endpoint = f"https://api.covid19api.com/country/{country}/status/confirmed"
815
params = {"from": str(yesterday), "to": str(today)}
916

17+
# The response will be a list of results per day, in your case only 1 result.
1018
response = requests.get(endpoint, params=params).json()
19+
20+
# Finally, you need to traverse through the response and increment the `total_confirmed` variable
21+
# with the total number of confirmed cases available that day, which is in the field `Cases`.
1122
total_confirmed = 0
12-
for region in response:
13-
cases = region.get("Cases", 0)
23+
for day in response:
24+
cases = day.get("Cases", 0)
1425
total_confirmed += cases
1526

16-
print("Total Confirmed COVID cases in %s: %s" % (country, total_confirmed))
27+
print(f"Total Confirmed Covid-19 cases in {country}: {total_confirmed}")

0 commit comments

Comments
 (0)