z230
This commit is contained in:
@@ -153,6 +153,61 @@ def import_study(study):
|
||||
return imported
|
||||
|
||||
|
||||
def import_from_dir(incoming_dir, done_dir, studies=None):
|
||||
"""
|
||||
Plochý layout: *.json + odpovídající *.pdf leží v incoming_dir.
|
||||
Study se bere z meta JSON. Po importu se pár přesune do done_dir.
|
||||
"""
|
||||
studies = set(studies) if studies else None
|
||||
if not os.path.isdir(incoming_dir):
|
||||
print(f" Incoming neexistuje: {incoming_dir}")
|
||||
return 0
|
||||
os.makedirs(done_dir, exist_ok=True)
|
||||
|
||||
import glob
|
||||
json_paths = sorted(
|
||||
glob.glob(os.path.join(incoming_dir, "*.json")),
|
||||
key=os.path.getmtime,
|
||||
)
|
||||
if not json_paths:
|
||||
print(" [notifikace] zadne nove (Incoming prazdny)")
|
||||
return 0
|
||||
|
||||
db = get_db()
|
||||
coll = db.iwrs_notifications
|
||||
imported = 0
|
||||
skipped = 0
|
||||
failed = 0
|
||||
for json_path in json_paths:
|
||||
try:
|
||||
with open(json_path, "r", encoding="utf-8") as f:
|
||||
meta = json.load(f)
|
||||
if studies and meta.get("study") not in studies:
|
||||
skipped += 1
|
||||
continue
|
||||
pdf_path = json_path[:-5] + ".pdf"
|
||||
pdf_bytes = None
|
||||
if os.path.exists(pdf_path):
|
||||
with open(pdf_path, "rb") as f:
|
||||
pdf_bytes = f.read()
|
||||
if not meta.get("pk"):
|
||||
print(f" CHYBI pk: {os.path.basename(json_path)}")
|
||||
failed += 1
|
||||
continue
|
||||
doc = build_document(meta, pdf_bytes)
|
||||
doc["last_imported_at"] = datetime.datetime.now()
|
||||
coll.replace_one({"_id": doc["_id"]}, doc, upsert=True)
|
||||
imported += 1
|
||||
shutil.move(json_path, os.path.join(done_dir, os.path.basename(json_path)))
|
||||
if os.path.exists(pdf_path):
|
||||
shutil.move(pdf_path, os.path.join(done_dir, os.path.basename(pdf_path)))
|
||||
except Exception as e:
|
||||
print(f" CHYBA {os.path.basename(json_path)}: {e}")
|
||||
failed += 1
|
||||
print(f" [notifikace] importovano={imported} preskoceno={skipped} selhalo={failed}")
|
||||
return imported
|
||||
|
||||
|
||||
def main(studies=None):
|
||||
studies = studies or STUDIES
|
||||
for s in studies:
|
||||
|
||||
Reference in New Issue
Block a user