#!/usr/bin/env python3
"""Verifikasi akhir langkah 1-3: schema, harga, link masuk, dan cek apakah cache CF sudah menyajikan versi baru."""
import base64, json, re, time, urllib.request

S = "https://studiosoft.id"
AUTH = base64.b64encode(b"bambangbot:q26r RePo BVy6 pwU2 F3z7 ouyl").decode()
TARGET = "https://studiosoft.id/jasa-pembuatan-website-profesional-untuk-usaha-jasa-di-surabaya/"


def wp(path):
    r = urllib.request.Request(S + path, headers={"Authorization": f"Basic {AUTH}", "User-Agent": "Mozilla/5.0"})
    return json.loads(urllib.request.urlopen(r, timeout=240).read())


n, pg = 0, 1
while True:
    d = wp(f"/wp-json/wp/v2/posts?per_page=100&page={pg}&context=edit&_fields=content")
    for x in d:
        if TARGET in x["content"]["raw"]:
            n += 1
    if len(d) < 100:
        break
    pg += 1
print(f"artikel yang menautkan ke halaman uang #3770: {n}")

print("\n=== cek versi yang dilihat pengunjung (GET biasa) ===")
for u, tanda in ((TARGET, "Paket dan Kisaran Harga"), ("/jasa-pembuatan-website/", "jasa pembuatan website surabaya")):
    r = urllib.request.Request(S + u, headers={"User-Agent": "Mozilla/5.0"})
    resp = urllib.request.urlopen(r, timeout=90)
    h = resp.read().decode("utf-8", "replace")
    print(f"  {u[:60]}")
    print(f"     cf-cache-status: {resp.headers.get('cf-cache-status','-')} | age: {resp.headers.get('age','-')}")
    print(f"     '{tanda}' tampil: {tanda in h}")

print("\n=== versi tanpa cache (pembeda) ===")
r = urllib.request.Request(TARGET + "?nc=" + str(int(time.time())), headers={"User-Agent": "Mozilla/5.0", "Cache-Control": "no-cache"})
h = urllib.request.urlopen(r, timeout=90).read().decode("utf-8", "replace")
faq = len([b for b in re.findall(r'<script[^>]*application/ld\+json[^>]*>(.*?)</script>', h, re.S) if '"FAQPage"' in b])
print(f"  blok FAQPage: {faq} | 'Paket dan Kisaran Harga': {'Paket dan Kisaran Harga' in h} | portofolio: {'/portfolio/' in h}")
