Last active
March 24, 2020 20:50
-
-
Save Gabrielcarvfer/c4a6195a09c85a5a332275c4e7e0558e to your computer and use it in GitHub Desktop.
Fetch COVID-19 cases from the G1 website
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
from datetime import datetime | |
import csv | |
import sys | |
#G1 source https://especiais.g1.globo.com/bemestar/coronavirus/mapa-coronavirus/ | |
urlBrazilMap = "https://especiais.g1.globo.com/bemestar/coronavirus/mapa-coronavirus/data/brazil-map.json" | |
urlCases = "https://especiais.g1.globo.com/bemestar/coronavirus/mapa-coronavirus/data/brazil-cases.json" | |
mapData = json.loads(requests.get(urlBrazilMap).content) | |
mapDataDict = {} | |
for cityEntry in mapData["objects"]["BR_LEVE"]["geometries"]: | |
mapDataDict[int(cityEntry["properties"]["id"])] = tuple(map(float, cityEntry["properties"]["centroide"][1:-1].split(','))) | |
del mapData, cityEntry | |
geo = { | |
"AC": [-8.77, -70.55], | |
"AL": [-9.62, -36.82], | |
"AM": [-3.47, -65.10], | |
"AP": [1.41, -51.77], | |
"BA": [-13.29, -41.71], | |
"CE": [-5.20, -39.53], | |
"DF": [-15.83, -47.86], | |
"ES": [-19.19, -40.34], | |
"GO": [-15.98, -49.86], | |
"MA": [-5.42, -45.44], | |
"MT": [-12.64, -55.42], | |
"MS": [-20.51, -54.54], | |
"MG": [-18.10, -44.38], | |
"PA": [-3.79, -52.48], | |
"PB": [-7.28, -36.72], | |
"PR": [-24.89, -51.55], | |
"PE": [-8.38, -37.86], | |
"PI": [-6.60, -42.28], | |
"RJ": [-22.25, -42.66], | |
"RN": [-5.81, -36.59], | |
"RO": [-10.83, -63.34], | |
"RS": [-30.17, -53.50], | |
"RR": [1.99, -61.33], | |
"SC": [-27.45, -50.95], | |
"SE": [-10.57, -37.45], | |
"SP": [-22.19, -48.79], | |
"TO": [-9.46, -48.26] | |
} | |
response = requests.get(urlCases) | |
if response.ok: | |
#Fetch json | |
data = json.loads(response.content) | |
#Remove older entries | |
cities = {} | |
for cityData in data["docs"]: | |
cityStateTuple = (cityData["state"], cityData["city_name"]) | |
if cityStateTuple not in cities: | |
cities[cityStateTuple] = cityData | |
else: | |
if cityData["date"] > cities[cityStateTuple]["date"]: | |
cities[cityStateTuple] = cityData | |
#Sort by state | |
sortedCityKeys = sorted(cities) | |
w = csv.writer(sys.stdout) | |
w.writerow(["City, State", "Country", "Last Update", "Confirmed", "Deaths", "Recovered", "Latitude", "Longitude"]) | |
for cityKey in sortedCityKeys: | |
#Format date | |
formattedDate = datetime.strptime(cities[cityKey]["date"], '%Y-%m-%d') | |
formattedDate = formattedDate.strftime("%Y-%m-%d %H:%M:%S") | |
#If the city has not been informed, use the state coordinates | |
if cityKey[1] not in ["Não informado"]: | |
cityCoordinates = mapDataDict[cities[cityKey]["city_cod"]] | |
else: | |
cityCoordinates = (geo[cityKey[0]][1], geo[cityKey[0]][0]) | |
#Write to the output | |
w.writerow([cities[cityKey]["city_name"], #City | |
cities[cityKey]["state"], #State | |
"Brazil", #Country | |
formattedDate, #Last Update | |
cities[cityKey]["count"], #Confirmed | |
0, #Deaths | |
0, #Recovered | |
cityCoordinates[1], #Latitude | |
cityCoordinates[0] #Longitude | |
]) | |
#print("%f,%f" % (cityCoordinates[1], cityCoordinates[0])) # tested on http://www.hamstermap.com/quickmap.php | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment