import os
import random
from datetime import datetime

import requests
from loguru import logger

from app.utils.common import get_all_cookies, get_proxies
from app.utils.ua import random_ua


def main():
    ua = random_ua()
    headers = {
        'User-Agent': ua,
        "Accept": "application/json, text/javascript, */*; q=0.01",
        "Accept-Encoding": "gzip, deflate, br",
        "Accept-Language": "zh-CN,zh;q=0.9",
        "Connection": "keep-alive",
        "Sec-Fetch-Site": "none",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-User": "?1",
        "Sec-Fetch-Dest": "document",
    }

    with open("../data/live_cookies.txt", "r") as fp:
        for cookie in fp:
            headers["Cookie"] = cookie.strip()
            try:
                req = requests.get(
                    'https://help.baidu.com/api/count',
                    headers=headers,
                    proxies=get_proxies(),
                    timeout=10)
                if req.json()['errno'] == 0:
                    logger.success(cookie)
                    # with open(f"./data/cookies_{datetime.now().strftime('%Y-%m-%d')}", 'a+') as f:
                    #     f.write(cookie + '\n')
            except Exception as e:
                logger.error(f"{e=}")


if __name__ == '__main__':
    main()
