-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.py
318 lines (297 loc) · 12.8 KB
/
init.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
def fetch_periods():
import requests
import json
base_url = "https://oda.ft.dk/api/Periode?$inlinecount=allpages"
outDict = {}
while base_url:
inData = requests.get(base_url).content
jsonData = json.loads(inData)
periodData = jsonData["value"]
for entry in periodData:
outDict[entry['id']] = {
'startdate': entry['startdato'].split("T")[0],
'enddate': entry['slutdato'].split("T")[0],
'type': entry['type']
}
try:
base_url = jsonData['odata.nextLink']
except KeyError:
base_url = False
with open("periods.json", "w") as outfile:
outfile.write(json.dumps(outDict))
return
def fetch_group(id,outfile):
print(f"Fetching {outfile}")
import requests
import json
base_url = r"https://oda.ft.dk/api/Aktør?$inlinecount=allpages&$filter=typeid%20eq%20" + str(id)
out_dict = {}
finished = False
counter = 0
while not finished:
print(counter)
counter += 1
in_data = requests.get(base_url).content
json_data = json.loads(in_data)
group_data = json_data["value"]
for group in group_data:
tmp_dict = {}
var_name_list = [
"id",
"name",
"short_name",
"start",
"end",
"periodid"
]
var_list = [
'id',
'navn',
'gruppenavnkort',
'startdato',
'slutdato',
'periodeid'
]
for entry in range(len(var_name_list)):
try:
tmp_dict[var_name_list[entry]] = group[var_list[entry]]
except KeyError:
tmp_dict[var_name_list[entry]] = None
if tmp_dict['start'] != None:
tmp_dict['start'] = tmp_dict['start'].split("T")[0]
if tmp_dict['end'] != None:
tmp_dict['end'] = tmp_dict['end'].split("T")[0]
out_dict[tmp_dict['id']] = tmp_dict
try:
base_url = json_data["odata.nextLink"]
except KeyError:
finished = True
with open(outfile, "w") as f:
f.write(json.dumps(out_dict,indent=4,ensure_ascii=False))
def fetch_groups():
in_dict = {
1: "minister_area.json",
2: "ministertitle.json",
3: "committee.json",
4: "party.json",
5: "person.json",
6: "group.json",
7: "other_group.json",
8: "ministry.json",
9: "commission.json",
10: "organisation.json",
11: "parlamentary.json",
12: "private_citizen.json",
13: "political_network.json",
}
for i in in_dict.keys():
fetch_group(i, in_dict[i])
def fetch_actors():
import requests
import json
base_url = r"https://oda.ft.dk/api/Akt%C3%B8r?$inlinecount=allpages"
finished = False
out_dict = {}
count = 0
while not finished:
print(count)
count += 1
in_data = requests.get(base_url).content
json_data = json.loads(in_data)
for val in json_data["value"]:
out_dict[val["id"]] = {
"name": val["navn"],
"type": val["typeid"],
}
try:
base_url = json_data["odata.nextLink"]
except KeyError:
finished = True
with open("actors.json", "w") as f:
f.write(json.dumps(out_dict,indent=4,ensure_ascii=False))
def fetch_actor_roles():
import requests
import json
base_url = r"https://oda.ft.dk/api/Akt%C3%B8rAkt%C3%B8rRolle?$inlinecount=allpages"
in_data = requests.get(base_url).content
json_data = json.loads(in_data)
out_dict = {}
for role in json_data["value"]:
out_dict[role["id"]] = role["rolle"]
with open("roles.json", "w") as f:
f.write(json.dumps(out_dict,indent=4,ensure_ascii=False))
return out_dict
def fetch_actor_relationships():
import requests
import json
with open("person.json", "r") as f:
person_dict = json.loads(f.read())
with open("roles.json", "r") as f:
role_dict = json.loads(f.read())
with open("periods.json","r") as f:
period_dict = json.loads(f.read())
# out_dict = {}
group_dict = {
1: "Ministerområde",
2: "Ministertitel",
3: "Udvalg",
4: "Folketingsgruppe",
5: "Person",
6: "Gruppe",
7: "Anden gruppe",
8: "Ministerium",
9: "Kommission",
10: "Organisation",
11: "Parlamentarisk Forsamling",
12: "Privat Borger",
13: "Tværpolitisk netværk"
}
file_dict = {
1: "minister_area.json",
2: "ministertitle.json",
3: "committee.json",
4: "party.json",
5: "person.json",
6: "group.json",
7: "other_group.json",
8: "ministry.json",
9: "commission.json",
10: "organisation.json",
11: "parlamentary.json",
12: "private_citizen.json",
13: "political_network.json",
}
with open("actors.json", "r") as f:
actor_dict = json.loads(f.read())
for person_key in person_dict.keys():
person = person_dict[person_key]
print(person["id"])
out_dict = {}
for actortype in ["til", "fra"]:
base_url = f"https://oda.ft.dk/api/Akt%C3%B8rAkt%C3%B8r?$inlinecount=allpages&$filter={actortype}akt%C3%B8rid%20eq%20" + str(person["id"])
finished = False
while not finished:
in_data = requests.get(base_url).content
json_data = json.loads(in_data)
group_data = json_data["value"]
for group in group_data:
tmp_dict = {}
var_list = [
"id",
"fraaktørid",
"tilaktørid",
"startdato",
"slutdato",
"rolleid"
]
var_name_list = [
"id",
"from_actor",
"to_actor",
"start",
"end",
"role"
]
for i in range(len(var_list)):
try:
tmp_dict[var_name_list[i]] = group[var_list[i]]
except KeyError:
tmp_dict[var_name_list[i]] = None
if tmp_dict["role"] != None:
tmp_dict["role"] = role_dict[str(tmp_dict["role"])]
tmp_dict["from_actor_name"] = actor_dict[str(group["fraaktørid"])]["name"]
tmp_dict["to_actor_name"] = actor_dict[str(group["tilaktørid"])]["name"]
if actortype == "fra":
tmp_dict["type"] = group_dict[actor_dict[str(group["tilaktørid"])]["type"]]
if tmp_dict["start"] == None:
with open(str(file_dict[actor_dict[str(group["tilaktørid"])]["type"]]),"r") as tmpfile:
tmp_dict_two = json.loads(tmpfile.read())
tmp_dict_two = tmp_dict_two[f"{group['tilaktørid']}"]
if tmp_dict["end"] == None:
if tmp_dict_two["end"] == None or tmp_dict_two["start"] == None:
tmp_start = period_dict[f"{tmp_dict_two['periodid']}"]["startdate"]
tmp_end = period_dict[f"{tmp_dict_two['periodid']}"]["enddate"]
tmp_dict["start"] = tmp_start
tmp_dict["end"] = tmp_end
else:
tmp_dict["start"] = tmp_dict_two["start"]
tmp_dict["end"] = tmp_dict_two["end"]
else:
if tmp_dict_two["start"] == None:
tmp_start = period_dict[f"{tmp_dict_two['periodid']}"]["startdate"]
tmp_dict["start"] = tmp_start
else:
tmp_dict["start"] = tmp_dict_two["start"]
elif tmp_dict["end"] == None:
tmp_dict["start"] = tmp_dict["start"].split("T")[0]
with open(str(file_dict[actor_dict[str(group["tilaktørid"])]["type"]]),"r") as tmpfile:
tmp_dict_two = json.loads(tmpfile.read())
tmp_dict_two = tmp_dict_two[f"{group['tilaktørid']}"]
if tmp_dict_two["end"] == None:
try:
tmp_end = period_dict[f"{tmp_dict_two['periodid']}"]["enddate"]
tmp_dict["end"] = tmp_end
except KeyError as ke:
with open("errors.log", "a") as errorfile:
errorfile.write(f"KeyError: {str(ke)}\n")
else:
tmp_dict["end"] = tmp_dict_two["end"]
else:
tmp_dict["start"] = tmp_dict["start"].split("T")[0]
tmp_dict["end"] = tmp_dict["end"].split("T")[0]
else:
tmp_dict["type"] = group_dict[actor_dict[str(group["fraaktørid"])]["type"]]
if tmp_dict["start"] == None:
with open(str(file_dict[actor_dict[str(group["fraaktørid"])]["type"]]),"r") as tmpfile:
tmp_dict_two = json.loads(tmpfile.read())
tmp_dict_two = tmp_dict_two[f"{group['fraaktørid']}"]
if tmp_dict["end"] == None:
if tmp_dict_two["end"] == None or tmp_dict_two["start"] == None:
tmp_start = period_dict[f"{tmp_dict_two['periodid']}"]["startdate"]
tmp_end = period_dict[f"{tmp_dict_two['periodid']}"]["enddate"]
tmp_dict["start"] = tmp_start
tmp_dict["end"] = tmp_end
else:
tmp_dict["start"] = tmp_dict_two["start"]
tmp_dict["end"] = tmp_dict_two["end"]
else:
if tmp_dict_two["start"] == None:
tmp_start = period_dict[f"{tmp_dict_two['periodid']}"]["startdate"]
tmp_dict["start"] = tmp_start
else:
tmp_dict["start"] = tmp_dict_two["start"]
elif tmp_dict["end"] == None:
tmp_dict["start"] = tmp_dict["start"].split("T")[0]
with open(str(file_dict[actor_dict[str(group["fraaktørid"])]["type"]]),"r") as tmpfile:
tmp_dict_two = json.loads(tmpfile.read())
tmp_dict_two = tmp_dict_two[f"{group['fraaktørid']}"]
if tmp_dict_two["end"] == None:
tmp_end = period_dict[f"{tmp_dict_two['periodid']}"]["enddate"]
tmp_dict["end"] = tmp_end
else:
tmp_dict["end"] = tmp_dict_two["end"]
else:
tmp_dict["start"] = tmp_dict["start"].split("T")[0]
tmp_dict["end"] = tmp_dict["end"].split("T")[0]
try:
out_dict[group[f"{actortype}aktørid"]].append(tmp_dict)
except KeyError:
out_dict[group[f"{actortype}aktørid"]] = []
out_dict[group[f"{actortype}aktørid"]].append(tmp_dict)
try:
base_url = json_data["odata.nextLink"]
except KeyError:
finished = True
out_id = group[f"{actortype}aktørid"]
if out_dict != {}:
with open(f"relationships/{out_id}.json", "w") as f:
f.write(json.dumps(out_dict,indent=4,ensure_ascii=False))
def main():
fetch_periods()
fetch_groups()
fetch_actor_roles()
fetch_actors()
fetch_actor_relationships()
# fetch_actor_relationships()
if __name__ == '__main__':
main()