# -*- coding: utf-8 -*-
import sys, io, base64, re
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace')
import fdb
conn = fdb.connect(
dsn=r'localhost:c:\medicus 3\data\medicus.fdb',
user='SYSDBA', password='masterkey', charset='win1250'
)
cur = conn.cursor()
# Nacti poslednich 5 PORTAL zaznamu s DATA
cur.execute("""
SELECT FIRST 5 ID, ODESLANO, IDFAK, ID_PODANI, DATA
FROM PORTAL
WHERE DATA IS NOT NULL
ORDER BY ID DESC
""")
rows = cur.fetchall()
for row in rows:
pid, odeslano, idfak, id_podani, data = row
print(f"\n{'='*70}")
print(f"PORTAL.ID={pid} ODESLANO={odeslano} IDFAK={idfak} ID_PODANI={id_podani}")
if data is None:
print("DATA: NULL")
continue
# data je string (win1250)
if isinstance(data, bytes):
data = data.decode('win1250', errors='replace')
# Parsuj XML obalku
print(f"DATA (prvnich 300 zn): {data[:300]}")
# Najdi BASE64 obsah uvnitr ...
m = re.search(r']*Format="BASE64"[^>]*>(.*?)', data, re.DOTALL)
if m:
b64_raw = m.group(1).strip()
# Dekoduj
try:
b64_clean = re.sub(r'\s+', '', b64_raw)
# Padding
b64_clean += '=' * (4 - len(b64_clean) % 4)
html_bytes = base64.b64decode(b64_clean)
html = html_bytes.decode('iso-8859-2', errors='replace')
# Extrahuj text
text = re.sub(r'', '', html, flags=re.DOTALL)
text = re.sub(r'', '', text, flags=re.DOTALL)
text = re.sub(r'', '', text, flags=re.DOTALL)
text = re.sub(r'<[^>]+>', ' ', text)
text = re.sub(r' ', ' ', text)
text = re.sub(r'&', '&', text)
text = re.sub(r'\s+', ' ', text).strip()
print(f"\nDEKÓDOVANÝ HTML protokol:")
print(text[:2000])
except Exception as e:
print(f"Chyba dekódování: {e}")
else:
# Zkus plain text odpoved (ne BASE64)
m2 = re.search(r']*>(.*?)', data, re.DOTALL)
if m2:
print(f"\nObsah Soubor (plain): {m2.group(1)[:500]}")
conn.close()