-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
74 lines (61 loc) · 2.08 KB
/
main.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
import json
import requests
from scraper import ECDCScaper, WikiScraper
from bs4 import BeautifulSoup
from flask import Flask, request, jsonify
# Scraper
ecdc = ECDCScaper()
wiki = WikiScraper()
# Flask
app = Flask(__name__)
@app.route('/covid', methods=['GET', 'POST'])
def handle_request():
""" Request handler """
url = 'https://opendata.ecdc.europa.eu/covid19/casedistribution/json/'
request_json = request.get_json(force=True)
data = ecdc.load_data(url=url)
if 'type' in request_json:
type = request_json['type']
else:
type = 'list'
if 'country' in request_json:
country = request_json['country']
if country == 'All':
payload = data.to_dict(type)
else:
payload = data[data.country == country].to_dict(type)
code = 200
elif 'code' in request_json:
code = request_json['code']
if code == 'ALL':
payload = data.to_dict(type)
else:
payload = data[data.code == code].to_dict(type)
code = 200
else:
payload = {'Error: please provide "country" or "code" in your payload.'}
code = 400
return json.dumps(payload), code
@app.route('/covid/de', methods=['GET', 'POST'])
def handle_request_de():
""" Request handler """
url = "https://de.wikipedia.org/wiki/COVID-19-Pandemie_in_Deutschland/Statistik"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'lxml')
# Extract data from website
table_header = wiki.extract_header(soup)
table_cases = wiki.extract_cases(soup)
table_deaths = wiki.extract_deaths(soup)
# Get data
names = wiki.get_names(table_header)
cases = wiki.get_data(table_cases, names)
deaths = wiki.get_data(table_deaths, names)
# Data prep
df_cases = wiki.prepare_data(cases, variable='cases')
df_deaths = wiki.prepare_data(deaths, variable='deaths')
df_payload = wiki.join_data(df_cases, df_deaths)
# Payload
payload = df_payload.to_dict('list')
return jsonify(payload), 200
if __name__ == '__main__':
app.run(host='0.0.0.0', port=80)