132 lines
5.2 KiB
Python
132 lines
5.2 KiB
Python
"""Exploration script — V0 Implantation Visit forms (formtype=10) in EvaMed DRY study."""
|
|
|
|
import asyncio
|
|
import json
|
|
from pathlib import Path
|
|
|
|
from playwright.async_api import async_playwright
|
|
|
|
BASE_URL = "https://prod.evamed.com/etude/soft/index.php"
|
|
LOGIN_URL = f"{BASE_URL}?module=authentification&class=login&client=myopowers-dry"
|
|
LIST_URL = f"{BASE_URL}?module=monitoring&class=formslisting¢er_id=2&formtype=10&l=ALL"
|
|
LOGIN = "vbuzalka"
|
|
PASSWORD = "Vlado9674+"
|
|
|
|
SCREENSHOTS_DIR = Path(__file__).parent / "screenshots_surgery"
|
|
SESSION_FILE = Path(__file__).parent / "session.json"
|
|
|
|
|
|
async def main():
|
|
SCREENSHOTS_DIR.mkdir(exist_ok=True)
|
|
|
|
async with async_playwright() as p:
|
|
browser = await p.chromium.launch(headless=False)
|
|
|
|
if SESSION_FILE.exists():
|
|
context = await browser.new_context(storage_state=str(SESSION_FILE))
|
|
print("Loaded saved session")
|
|
else:
|
|
context = await browser.new_context(viewport={"width": 1400, "height": 900})
|
|
|
|
page = await context.new_page()
|
|
|
|
# Login if needed
|
|
await page.goto(LIST_URL)
|
|
await page.wait_for_load_state("networkidle")
|
|
if "authentification" in page.url:
|
|
print("Logging in...")
|
|
await page.goto(LOGIN_URL)
|
|
await page.wait_for_load_state("networkidle")
|
|
await page.locator("#login").fill(LOGIN)
|
|
await page.locator('input[type="password"]').first.fill(PASSWORD)
|
|
await page.click('input[value="Connection"]')
|
|
await page.wait_for_load_state("networkidle")
|
|
await context.storage_state(path=str(SESSION_FILE))
|
|
print("Session saved")
|
|
await page.goto(LIST_URL)
|
|
await page.wait_for_load_state("networkidle")
|
|
else:
|
|
print("Session valid")
|
|
|
|
await page.screenshot(path=str(SCREENSHOTS_DIR / "01_listing.png"), full_page=False)
|
|
print("Screenshot: listing")
|
|
|
|
# Get all form links from the listing
|
|
form_infos = await page.evaluate("""() => {
|
|
const results = [];
|
|
document.querySelectorAll('a[title="Open form"]').forEach(a => {
|
|
const href = a.getAttribute('href') || '';
|
|
const m = href.match(/id=(\\d+)/);
|
|
if (!m) return;
|
|
const row = a.closest('tr');
|
|
const dirLink = row ? row.querySelector('a[title="Open directory"]') : null;
|
|
const patientCode = dirLink ? dirLink.innerText.trim() : '';
|
|
const cells = row ? Array.from(row.querySelectorAll('td')).map(c => c.innerText.trim()) : [];
|
|
results.push({ formId: m[1], patientCode, cells });
|
|
});
|
|
return results;
|
|
}""")
|
|
|
|
print(f"\nFound {len(form_infos)} Implantation Visit forms")
|
|
for i, info in enumerate(form_infos[:5]):
|
|
print(f" [{i}] form_id={info['formId']} patient={info['patientCode']} cells={info['cells']}")
|
|
|
|
if not form_infos:
|
|
print("NO FORMS FOUND!")
|
|
html = await page.content()
|
|
(SCREENSHOTS_DIR / "01_listing.html").write_text(html, encoding="utf-8")
|
|
await browser.close()
|
|
return
|
|
|
|
# Open the first form
|
|
first = form_infos[0]
|
|
form_url = f"{BASE_URL}?module=dossier&class=file&event=show&id={first['formId']}#fiche"
|
|
print(f"\nOpening form: {form_url}")
|
|
await page.goto(form_url)
|
|
await page.wait_for_load_state("networkidle")
|
|
await page.screenshot(path=str(SCREENSHOTS_DIR / "02_form.png"), full_page=True)
|
|
print("Screenshot: form")
|
|
|
|
# Extract fields using span.label + span.valeur pattern
|
|
fields_label_value = await page.evaluate("""() => {
|
|
const fields = [];
|
|
document.querySelectorAll('.tableauFormulaire span.label').forEach(label => {
|
|
const key = label.innerText.trim();
|
|
const valEl = label.nextElementSibling;
|
|
const value = valEl ? valEl.innerText.trim() : null;
|
|
const valClass = valEl ? valEl.className : '';
|
|
fields.push({ key, value, valueClass: valClass });
|
|
});
|
|
return fields;
|
|
}""")
|
|
|
|
print(f"\n=== Fields (span.label -> span.valeur) : {len(fields_label_value)} ===")
|
|
for f in fields_label_value:
|
|
key = f['key'].encode('ascii', 'replace').decode()
|
|
val = (f['value'] or '').encode('ascii', 'replace').decode()
|
|
print(f" {key:50s} = {val}")
|
|
|
|
# Save full form HTML
|
|
html = await page.content()
|
|
(SCREENSHOTS_DIR / "02_form.html").write_text(html, encoding="utf-8")
|
|
print("\nSaved: full form HTML")
|
|
|
|
# Save extracted data as JSON
|
|
result = {
|
|
"form_id": first['formId'],
|
|
"patient_code": first['patientCode'],
|
|
"listing_cells": first['cells'],
|
|
"fields": fields_label_value,
|
|
}
|
|
(SCREENSHOTS_DIR / "form_data.json").write_text(
|
|
json.dumps(result, indent=2, ensure_ascii=False), encoding="utf-8"
|
|
)
|
|
print("Saved: form_data.json")
|
|
|
|
await browser.close()
|
|
print("\nDone")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main())
|