# -*- coding:utf-8 -*-

import requests
import json
import re
import random
import time


def get_token():
    cookies = [{'domain': '.qq.com', 'expiry': 1694488114, 'httpOnly': False, 'name': '_clsk', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': '1ni8qoa|1694401714307|1|1|mp.weixin.qq.com/weheat-agent/payload/record'}, {'domain': 'mp.weixin.qq.com', 'expiry': 1728961710, 'httpOnly': False, 'name': 'mm_lang', 'path': '/', 'sameSite': 'Lax', 'secure': True, 'value': 'zh_CN'}, {'domain': 'mp.weixin.qq.com', 'expiry': 1694747312, 'httpOnly': True, 'name': 'slave_user', 'path': '/', 'sameSite': 'Lax', 'secure': True, 'value': 'gh_0d8cf8319a3b'}, {'domain': 'mp.weixin.qq.com', 'expiry': 1694747312, 'httpOnly': True, 'name': 'slave_sid', 'path': '/', 'sameSite': 'Lax', 'secure': True, 'value': 'cGVCVnJmUkJSclZ2NjNNaXJRNjAxb2RiUk1VUzZJVFZ2OHZHWVhfNDc2eFZ6UE8wRjNialFPSkpGenZ4YjcxeVRCSUJEN2pUV1psTGR3dEVJb01HaEc3OTk1ZGw3czc0cHNqWXp3NmF4TE9sNWpXVFQ0UGZmYk9pMWMyTkhoRGZhRjhZdTRmMWVsNkRtclBw'}, {'domain': 'mp.weixin.qq.com', 'httpOnly': True, 'name': 'uuid', 'path': '/', 'sameSite': 'Lax', 'secure': True, 'value': '64a3735c687abefa844b964ee5ee22b3'}, {'domain': 'mp.weixin.qq.com', 'expiry': 1694747312, 'httpOnly': True, 'name': 'data_ticket', 'path': '/', 'sameSite': 'Lax', 'secure': True, 'value': 'huLvjc4b9KUUU2y5UDhAGtJMnkHdiHLomACVKu3FNRXQNW+cqWis6hmeiDex+48S'}, {'domain': 'mp.weixin.qq.com', 'expiry': 1694747312, 'httpOnly': True, 'name': 'bizuin', 'path': '/', 'sameSite': 'Lax', 'secure': True, 'value': '3524986952'}, {'domain': 'mp.weixin.qq.com', 'expiry': 1694747312, 'httpOnly': True, 'name': 'data_bizuin', 'path': '/', 'sameSite': 'Lax', 'secure': True, 'value': '3524986952'}, {'domain': '.qq.com', 'expiry': 1725937701, 'httpOnly': False, 'name': '_clck', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': '1mms0gn|1|fex|0'}, {'domain': 'mp.weixin.qq.com', 'expiry': 1728961701, 'httpOnly': False, 'name': 'wxuin', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': '94401701746369'}, {'domain': 'mp.weixin.qq.com', 'expiry': 1694747312, 'httpOnly': True, 'name': 'slave_bizuin', 'path': '/', 'sameSite': 'Lax', 'secure': True, 'value': '3524986952'}, {'domain': 'mp.weixin.qq.com', 'expiry': 1694747312, 'httpOnly': True, 'name': 'rand_info', 'path': '/', 'sameSite': 'Lax', 'secure': True, 'value': 'CAESIFrcVKCpifIofoD16bix61WlVXwAQWuTA8IiGdhqRfVQ'}, {'domain': 'mp.weixin.qq.com', 'expiry': 1728961710, 'httpOnly': True, 'name': 'xid', 'path': '/', 'sameSite': 'Lax', 'secure': True, 'value': 'f58a1e442db7bee9404cb07398977db9'}, {'domain': 'mp.weixin.qq.com', 'expiry': 1728961710, 'httpOnly': True, 'name': 'ua_id', 'path': '/', 'sameSite': 'Lax', 'secure': True, 'value': 'EM3eBSFcF5UwBxuLAAAAAA-FF__vfnjkh_D-s1AK7OA='}]

    cookie = {}
    for items in cookies:
        cookie[items.get("name")] = items.get("value")
    url = "https://mp.weixin.qq.com"
    response = requests.get(url, cookies=cookie)
    print(response.url)
    token = re.findall(r'token=(\d+)', str(response.url))[0]  # 从url中获取token
    return token, cookie


def extract_fake_id(fake_id):
    token, cookie = get_token()
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36",
        "Referer": "https://mp.weixin.qq.com/cgi-bin/appmsg?t=media/appmsg_edit_v2&action=edit&isNew=1&type=10&token=" + token + "&lang=zh_CN",
        "Host": "mp.weixin.qq.com",
    }
    requestUrl = "https://mp.weixin.qq.com/cgi-bin/appmsg?token=" + token + "&lang=zh_CN&f=json&ajax=1&random=" + str(
        random.random()) \
                 + "&action=list_ex&begin=" + str(0) + "&count=5&query=&fakeid={}&type=9".format(fake_id)
    search_response = requests.get(requestUrl, cookies=cookie, headers=headers)
    re_text = search_response.json()
    return re_text


if __name__ == "__main__":
    t, c = get_token()
    path = "/Users/luojunhui/cyber/gzh_spider/other_functions/fake_id.json"
    with open(path, "r", encoding="utf-8") as f:
        fake_id_list = json.loads(f.read())
    result_list_path = "result_id.json"
    f = open("result.txt", "a+", encoding="utf-8")
    for fake_info in fake_id_list[53:100]:
        response_obj = extract_fake_id(fake_info)
        print(response_obj)
        time.sleep(random.randint(20, 30))
        f.write(json.dumps(response_obj, ensure_ascii=False) + "\n")
    f.close()
    # if os.path.exists(result_list_path):
    #     with open(result_list_path, "r", encoding="utf-8") as f:
    #         result_list = json.loads(f.read())
    #     result_list.append(response_obj)
    # else:
    #     result_list = [response_obj]
    #
    # # 写文件
    # with open(result_list_path, "w", encoding="utf-8") as f:
