-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathaggregate.py
62 lines (51 loc) · 2.13 KB
/
aggregate.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
import csv
import glob
import json
import logging
import os
import pandas as pd
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(message)s",
handlers=[
logging.FileHandler("debug.log"),
logging.StreamHandler()
]
)
# Aggregate legislative activity by activity type
def aggregate_json(data, activityType):
aggregated = []
try:
for representative in data:
for activity in representative["Activity"][activityType]:
activity["Representative"] = representative["Name"]
aggregated.append(activity)
except:
logging.error("Failed to aggregate {} activities".format(activityType))
return aggregated
# Build CSVs with legislative activity of every representative aggregated by activity type
def build_csvs():
directory_path = './json'
json_files = glob.glob(os.path.join(directory_path, '**/*.json'), recursive=True)
try:
for json_file in json_files:
with open(json_file, 'r') as file:
data = json.load(file)
for activityType in ["Debates", "Questions", "Private Member Bills"]:
dataFrame = pd.DataFrame(aggregate_json(data, activityType))
# Convert first column of dataFrame to datetime format
try:
dataFrame.iloc[:, 0] = pd.to_datetime(dataFrame.iloc[:, 0], format="%d.%m.%Y")
except:
dataFrame.iloc[:, 0] = pd.to_datetime(dataFrame.iloc[:, 0], format="%Y-%m-%d")
csv_file_directory = os.path.join("activity", "{}".format(activityType), "{}".format(json_file.split("/")[2]))
os.makedirs(csv_file_directory, exist_ok=True)
csv_file_path = os.path.join(csv_file_directory, json_file.split("/")[-1].replace("json", "csv"))
dataFrame.to_csv(csv_file_path, index=False, sep=";", quoting=csv.QUOTE_ALL)
except:
logging.error("Failed to process {}".format(json_file))
raise
def aggregate():
build_csvs()
if __name__ == "__main__":
aggregate()