set -euo pipefail
python3 - <<'PY2'
from pathlib import Path
import subprocess, os

cfg = Path('/srv/sub2api-80/shared/data/config.yaml')
if not cfg.exists():
    cfg = Path('/home/admin/docker-projects/sub2api/deploy/.env')

text = cfg.read_text()
if cfg.suffix in {'.yaml', '.yml'}:
    in_db = False
    data = {}
    for raw in text.splitlines():
        if not raw.strip() or raw.lstrip().startswith('#'):
            continue
        if not raw.startswith('  '):
            in_db = raw.strip() == 'database:'
            continue
        if in_db and ':' in raw:
            k, v = raw.strip().split(':', 1)
            data[k] = v.strip().strip('"')
    env = os.environ.copy()
    env['PGPASSWORD'] = data['password']
    host = data['host']
    port = data['port']
    user = data['user']
    dbname = data['dbname']
else:
    vals = {}
    for raw in text.splitlines():
        s = raw.strip()
        if not s or s.startswith('#') or '=' not in s:
            continue
        k, v = s.split('=', 1)
        vals[k] = v
    env = os.environ.copy()
    env['PGPASSWORD'] = vals['DATABASE_PASSWORD']
    host = vals['DATABASE_HOST']
    port = vals['DATABASE_PORT']
    user = vals['DATABASE_USER']
    dbname = vals['DATABASE_DBNAME']

res = subprocess.run([
    'psql','-h',host,'-p',str(port),'-U',user,'-d',dbname,'-Atc',
    "select value from settings where key='admin_api_key';"
], env=env, capture_output=True, text=True, check=True)
key = res.stdout.strip()
print(f'ADMIN_KEY_PRESENT={bool(key)} LEN={len(key)}')
get = subprocess.run([
    'curl','--noproxy','*','-sS','-D','-','-o','/tmp/admin_ann_list.json',
    '-H',f'x-api-key: {key}',
    'https://sub-lb.tap365.org/api/v1/admin/announcements?page=1&page_size=3'
], capture_output=True, text=True, check=True)
print(get.stdout)
print('---BODY---')
print(Path('/tmp/admin_ann_list.json').read_text()[:800])
PY2
