This commit is contained in:
2025-12-05 08:53:11 +01:00
parent 65c90750dc
commit bc44a65806
5 changed files with 463 additions and 55 deletions

View File

@@ -6,41 +6,15 @@ import requests
from pathlib import Path
from datetime import datetime
from dateutil import parser
import sys
# Force UTF-8 output even under Windows Task Scheduler
import sys
try:
sys.stdout.reconfigure(encoding='utf-8')
sys.stderr.reconfigure(encoding='utf-8')
except AttributeError:
# Python < 3.7 fallback (not needed for you, but safe)
import io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8')
# ================================
# 🛡 SAFE PRINT FOR CP1250 / Emoji
# ================================
def safe_print(text: str):
enc = sys.stdout.encoding or ""
if not enc.lower().startswith("utf"):
# strip emoji + characters outside BMP
text = ''.join(ch for ch in text if ord(ch) < 65536)
try:
print(text)
except UnicodeEncodeError:
# final fallback to ASCII only
text = ''.join(ch for ch in text if ord(ch) < 128)
print(text)
# ================================
# 🔧 CONFIGURATION
# ================================
TOKEN_PATH = Path("token.txt")
CLINIC_SLUG = "mudr-buzalkova"
LIMIT = 300
LIMIT = 500 # batch size / number of records
FULL_DOWNLOAD = False # 🔥 TOGGLE: False = last X, True = ALL batches
DB_CONFIG = {
"host": "192.168.1.76",
@@ -52,7 +26,7 @@ DB_CONFIG = {
"cursorclass": pymysql.cursors.DictCursor,
}
# ⭐ GraphQL query
# ⭐ Query with lastMessage
GRAPHQL_QUERY = r"""
query ClinicRequestList2(
$clinicSlug: String!,
@@ -95,27 +69,23 @@ query ClinicRequestList2(
# ================================
def read_token(path: Path) -> str:
tok = path.read_text(encoding="utf-8").strip()
if tok.startswith("Bearer "):
return tok.split(" ", 1)[1]
return tok
return tok.split(" ", 1)[1] if tok.startswith("Bearer ") else tok
# ================================
# DATETIME PARSER
# DATETIME PARSER (UTC → MySQL)
# ================================
def to_mysql_dt(iso_str):
if not iso_str:
return None
try:
dt = parser.isoparse(iso_str)
dt = dt.astimezone()
dt = parser.isoparse(iso_str) # ISO8601 → aware datetime (UTC)
dt = dt.astimezone() # convert to local timezone
return dt.strftime("%Y-%m-%d %H:%M:%S")
except:
return None
# ================================
# UPSERT
# UPSERT REQUEST
# ================================
def upsert(conn, r):
p = r.get("extendedPatient") or {}
@@ -147,7 +117,7 @@ def upsert(conn, r):
"""
vals = (
r.get("id"),
r["id"],
r.get("displayTitle"),
to_mysql_dt(r.get("createdAt")),
final_updated,
@@ -163,16 +133,15 @@ def upsert(conn, r):
conn.commit()
# ================================
# FETCH LAST 300 DONE REQUESTS
# FETCH DONE REQUESTS (one batch)
# ================================
def fetch_done(headers):
def fetch_done(headers, offset):
vars = {
"clinicSlug": CLINIC_SLUG,
"queueId": None,
"queueAssignment": "ANY",
"pageInfo": {"first": LIMIT, "offset": 0},
"pageInfo": {"first": LIMIT, "offset": offset},
"locale": "cs",
"state": "DONE",
}
@@ -187,8 +156,7 @@ def fetch_done(headers):
r.raise_for_status()
data = r.json()["data"]["requestsResponse"]
return data.get("patientRequests", [])
return data.get("patientRequests", []), data.get("count", 0)
# ================================
# MAIN
@@ -203,18 +171,40 @@ def main():
conn = pymysql.connect(**DB_CONFIG)
safe_print(f"\n=== Downloading last {LIMIT} DONE requests @ {datetime.now():%Y-%m-%d %H:%M:%S} ===")
print(f"\n=== Sync CLOSED requests @ {datetime.now():%Y-%m-%d %H:%M:%S} ===")
requests_list = fetch_done(headers)
safe_print(f"📌 Requests returned: {len(requests_list)}")
offset = 0
total_count = None
total_processed = 0
for r in requests_list:
upsert(conn, r)
while True:
batch, count = fetch_done(headers, offset)
if total_count is None:
total_count = count
print(f"📡 Total DONE in Medevio: {count}")
if not batch:
break
print(f" • Processing batch offset={offset} size={len(batch)}")
for r in batch:
upsert(conn, r)
total_processed += len(batch)
if not FULL_DOWNLOAD:
# process only last LIMIT records
break
# FULL DOWNLOAD → fetch next batch
offset += LIMIT
if offset >= count:
break
conn.close()
safe_print("\n\u2705 DONE - latest closed requests synced.\n")
print(f"\n✅ DONE — {total_processed} requests synced.\n")
# ================================
if __name__ == "__main__":
main()