#!/usr/bin/env python3
"""Jejak PT Sampoerna Jaya Baja: Bing, Google Maps (rincian tempat), LinkedIn, dan Instagram."""
import asyncio, re


async def jalan():
    from playwright.async_api import async_playwright
    async with async_playwright() as pw:
        br = await pw.chromium.launch()
        ctx = await br.new_context(user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0 Safari/537.36",
                                   locale="id-ID", viewport={"width": 1366, "height": 900})
        hal = await ctx.new_page()

        print("=== BING ===")
        for q in ('"Sampoerna Jaya Baja"', 'PT Sampoerna Jaya Baja Bekasi baja'):
            try:
                await hal.goto("https://www.bing.com/search?hl=id&setlang=id&q=" + q.replace(" ", "+").replace('"', "%22"),
                               wait_until="domcontentloaded", timeout=90000)
                await hal.wait_for_timeout(4000)
                blok = await hal.query_selector_all("li.b_algo")
                print(f"\n[{q}] blok hasil: {len(blok)}")
                for b in blok[:8]:
                    teks = (await b.inner_text()).replace("\n", " | ")[:300]
                    a = await b.query_selector("h2 a")
                    href = await a.get_attribute("href") if a else ""
                    print(f"   » {teks}")
                    if href:
                        print(f"     {href[:130]}")
            except Exception as e:
                print("   gagal:", str(e)[:100])

        print("\n\n=== GOOGLE MAPS: rincian tempat ===")
        try:
            await hal.goto("https://www.google.com/maps/search/" + "Sampoerna+Jaya+Baja+Bekasi", wait_until="domcontentloaded", timeout=90000)
            await hal.wait_for_timeout(8000)
            judul = await hal.title()
            print("   judul halaman:", judul[:100])
            teks = await hal.inner_text("body")
            teks = re.sub(r"\s+", " ", teks)
            for pola in (r"PT Sampoerna Jaya Baja[^|]{0,120}", r"Jl[^|]{0,90}Narogong[^|]{0,90}",
                         r"\(021\)[^|]{0,20}", r"\b\d,\d\b[^|]{0,40}", r"Buka[^|]{0,40}|Tutup[^|]{0,40}",
                         r"\d+ ulasan[^|]{0,40}"):
                hasil = re.findall(pola, teks, re.I)[:3]
                if hasil:
                    print(f"   {pola[:28]:<30} {hasil}")
        except Exception as e:
            print("   gagal:", str(e)[:100])

        print("\n\n=== LINKEDIN / INSTAGRAM / FACEBOOK ===")
        for nama, u in (("LinkedIn", "https://www.linkedin.com/search/results/companies/?keywords=Sampoerna%20Jaya%20Baja"),
                        ("Instagram", "https://www.instagram.com/explore/search/keyword/?q=sampoernajayabaja"),
                        ("Facebook", "https://www.facebook.com/search/top?q=Sampoerna%20Jaya%20Baja")):
            try:
                await hal.goto(u, wait_until="domcontentloaded", timeout=70000)
                await hal.wait_for_timeout(4000)
                teks = re.sub(r"\s+", " ", await hal.inner_text("body"))[:500]
                print(f"\n   {nama}: {teks[:400]}")
            except Exception as e:
                print(f"\n   {nama}: gagal {str(e)[:90]}")

        await br.close()


asyncio.run(jalan())
