-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
83 lines (66 loc) · 2.23 KB
/
app.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
78
79
80
81
82
83
import csv
import logging
import requests
from datetime import date
URL = "https://api.sejm.gov.pl/sejm/term10/MP"
class StanScrapper:
def __init__(self):
logging.basicConfig(level=logging.INFO)
self.output_data = []
def get_data(self) -> None:
"""Use API to get all"""
data = requests.get(URL).json()
for mp in data:
name = mp["firstLastName"]
district = f"{mp['districtNum']} {mp['districtName']}"
party = mp["club"]
age = self.get_age(mp["birthDate"])
email = mp["email"]
self.output_data.append((name, district, party, age, email))
def get_age(self, birth_date: str) -> int:
"""Compute politician's age."""
byear, bmonth, bday = [int(x) for x in birth_date.split("-")]
today = date.today()
age = today.year - byear - ((today.month, today.day) < (bmonth, bday))
return age
def write_to_csv(self) -> None:
"""Write gathered output to csv file."""
header = ["name", "district", "party", "age", "email"]
try:
# Create data.csv file if it doesn't exist
open("data/data.csv", "x")
except FileExistsError:
pass
with open("data/data.csv", "w") as f:
writer = csv.writer(f)
writer.writerow(header)
for row in self.output_data:
writer.writerow(row)
logging.info("Data saved successfully to `data/data.csv`.")
def drop_column(column: str) -> None:
"""I had to do it."""
try:
f = open("data/data.csv", "r")
except FileExistsError:
return
else:
data = csv.DictReader(f)
headers = [f for f in data.fieldnames]
if column not in headers:
logging.warn(f"{column} not found in csv file.")
logging.debug(f"Headers found: {headers}")
return
d = []
for row in data:
row.pop(column)
d.append(row)
headers.pop(headers.index(column))
f.close()
with open("data/data.csv", "w") as f:
writer = csv.DictWriter(f, headers)
writer.writeheader()
writer.writerows(d)
if __name__ == "__main__":
s = StanScrapper()
s.get_data()
s.write_to_csv()