import base64,json,requests
WP='https://tslaw.id/wp-json/wp/v2'
UA='Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/120 Safari/537.36'
AUTH='Basic '+base64.b64encode(b'bambangbot:EFKm EYGx VQCP Y9go ncPr uM2Y').decode()
OLD='6281935000624'
NEW='6281909222026'  # +62 819-0922-2026

for pid in [3208, 3317]:
    r=requests.get(f'{WP}/posts/{pid}',headers={'User-Agent':UA},timeout=60)
    if r.status_code!=200:
        print(pid,'fetch err',r.status_code); continue
    html=(r.json().get('content') or {}).get('rendered','')
    n_old=html.count(OLD)
    print(f'== POST {pid}: OLD({OLD}) x{n_old} | NEW({NEW}) x{html.count(NEW)} ==')
    if n_old==0:
        continue
    new_wa_html=html.replace(OLD,NEW)
    up=requests.post(f'{WP}/posts/{pid}',
        headers={'Authorization':AUTH,'User-Agent':UA,'Content-Type':'application/json'},
        json={'content':new_wa_html},timeout=90)
    print('  update:',up.status_code)
    if up.status_code in (200,201):
        v=requests.get(f'{WP}/posts/{pid}',headers={'User-Agent':UA},timeout=60).json()
        vr=v.get('content',{}).get('rendered','')
        print('  VERIFY: OLD x%d | NEW x%d' % (vr.count(OLD), vr.count(NEW)))
    else:
        print('  ERR:',up.text[:300])
