import time

import requests
import sqlite3
from datetime import datetime

def get_dmarket_data():
    """从DMarket API获取商品数据"""
    url = "https://api.dmarket.com/exchange/v1/market/items"
    headers = {
        "Accept": "application/json, text/plain, */*",
        "Language": "ZH",
        "Referer": "https://dmarket.com/",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
        "jkkat": "eafac24b",
        "payment-session-id": "3a1b1e5b-2a62-420b-8064-444495216e98"
    }
    params = {
        "side": "market",
        "orderBy": "updated",
        "orderDir": "desc",
        "priceFrom": 0,
        "priceTo": 0,
        "gameId": "a8db",
        "types": "dmarket",
        "limit": 100,  # 每次获取100条，可根据需要调整
        "currency": "USD",
        "platform": "browser"
    }
    
    response = requests.get(url, headers=headers, params=params)
    return response.json()

def save_to_database(data):
    """保存DMarket数据到数据库"""
    conn = sqlite3.connect('buff_prices.db')
    cursor = conn.cursor()
    current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    
    try:
        items = data.get('objects', [])
        count = 0
        updated_count = 0
        not_found_count = 0
        
        for item in items:
            # 从DMarket数据中获取商品名称和磨损
            item_name = item.get('extra', {}).get('name', '')
            exterior = item.get('extra', {}).get('exterior', '')
            
            if not item_name:
                continue
            
            # 构建market_hash_name格式
            if exterior:
                market_hash_name = f"{item_name} ({exterior.capitalize()})"
            else:
                market_hash_name = item_name
            print(market_hash_name)
            # 在products表中查找匹配的商品
            cursor.execute('SELECT id FROM products WHERE market_hash_name LIKE ?', ('%' + market_hash_name,))
            product = cursor.fetchone()
            
            if product:
                product_id = product[0]
                
                # DMarket的price.USD单位是美分，转换为美元
                price_usd = item.get('price', {}).get('USD', '')
                if price_usd:
                    min_price = float(price_usd) / 100
                else:
                    continue
                
                # 插入或更新DMarket平台价格
                cursor.execute('''
                    INSERT INTO platform_prices (product_id, platform_code, min_price, recorded_at, currency)
                    VALUES (?, ?, ?, ?, 'USD')
                    ON CONFLICT(product_id, platform_code) DO UPDATE SET
                        min_price = excluded.min_price,
                        recorded_at = excluded.recorded_at
                ''', (product_id, 'dmarket', min_price, current_time))
                
                updated_count += 1
            else:
                not_found_count += 1
            
            count += 1
        
        conn.commit()
        print(f"处理了 {count} 条DMarket商品数据")
        print(f"成功更新 {updated_count} 条价格")
        print(f"未匹配到商品 {not_found_count} 条")
        
    except Exception as e:
        conn.rollback()
        print(f"保存数据出错: {e}")
    finally:
        conn.close()

def main():
    while True:
        response_data = get_dmarket_data()
        
        if response_data and 'objects' in response_data:
            save_to_database(response_data)
        else:
            print("API返回数据异常")
        time.sleep(5)

if __name__ == "__main__":
    main()