# 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)

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  # 自动解码为字符串，避免bytes类型问题
        )
        return redis_client
    except Exception as e:
        return None
import uuid
def deduplicate_redis_list(redis_client, key, batch_size=500):
    old_count = redis_client.llen(key)

    if old_count == 0:
        return 0, 0

    temp_key = f"{key}:dedup:{uuid.uuid4().hex}"
    seen_req_ids = set()
    new_count = 0

    try:
        for start in range(0, old_count, batch_size):
            end = min(start + batch_size - 1, old_count - 1)

            values = redis_client.lrange(key, start, end)
            unique_values = []

            for value in values:
                if isinstance(value, bytes):
                    text = value.decode("utf-8")
                else:
                    text = value

                try:
                    data = json.loads(text)
                except (TypeError, json.JSONDecodeError):
                    # 非法 JSON 暂时保留
                    unique_values.append(value)
                    continue

                req_id = data.get("reqId")

                # 没有 reqId 的数据保留
                if not req_id:
                    unique_values.append(value)
                    continue

                if req_id in seen_req_ids:
                    continue

                seen_req_ids.add(req_id)
                unique_values.append(value)

            if unique_values:
                # RPUSH 保持原 List 的顺序
                redis_client.rpush(temp_key, *unique_values)
                new_count += len(unique_values)

        if new_count > 0:
            # 原子地用临时 List 覆盖原 List
            redis_client.rename(temp_key, key)
        else:
            redis_client.delete(key)

        return old_count, new_count

    except Exception:
        # 发生异常时保留原 List，只清理临时数据
        redis_client.delete(temp_key)
        raise

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']
    # mt:snipaste_v3:only_content
    init_redis_1 = init_redis()
    init_redis_1.delete('DB:geo:batch:list')
    init_redis_1.delete('DB:geo:stream_batch:list')

    # old_count, new_count = deduplicate_redis_list(
    #     redis_client8,
    #     "geo:task_commit:list",
    #     batch_size=500,
    # )
    # print(redis_client8.llen("geo:task_commit:list"))








