import base64,json,re,requests

def read(p): return open(p,encoding='utf-8').read().strip()

def md_to_html(md):
    lines=md.split('\n'); out=[]; in_list=False
    for line in lines:
        s=line.strip()
        if not s:
            if in_list: out.append('</ul>'); in_list=False
            continue
        m=re.match(r'^(#{2,3})\s+(.*)$',s)
        if m:
            if in_list: out.append('</ul>'); in_list=False
            lvl=len(m.group(1)); out.append(f'<h{lvl}>{m.group(2)}</h{lvl}>'); continue
        m=re.match(r'^(\d+\.|-)\s+(.*)$',s)
        if m:
            if not in_list: out.append('<ul>'); in_list=True
            out.append(f'<li>{m.group(2)}</li>'); continue
        if s.startswith('>'):
            if in_list: out.append('</ul>'); in_list=False
            out.append(f'<blockquote>{s.lstrip("> ")}</blockquote>'); continue
        if in_list: out.append('</ul>'); in_list=False
        s=re.sub(r'\*\*(.+?)\*\*', r'<strong>\1</strong>', s)
        out.append(f'<p>{s}</p>')
    if in_list: out.append('</ul>')
    return '\n'.join(out)

def wc(html):
    txt=re.sub(r'<[^>]+>',' ',html)
    return len([w for w in re.split(r'\s+',txt) if re.search(r'[A-Za-z0-9]',w)])

WP='https://www.everybeofficial.com/wp-json/wp/v2'
UA='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/120 Safari/537.36'
AUTH='Basic '+base64.b64encode(b'bambangbot:2Aa0 OwYY NJaL Oc8b 9OCo Owcw').decode()
H={'Authorization':AUTH,'Content-Type':'application/json','User-Agent':UA,'Accept':'application/json'}

for pid,path in [(3152,'/home/mantapman/.openclaw/workspace-ujang/artikel_everybe_cobloong.md'),
                 (3153,'/home/mantapman/.openclaw/workspace-ujang/artikel_everybe_femine.md')]:
    h=md_to_html(read(path))
    r=requests.post(f'{WP}/posts/{pid}',headers=H,json={'content':h},timeout=60)
    print(f'Post {pid}: words={wc(h)} status={r.status_code}')
    if r.status_code not in (200,201):
        print(r.text[:400])
