Skip to content

Commit

Permalink
[psp] copyfileobj misto urlretrieve
Browse files Browse the repository at this point in the history
  • Loading branch information
kokes committed Mar 26, 2024
1 parent 273716d commit e60f64f
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions data/psp/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,28 @@
import logging
import multiprocessing
import os
import shutil
import zipfile
from contextlib import contextmanager
from datetime import datetime
from io import TextIOWrapper
from tempfile import TemporaryDirectory
from urllib.request import urlretrieve
from urllib.request import urlopen

NULL_DATE = datetime(1900, 1, 1, 0, 0)
HTTP_TIMEOUT = 90


@contextmanager
def read_compressed(zipname, filename):
burl = "http://www.psp.cz/eknih/cdrom/opendata/{}"
with TemporaryDirectory() as tdir:
tfn = os.path.join(tdir, "tmp.zip")
urlretrieve(burl.format(zipname), tfn)
with open(tfn, "wb") as f, urlopen(
burl.format(zipname), timeout=HTTP_TIMEOUT
) as u:
shutil.copyfileobj(u, f)

with zipfile.ZipFile(tfn) as zf, zf.open(filename) as zfh:
# tisky.unl maj encoding chyby
yield TextIOWrapper(zfh, "cp1250", errors="ignore")
Expand Down

0 comments on commit e60f64f

Please sign in to comment.