#!/usr/bin/env python3
"""
现货价格监控 - 每5分钟报价
"""

import json
import subprocess
import time
from datetime import datetime

SYMBOLS = {
    "btcusdt": "BTC",
    "ethusdt": "ETH",
    "riverusdt": "RIVER",
    "pippinusdt": "PIPPIN"
}
TG_GROUP = "-1003737456065"

def curl_cmd(url):
    result = subprocess.run(["curl", "-s", url], capture_output=True, text=True)
    return result.stdout

def get_price(symbol):
    url = f"https://api.huobi.pro/market/detail/merged?symbol={symbol}"
    data = curl_cmd(url)
    try:
        tick = json.loads(data).get("tick", {})
        return {
            "price": tick.get("close", 0),
            "high": tick.get("high", 0),
            "low": tick.get("low", 0),
            "vol": tick.get("vol", 0)
        }
    except:
        return None

def format_price(price):
    if price >= 1000:
        return f"{price:.2f}"
    elif price >= 1:
        return f"{price:.4f}"
    else:
        return f"{price:.6f}"

def send_telegram(message):
    BOT_TOKEN = "8114165855:AAHC8ZETGjG_5H5vJjCFS4GaFhRvlAyocs4"
    url = f"https://api.telegram.org/bot{BOT_TOKEN}/sendMessage"
    data = {"chat_id": TG_GROUP, "text": message}
    cmd = f'curl -s -X POST "{url}" -H "Content-Type: application/json" -d \'{json.dumps(data)}\''
    subprocess.run(cmd, shell=True)

def main():
    prices = []
    for symbol, name in SYMBOLS.items():
        tick = get_price(symbol)
        if tick:
            prices.append(f"{name}: {format_price(tick['price'])}")
    
    if prices:
        msg = f"📊 {datetime.now().strftime('%H:%M')} 现报价\n" + " | ".join(prices)
        print(msg)
        send_telegram(msg)

if __name__ == "__main__":
    main()
