Skip to content

Commit de66c68

Browse files
committed
Create download_data.py
1 parent 0f0e194 commit de66c68

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

COVID19/data/download_data.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
from shutil import rmtree
2+
3+
import requests
4+
import wbdata
5+
from git import Repo
6+
from time import sleep
7+
8+
def delete_directory(path):
9+
"""
10+
Remove directory.
11+
"""
12+
13+
try:
14+
rmtree(path)
15+
except FileNotFoundError:
16+
pass
17+
18+
def download_covid():
19+
"""
20+
Download COVID-19 case data.
21+
"""
22+
23+
url = 'https://github.com/CSSEGISandData/COVID-19'
24+
path = './data/COVID-19'
25+
26+
print('Downloading covid data.')
27+
28+
delete_directory(path=path)
29+
30+
# Clone repo with covid data
31+
Repo.clone_from(url, to_path=path)
32+
33+
def download_countries():
34+
"""
35+
Download countries csv.
36+
"""
37+
38+
url = 'https://datahub.io/JohnSnowLabs/country-and-continent-codes-list/r/country-and-continent-codes-list-csv.csv'
39+
path = './data/datahub'
40+
41+
print('Downloading country data.')
42+
43+
delete_directory(path=path)
44+
45+
req = requests.get( url=url)
46+
content = req.content
47+
with open(f'{path}/countries.csv', 'wb') as csv:
48+
csv.write(content)
49+
50+
def download_world_bank():
51+
"""
52+
Download data from the World Bank
53+
"""
54+
55+
path = './data/world_bank'
56+
57+
delete_directory(path=path)
58+
59+
indicators = [{'NY.GDP.PCAP.PP.CD': f'GDP per capita, PPP (current international $)'},
60+
{'SP.POP.TOTL': f'Population, total'},
61+
{'SP.URB.TOTL.IN.ZS': f'Urban population (% of total population)'},
62+
{'EN.POP.SLUM.UR.ZS': f'Urban population (% of total population)'},
63+
{'SP.RUR.TOTL.ZS': f'Urban population (% of total population)'},
64+
{'SP.DYN.LE00.IN': f'Life expectancy at birth, total (years)'},
65+
{'SH.XPD.CHEX.GD.ZS': f'Current health expenditure (% of GDP)'}]
66+
67+
for indicator in indicators:
68+
69+
file_name = list(indicator.keys())[0]
70+
full_path = f'{path}/{file_name}.csv'
71+
72+
print(f'Downloading {indicator}.')
73+
74+
try:
75+
df = wbdata.get_dataframe(indicator)
76+
df.to_csv(full_path)
77+
sleep(2)
78+
except Exception:
79+
print(f'Download failed for {indicator}')
80+
81+
if __name__ == '__main__':
82+
83+
download_covid()
84+
download_countries()
85+
download_world_bank()

0 commit comments

Comments
 (0)