Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[justice] nové objekty #274

Merged
merged 13 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 40 additions & 9 deletions data/justice/main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import contextlib
import csv
import datetime as dt
import functools
import glob
import gzip
import hashlib
import json
import multiprocessing
import os
import re
import shutil
from urllib.parse import urlparse
from urllib.request import urlopen

Expand All @@ -14,6 +18,8 @@

NON_ISO_DATUM = re.compile(r"^(\d{1,2})[\.\-](\d{1,2})[\.\-](\d{4})$")
HTTP_TIMEOUT = 60
CACHE_DIR = "cache"
CACHE_ENABLED = True # TODO(PR): parametrizovat; redefinovat urlopen


def gen_schema(element, parent=None):
Expand Down Expand Up @@ -68,10 +74,31 @@ def uprav_data(row, mapping):
return row


@contextlib.contextmanager
def cached_urlopen(url, timeout=HTTP_TIMEOUT):
shasum = hashlib.sha256(url.encode("utf-8")).hexdigest()
if url.endswith(".gz"):
shasum += ".gz"
fn_path = os.path.join(CACHE_DIR, shasum)
if os.path.isfile(fn_path):
with open(fn_path, "rb") as f:
yield f
return

with urlopen(url, timeout=timeout) as r:
fn_tmp = fn_path + ".tmp"
with open(fn_tmp, "wb") as f:
shutil.copyfileobj(r, f)
os.rename(fn_tmp, fn_path)
with cached_urlopen(url, timeout) as r:
yield r


def nahraj_ds(url):
with urlopen(url, timeout=HTTP_TIMEOUT) as r, gzip.GzipFile(fileobj=r) as f:
et = lxml.etree.iterparse(f)
yield from et
with cached_urlopen(url, timeout=HTTP_TIMEOUT) as r:
with gzip.open(r, "rb") as f:
et = lxml.etree.iterparse(f)
yield from et


def zpracuj_ds(url, schemas, outdir, partial, autogen):
Expand Down Expand Up @@ -208,13 +235,17 @@ def zpracuj_ds(url, schemas, outdir, partial, autogen):


def main(outdir: str, partial: bool = False):
os.makedirs(CACHE_DIR, exist_ok=True)
for file in glob.glob(os.path.join(CACHE_DIR, "*.tmp")):
os.remove(file)

# package_list a package_list_compact se asi lisi - ten nekompaktni endpoint
# nejde filtrovat??? Tak to asi udelame na klientovi
url_pl = "https://dataor.justice.cz/api/3/action/package_list"

r = urlopen(url_pl, timeout=HTTP_TIMEOUT)
data = json.load(r)
assert data["success"]
with cached_urlopen(url_pl, timeout=HTTP_TIMEOUT) as r:
data = json.load(r)
assert data["success"]

dss = [ds for ds in data["result"] if "-full-" in ds]
print(f"celkem {len(dss)} datasetu, ale filtruji jen na ty letosni")
Expand All @@ -229,9 +260,9 @@ def main(outdir: str, partial: bool = False):
if partial and len(urls) > 20:
break
url = f"https://dataor.justice.cz/api/3/action/package_show?id={ds}"
r = urlopen(url, timeout=HTTP_TIMEOUT)
dtp = json.load(r)
assert dtp["success"]
with cached_urlopen(url, timeout=HTTP_TIMEOUT) as r:
dtp = json.load(r)
assert dtp["success"]
ds_url = [
j["url"] for j in dtp["result"]["resources"] if j["url"].endswith(".xml.gz")
]
Expand Down
103 changes: 101 additions & 2 deletions data/justice/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
Column("ico", ForeignKey("subjekty.ico"), nullable=False),
Column("datum_zapis", Date, nullable=False),
Column("datum_vymaz", Date, nullable=True),
Column("text", Text, nullable=True),
Column("text", Text, nullable=False),
),
Table(
"pravni_forma_text",
Expand Down Expand Up @@ -438,7 +438,7 @@
Column("osoba_datum_narozeni", Date, nullable=True),
Column("osoba_titul_pred", Text, nullable=True),
Column("osoba_titul_za", Text, nullable=True),
Column("adresa_stat_nazev", Text, nullable=False),
Column("adresa_stat_nazev", Text, nullable=True),
Column("adresa_ulice", Text, nullable=True),
Column("adresa_psc", Text, nullable=True),
Column("adresa_obec", Text, nullable=True),
Expand All @@ -461,6 +461,105 @@
Column("text_prvni_faze", Text, nullable=True),
Column("text_druha_faze", Text, nullable=True),
),
Table(
"min_kmenove_jmeni",
meta,
Column("ico", ForeignKey("subjekty.ico"), nullable=False),
Column("datum_zapis", Date, nullable=False),
Column("datum_vymaz", Date, nullable=True),
Column("vklad_typ", Text, nullable=False),
Column("vklad", Numeric, nullable=False),
),
Table(
"evidence",
meta,
Column("ico", ForeignKey("subjekty.ico"), nullable=False),
Column("datum_zapis", Date, nullable=False),
Column("datum_vymaz", Date, nullable=True),
Column("evidence", Text, nullable=False),
),
Table(
"zakladatel_sf",
meta,
Column("ico", Integer, nullable=False),
Column("datum_zapis", Date, nullable=False),
Column("datum_vymaz", Date, nullable=True),
Column("udelen_souhlas", Boolean, nullable=False),
Column("osoba_ico", Integer, nullable=True),
Column("osoba_jmeno", Text, nullable=True),
Column("osoba_prijmeni", Text, nullable=True),
Column("osoba_datum_narozeni", Date, nullable=True),
Column("osoba_titul_pred", Text, nullable=True),
Column("osoba_titul_za", Text, nullable=True),
Column("adresa_stat_nazev", Text, nullable=True),
Column("adresa_obec", Text, nullable=True),
Column("adresa_cast_obce", Text, nullable=True),
Column("adresa_ulice", Text, nullable=True),
Column("adresa_cislo_po", Text, nullable=True),
Column("adresa_cislo_or", Text, nullable=True),
Column("adresa_psc", Text, nullable=True),
Column("adresa_okres", Text, nullable=True),
Column("adresa_cislo_ev", Text, nullable=True),
Column("adresa_text", Text, nullable=True),
Column("bydliste_stat_nazev", Text, nullable=True),
Column("bydliste_obec", Text, nullable=True),
Column("bydliste_cast_obce", Text, nullable=True),
Column("bydliste_cislo_po", Text, nullable=True),
Column("bydliste_cislo_or", Text, nullable=True),
Column("bydliste_psc", Text, nullable=True),
Column("bydliste_ulice", Text, nullable=True),
),
Table(
"ucel_fondu",
meta,
Column("ico", Integer, nullable=False),
Column("datum_zapis", Date, nullable=False),
Column("datum_vymaz", Date, nullable=True),
Column("ucel", Text, nullable=False),
),
Table(
"vyzva_pro_veritele",
meta,
Column("ico", Integer, nullable=False),
Column("datum_zapis", Date, nullable=False),
Column("datum_vymaz", Date, nullable=True),
Column("ucel", Text, nullable=True),
# TODO: maj tam bordel, proto text
Column("datum_zverejneni", Text, nullable=False),
Column("zverejneni", Text, nullable=False),
),
Table(
"obmysleny_zpusob",
meta,
Column("ico", Integer, nullable=False),
Column("datum_zapis", Date, nullable=False),
Column("datum_vymaz", Date, nullable=True),
Column("zpusob", Text, nullable=False),
),
Table(
"protektor",
meta,
Column("ico", Integer, nullable=False),
Column("datum_zapis", Date, nullable=False),
Column("datum_vymaz", Date, nullable=True),
Column("udelen_souhlas", Boolean, nullable=False),
Column("osoba_ico", Integer, nullable=True),
Column("osoba_jmeno", Text, nullable=True),
Column("osoba_prijmeni", Text, nullable=True),
Column("osoba_datum_narozeni", Date, nullable=True),
Column("osoba_titul_pred", Text, nullable=True),
Column("osoba_titul_za", Text, nullable=True),
Column("adresa_stat_nazev", Text, nullable=True),
Column("adresa_obec", Text, nullable=True),
Column("adresa_cast_obce", Text, nullable=True),
Column("adresa_ulice", Text, nullable=True),
Column("adresa_cislo_po", Text, nullable=True),
Column("adresa_cislo_or", Text, nullable=True),
Column("adresa_psc", Text, nullable=True),
Column("adresa_okres", Text, nullable=True),
Column("adresa_cislo_ev", Text, nullable=True),
Column("adresa_text", Text, nullable=True),
),
]


Expand Down
Loading
Loading