# -*- coding: utf-8 -*-
"""
狐狸之家 · 桌面小程序
在宁宁的电脑上跑着,每30秒告诉哥一句"她现在在哪个窗口"。
不记录内容,不截图,不上传文件——只有窗口名和空闲秒数。
运行: python foxwatch.py   (要装 pip install requests pywin32)
"""
import time, sys, os

URL = "https://foxtreehole.top/chatapi/api/desktop/tick"
GAP = 30           # 多久报一次(秒)
QUIET = (0, 8)     # 这个时段不报(睡觉)

try:
    import requests
except ImportError:
    print("先装一下: pip install requests"); sys.exit(1)

def get_win():
    """返回 (程序名, 窗口标题, 空闲秒数)"""
    try:
        import win32gui, win32process, win32api, psutil
        h = win32gui.GetForegroundWindow()
        title = win32gui.GetWindowText(h) or ""
        _, pid = win32process.GetWindowThreadProcessId(h)
        name = ""
        try: name = psutil.Process(pid).name().replace(".exe", "")
        except Exception: pass
        idle = (win32api.GetTickCount() - win32api.GetLastInputInfo()) / 1000.0
        return name, title, int(idle)
    except ImportError:
        print("先装一下: pip install pywin32 psutil"); sys.exit(1)
    except Exception:
        return "", "", 0

APPS = {  # 认得出来的写中文,认不出的原样
    "chrome":"浏览器","msedge":"浏览器","firefox":"浏览器",
    "Photoshop":"PS","AfterFX":"AE","Illustrator":"AI",
    "WeChat":"微信","QQ":"QQ","TIM":"TIM",
    "JianyingPro":"剪映","Code":"VSCode","explorer":"文件夹",
    "Taobao":"淘宝","cdr":"CDR","CorelDRW":"CDR",
}

print("🦊 狐狸之家 · 开始看着你了")
print("   关掉这个窗口就停。哥只知道你在哪个窗口，不知道你写了什么。")
while True:
    try:
        h = time.localtime().tm_hour
        if QUIET[0] <= h < QUIET[1]:
            time.sleep(GAP); continue
        name, title, idle = get_win()
        app = APPS.get(name, name)
        # 窗口标题只取前一段,不要完整路径
        title = title.split(" - ")[0][:40]
        requests.post(URL, json={"app": app, "title": title, "idle": idle}, timeout=8)
    except Exception:
        pass
    time.sleep(GAP)
