# -*- coding: utf-8 -*-
# @Author: wangkun
# @Time: 2023/4/23
import requests

from common.feishu import Feishu


class GetName:
    @classmethod
    def get_users(cls, token, cookie):
        user_sheet = Feishu.get_values_batch(log_type="get", crawler="gongzhonghao", sheetid="Bzv72P")
        for i in range(143, len(user_sheet)):
        # for i in range(1, 6):
            user_name = user_sheet[i][0]
            user_index = user_sheet[i][1]
            # print(f"user_name:{user_name}")
            # print(f"user_index:{user_index}")
            url = 'https://mp.weixin.qq.com/cgi-bin/searchbiz'
            headers = {
                'authority': 'mp.weixin.qq.com',
                'accept': '*/*',
                'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
                'cache-control': 'no-cache',
                'cookie': cookie,
                'pragma': 'no-cache',
                'referer': 'https://mp.weixin.qq.com/cgi-bin/appmsg?t=media/appmsg_edit_v2&action=edit&isNew=1&type=77&createType=0&token=1221914130&lang=zh_CN',
                'sec-ch-ua': '"Chromium";v="112", "Microsoft Edge";v="112", "Not:A-Brand";v="99"',
                'sec-ch-ua-mobile': '?0',
                'sec-ch-ua-platform': '"macOS"',
                'sec-fetch-dest': 'empty',
                'sec-fetch-mode': 'cors',
                'sec-fetch-site': 'same-origin',
                'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36 Edg/112.0.1722.58',
                'x-requested-with': 'XMLHttpRequest',
            }
            params = {
                'action': 'search_biz',
                'begin': '0',
                'count': '5',
                'query': user_name,
                'token': token,
                'lang': 'zh_CN',
                'f': 'json',
                'ajax': '1',
            }
            response = requests.get(url=url, params=params, headers=headers)
            user_info = response.json()["list"][user_index - 1]
            wechat_name = user_info['alias']
            avatar_url = user_info['round_head_img']
            print(f"user_name:{user_name}")
            print(f"wechat_name:{wechat_name}")
            print(f"avatar_url:{avatar_url}")
            print("\n")
            Feishu.update_values(log_type="get",
                                 crawler="gongzhonghao",
                                 sheetid="Bzv72P",
                                 ranges=f"C{i+1}:D{i+1}",
                                 values=[[wechat_name, avatar_url]])


if __name__ == "__main__":
    gzh_token = "1053633489"
    gzh_cookie = "pgv_pvid=4569186026; pac_uid=0_f9e46a4283b4d; ua_id=OAMvmEDYQG3jR7vtAAAAAPDDtar6-DZte3Voa67Zjr8=; wxuin=69603835570065; mm_lang=zh_CN; tvfe_boss_uuid=7f6d4ba3822d4b08; _clck=3948330815|1|f70|0; uuid=bf85c4c650144e11babcaa44773f9e94; rand_info=CAESIEl8HDdHsNxsJUNCDsHOoz//O780u3eJp9SkZqbeapgW; slave_bizuin=3948330815; data_bizuin=3948330815; bizuin=3948330815; data_ticket=1hMvddRGqB+4IqQVJ1OfzsqMEKTjaYBiPahsRwuM11fmeHY+P7cOUqnlsVoRCr0p; slave_sid=dXFQaHo5V3kyVURkTlpMb3dkMXpWZ21teWhvQ1AwU09VMkI0aHhqR1RXVU52aDYyTGp5NGttZGNLSk5Ubmp1TjJVM0xjZFF1V0l3NTdBNW5nQ3pGUUZLYUF4eEFFNmhMMmNYX1lYSXY1azhBbE5DY25mUkxvNnRCcWpDVktCdkk2cU1UM3Z3NmhCZzVWeGtC; slave_user=gh_c53f57bf4c88; xid=ca068ab9b5f3afd98718b114943b87e5"
    GetName.get_users(gzh_token, gzh_cookie)
