-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport.py
293 lines (252 loc) · 9.64 KB
/
import.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
import os
import sys
from functools import cache
import requests
from alephclient.api import AlephAPI
from alephclient.errors import AlephException
from alephclient.util import backoff
from banal import clean_dict, ensure_dict, ensure_list
from mmmeta import mmmeta
from nomenklatura.dataset.catalog import DataCatalog
from nomenklatura.dataset.dataset import DS, Dataset
from normality import slugify
from servicelayer.archive import init_archive
from structlog import get_logger
log = get_logger("dokukratie")
CATALOG_URL = "https://s3.investigativedata.org/dokukratie/catalog.json"
ARCHIVE_MIME = "application/json+archive"
STATES = (
"bb",
"be",
"bw",
"by",
"hb",
"he",
"hh",
"mv",
"ni",
"nw",
"rp",
"sh",
"sl",
"sn",
"st",
"th",
)
def get_document_type(data: dict) -> str:
dtype = data.get("document_type", "")
if not dtype:
return "Anfrage"
if "minor" in dtype:
return "Kleine Anfrage"
if "major" in dtype:
return "Große Anfrage"
return "Anfrage"
def ensure_str(value: str | None = None) -> str | None:
if not value:
return
return str(value)
def create_meta_object(data: dict, ensure_ref_in_name=False) -> dict:
source_url = data.get("source_url", data.get("url"))
foreign_id = data.get(
"foreign_id", data.get("reference", data.get("request_id", source_url))
)
name = data.get("title") or data.get("file_name")
if name is None and source_url is not None:
name = source_url.split("/")[-1]
if name is not None:
if ensure_ref_in_name:
ref = data.get("reference")
if ref is not None and ref.lower() not in name.lower():
name = f"{ref} - {name}"
return clean_dict(
{
"crawler": "dokukratie",
"foreign_id": foreign_id,
"source_url": source_url,
"title": data.get("title"),
"name": name,
"file_name": name,
"author": data.get("author"),
"publisher": data.get("publisher:name"),
"publisher_url": data.get("publisher:url"),
"retrieved_at": ensure_str(data.get("retrieved_at")),
"modified_at": ensure_str(data.get("modified_at")),
"published_at": ensure_str(data.get("published_at")),
"headers": ensure_dict(data.get("headers")),
"keywords": ensure_list(data.get("keywords")),
"languages": data.get("languages"),
"countries": data.get("countries"),
"mime_type": data.get("mime_type"),
"parent": data.get("parent"),
}
)
def aleph_emit_document(
api: AlephAPI, collection_id: int, data: dict, fp: str, dataset: str
):
label = data.get("file_name", data.get("source_url"))
log.info("Upload: %s", label, dataset=dataset)
for try_number in range(api.retries):
try:
res = api.ingest_upload(collection_id, fp, data)
document_id = res.get("id")
log.info("Aleph document ID: %s", document_id, dataset=dataset)
return
except AlephException as exc:
if try_number > api.retries or not exc.transient:
log.error("Error: %s" % exc)
return
backoff(exc, try_number)
@cache
def make_folder(
api: AlephAPI, collection_id: int, name: str, parent: tuple[int, str] | None = None
) -> str:
foreign_id = slugify(name)
if foreign_id is None:
log.warning("No folder foreign ID!")
return
if parent:
foreign_id = f"{parent[1]}/{foreign_id}"
log.info("Make folder: %s", name, foreign_id=foreign_id)
for try_number in range(api.retries):
try:
res = api.ingest_upload(
collection_id,
metadata=clean_dict(
{
"file_name": name,
"foreign_id": foreign_id,
"parent": {"id": parent[0]} if parent else None,
}
),
sync=True,
)
document_id = res.get("id")
log.info("Aleph folder entity ID: %s", document_id)
return document_id, foreign_id
except AlephException as ae:
if try_number > api.retries or not ae.transient:
log.error("Error: %s" % ae)
return
backoff(ae, try_number)
@cache
def make_folders(api: AlephAPI, collection_id: int, *paths: list[str]) -> int:
parent = None
folder = None
for path in paths:
folder = make_folder(api, collection_id, path, parent)
parent = folder
return folder
def ensure_collection(api: AlephAPI, dataset: DS, frequency="weekly") -> str:
foreign_id = dataset.name
if foreign_id.startswith("de_"):
foreign_id = foreign_id[3:]
if not foreign_id.startswith("de_dokukratie_"):
foreign_id = f"de_dokukratie_{foreign_id}"
aleph_collection = api.get_collection_by_foreign_id(foreign_id)
dataset = dataset.to_dict()
data = {
"label": dataset["title"],
"summary": (
dataset.get("description", "") + "\n\n" + dataset.get("summary", "")
).strip(),
"publisher": dataset.get("publisher", {}).get("name"),
"publisher_url": dataset.get("publisher", {}).get("url"),
"countries": ensure_list(dataset.get("publisher", {}).get("country")),
"data_url": dataset.get("data", {}).get("url"),
"category": dataset.get("category", "library"),
}
if "frequency" in dataset or frequency is not None:
data["frequency"] = dataset.get("frequency", frequency)
if aleph_collection is not None:
log.info("[%s] Updating collection metadata ..." % foreign_id)
# don't overwrite existing (probably user changed) category:
data.pop("category", None)
aleph_collection = api.update_collection(
aleph_collection["collection_id"], data
)
else:
log.info("[%s] Creating collection ..." % foreign_id)
aleph_collection = api.create_collection({**data, **{"foreign_id": foreign_id}})
return aleph_collection["collection_id"]
if __name__ == "__main__":
res = requests.get(CATALOG_URL)
if not res.ok:
raise requests.HTTPError(f"Fetch catalog failed: {res.status_code}")
catalog = DataCatalog(Dataset, res.json())
log.info("Loaded catalog.", url=CATALOG_URL)
api = AlephAPI()
include = sys.argv[1]
for dataset in catalog.datasets:
if include and dataset.name != include:
log.info("Skipping dataset.", dataset=dataset.name)
continue
log.info("Lodaded dataset.", dataset=dataset.name)
storage = None
for resource in dataset.resources:
if resource.mime_type == ARCHIVE_MIME:
res = requests.get(resource.url)
if not res.ok:
raise requests.HTTPError(
f"Fetch resource failed: {res.status_code}"
)
archive_manifest = res.json()
storage = init_archive(**archive_manifest)
if storage is None:
log.warn("No archive storage configured for dataset", dataset=dataset.name)
continue
collection_id = ensure_collection(api, dataset)
log.info("[%s] Loading mmmeta ..." % dataset.name)
m = mmmeta()
m.update()
log.info("mmmeta: %d files" % len(m.files))
for file in m.files:
if file["imported"]:
log.info(
"Skipping already imported file", foreign_id=file["foreign_id"]
)
continue
if (
file["publisher:type"] == "parliament"
and not file["legislative_term"] # noqa
):
log.warn("No legislative term", file=file.serialize())
continue
key = storage._locate_key(file["content_hash"])
if key and key.endswith(".json"):
log.warn("key: %s" % key, dataset=dataset.name)
continue
fp = storage.load_file(file["content_hash"])
if fp is None:
log.warn(
"No file found for content hash: %s" % file["content_hash"],
dataset=dataset.name,
)
continue
if not file["foreign_id"]:
file["foreign_id"] = file["reference"]
if dataset.name in ("sehrgutachten", "de_vsberichte"):
data = clean_dict(create_meta_object(file.serialize()))
if data.get("publisher"):
parent, _ = make_folders(api, collection_id, data["publisher"])
data["parent"] = {"id": parent}
if dataset.name == "de_vsberichte":
if data["publisher"] not in data["file_name"]:
data["file_name"] = f'{data["publisher"]} - {data["file_name"]}'
elif dataset.name in STATES:
data = clean_dict(create_meta_object(file.serialize(), True))
parent, _ = make_folders(
api,
collection_id,
f"{file['legislative_term']}. Wahlperiode",
"Drucksache",
get_document_type(file.serialize()),
)
data["parent"] = {"id": parent}
else:
log.error("Unsupported dataset: %s" % dataset.name)
continue
aleph_emit_document(api, collection_id, data, fp, dataset.name)
file["imported"] = True
file.save()
os.remove(fp)