# config/base_config.py
import json
import os
import time
from enum import Enum
import redis
from loguru import logger

def init_redis():
    try:
        redis_client = redis.Redis(
            host='172.16.0.24',
            port=6379,
            db=0,
            password='aiyingli@@123',
            socket_timeout=5,
            decode_responses=True  # 自动解码为字符串，避免bytes类型问题
        )
        return redis_client
    except Exception as e:
        return None

_redis8_client = None
_redis8_pool = None

def init_redis8():
    global _redis8_client, _redis8_pool

    if _redis8_client is not None:
        return _redis8_client

    try:
        _redis8_pool = redis.ConnectionPool(
            host='redis-cnlfmu7rl14awitrz.redis.ivolces.com',
            port=6379,
            db=8,
            password='aiyingli@@123',
            socket_timeout=120,
            socket_connect_timeout=5,
            decode_responses=True,
            retry_on_timeout=True,
            health_check_interval=30,
            socket_keepalive=True,
            max_connections=100,
        )

        _redis8_client = redis.Redis(connection_pool=_redis8_pool)
        _redis8_client.ping()
        return _redis8_client
    except Exception as e:
        logger.error(f"Redis 初始化失败: {e}")
        _redis8_client = None
        _redis8_pool = None
        return None



class PlatformType(Enum):
    DEEPSEEK = "DP"
    DOUBAO = "DB"
    YUANBAO = "TXYB"
    QIANWEN = "TYQW"
    KIMI = "KIMI"
    WENXINYIYAN = "WXYY"
    BAIDUAI = "BDAI"
    DOUYINAI = "DYAI"
    DOUBAOANDROID = "DOUBA"
    DEEPSEEKANDROID = "DPA"
    QIANWENANDROID = "TYQWA"
    YUANBAOANDROID = "TXYBA"
    XIAOHONGSHUANDROID = "XHSA"
    BAIDUBAIKAN = "BK"
    BAIDUWENXIN = "BDWX"


    @classmethod
    def from_str(cls, platform_str: str) -> "PlatformType":
        for member in cls:
            if member.value == platform_str:
                return member
        raise ValueError(f"无效的平台字符串：{platform_str}，可选值：{[m.value for m in cls]}")


class BaseConfig:
    base_url = "http://172.16.1.37:10444/api/v1/"
    PLATFORM_CONFIGS = {
        PlatformType.DEEPSEEK.value: {
            "url": f"{base_url}deepseek",
            "storage_path": lambda tid: f"geo/{tid}/DP/original.text"
        },
        PlatformType.DOUBAO.value: {
            "url": f"{base_url}doubao",
            "storage_path": lambda tid: f"geo/{tid}/DB/original.text"
        },
        PlatformType.YUANBAO.value: {
            "url": f"{base_url}yuanbao",
            "storage_path": lambda tid: f"geo/{tid}/TXYB/original.text"
        },
        PlatformType.QIANWEN.value: {
            "url": f"{base_url}qianwen",
            "storage_path": lambda tid: f"geo/{tid}/TYQW/original.text"
        },
        PlatformType.KIMI.value: {
            "url": f"{base_url}kimi",
            "storage_path": lambda tid: f"geo/{tid}/KIMI/original.text"
        },
        PlatformType.WENXINYIYAN.value: {
            "url": f"{base_url}baiduai",
            "storage_path": lambda tid: f"geo/{tid}/WXYY/original.text"
        },
        PlatformType.BAIDUAI.value: {
            "url": f"{base_url}baikan",
            "storage_path": lambda tid: f"geo/{tid}/BDAI/original.text"
        },
        PlatformType.DOUYINAI.value: {
            "url": f"{base_url}aidouyin",
            "storage_path": lambda tid: f"geo/{tid}/DYAI/original.text"
        },
        PlatformType.DOUBAOANDROID.value: {
            "url": f"{base_url}doubao_android",
            "storage_path": lambda tid: f"geo/{tid}/DOUBA/original.text"
        },
        PlatformType.DEEPSEEKANDROID.value: {
            "url": f"{base_url}deepseek_android",
            "storage_path": lambda tid: f"geo/{tid}/DPA/original.text"
        },
        PlatformType.QIANWENANDROID.value: {
            "url": f"{base_url}qianwen_android",
            "storage_path": lambda tid: f"geo/{tid}/TYQWA/original.text"
        },
        PlatformType.YUANBAOANDROID.value: {
            "url": f"{base_url}yuanbao_android",
            "storage_path": lambda tid: f"geo/{tid}/TXYBA/original.text"
        },
        PlatformType.XIAOHONGSHUANDROID.value: {
            "url": f"{base_url}hongshu_android",
            "storage_path": lambda tid: f"geo/{tid}/XHSA/original.text"
        },
        PlatformType.BAIDUBAIKAN.value: {
            "url": f"{base_url}baikan",
            "storage_path": lambda tid: f"geo/{tid}/BK/original.text"
        },
        PlatformType.BAIDUWENXIN.value: {
            "url": f"{base_url}baiduai",
            "storage_path": lambda tid: f"geo/{tid}/BDWX/original.text"
        }
    }


def get_sec_id(user_id):
    import requests

    url = f"https://api.douchacha.com/open/d/hx/user/user_info?user_id={user_id}"

    payload = {}
    headers = {
        'Authorization': 'eyJhbGciOiJIUzI1NiJ9.eyJ0eXBlIjoiUEMiLCJ1c2VySWQiOjEzOTE2OTMxOTU3NTQyMjU2NjQsImNyZWF0ZURhdGUiOiIyMDI2LTA2LTAzIDE3OjE4OjEzIiwiZXhwIjoxNzgzMDcwMjkzfQ.S12OckXnT7PZot-dnQHPFoFmL3GTZ8M1IDL6x2aWeKU'
    }

    response = requests.request("GET", url, headers=headers, data=payload)

    return response.json().get('data').get('sec_uid')


def get_user_info(secUid):

    import requests
    url = f"http://172.16.1.37:8873/collect_user_info?secUid={secUid}"

    payload = {}
    headers = {}

    response = requests.request("GET", url, headers=headers, data=payload)

    print(response.text)



if __name__ == '__main__':
    key_list = ['BDAI:geo:stream_batch:list',
'BDAI:geo:batch:list',
'DB:geo:stream_batch:list',
'DB:geo:batch:list',
'DOUBA:geo:stream_batch:list',
'DOUBA:geo:batch:list',
'DP:geo:stream_batch:list',
'DP:geo:batch:list',
'DPA:geo:stream_batch:list',
'DPA:geo:batch:list',
'DYAI:geo:stream_batch:list',
'DYAI:geo:batch:list',
'KIMI:geo:stream_batch:list',
'KIMI:geo:batch:list',
'TXYB:geo:stream_batch:list',
'TXYB:geo:batch:list',
'TXYBA:geo:stream_batch:list',
'TXYBA:geo:batch:list',
'TYQW:geo:stream_batch:list',
'TYQW:geo:batch:list',
'TYQWA:geo:stream_batch:list',
'TYQWA:geo:batch:list',
'WXYY:geo:stream_batch:list',
'WXYY:geo:batch:list',
'XHSA:geo:stream_batch:list',
'XHSA:geo:batch:list',
'geo:task_commit:list']


    def init_redis4():
        try:
            redis_client = redis.Redis(
                host="172.16.0.24",
                port=6379,
                db=4,
                password="aiyingli@@123",
                socket_timeout=5,
                decode_responses=True
            )
            return redis_client
        except Exception as e:
            return None


    # aa = list(aa)

    # for a in aa:
    #     sec_uid = get_sec_id(a)
    #     get_user_info(sec_uid)
    # print(t.scard('third_hx_user_id'))
    # while 1:
    #     for k in key_list:
    #         print(f"{k}----{t.llen(k)}")
    #     time.sleep(10)

    #
    # print(len(t.smembers(key)))





