#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Riwayat warna IG StudioSoft 25 Sep - 3 Okt (dari nama file media) -> untuk pelajari 'ritme'."""
import sys, os, time, datetime
sys.path.insert(0, "/home/mantapman/.hermes/profiles/atun/workspace")
from repliz_client import get_client
from PIL import Image

IG_SS = "6a38ac33c5ff4ce3a3231232"
IMG = "/home/mantapman/.hermes/profiles/atun/workspace/openclaw-img"
WIB = datetime.timezone(datetime.timedelta(hours=7))

c = get_client()
docs, page = [], 1
while page <= 200:
    r = None
    for a in range(6):
        try:
            r = c.get_schedules(page=page, limit=100); break
        except Exception:
            time.sleep(3 + 2 * a)
    if r is None:
        print("fetch gagal"); break
    b = r.get("docs") or r.get("data") or []
    docs.extend(b)
    if len(b) < 100: break
    page += 1

def warna(fn):
    p = os.path.join(IMG, fn)
    if fn.endswith(".mp4"):
        if "putih" in fn: return "PUTIH"
        if "biru" in fn: return "BIRU"
        return "video-lain"
    if not os.path.exists(p): return "?"
    try:
        px = Image.open(p).convert("RGB").getpixel((20, 20))
        return "PUTIH" if sum(px) > 600 else "BIRU"
    except Exception:
        return "?"

rows = []
for d in docs:
    if (d.get("accountId") or (d.get("account") or {}).get("_id")) != IG_SS: continue
    at = d.get("scheduleAt") or ""
    try:
        dt = datetime.datetime.fromisoformat(at.replace("Z", "+00:00")).astimezone(WIB)
    except Exception:
        continue
    if datetime.date(2026, 9, 28) <= dt.date() <= datetime.date(2026, 10, 3):
        rows.append((dt, d))
rows.sort(key=lambda x: x[0])
for dt, d in rows:
    medias = d.get("medias") or []
    names = [m.get("url", "").split("/")[-1] for m in medias]
    w = warna(names[0]) if names else "-"
    print(f"{dt:%d %b %H:%M} {d.get('type'):6} {str(d.get('status')):8} {w:9} {'|'.join(names[:2])[:60]}")
