# -*- coding:UTF-8 -*-
# @Time  : 2020/8/13 11:47
# @File  : xiaohongshu_t.py
# @email : litao@igengmei.com
# @author : litao

import hashlib
import json

import redis

from crawler_sys.utils.output_results import retry_get_url


def md5(src):
    if isinstance(src, str):
        src = src.encode('utf-8')
    m = hashlib.md5()
    m.update(src)
    return m.hexdigest()


def cc():
    a = "fid=15972878231090404caae735a593231344ec8933c2cf&device_fingerprint=2020061115511043c9ecd8196208af8594f1819f7c53c001ef7ccc21dd7463&device_fingerprint1=2020061115511043c9ecd8196208af8594f1819f7c53c001ef7ccc21dd7463&channel=Xiaomi&versionName=6.57.0&deviceId=6e902889-1675-36dd-81d1-9653cafbd11a&platform=android&sid=session.1597287892454943279563&identifier_flag=4&t=1597288034&x_trace_page_current=search_result_notes&lang=zh-Hans&uis=light"
    bArr = bytes(a, encoding="utf8")
    st1 = "6e902889-1675-36dd-81d1-9653cafbd11a"
    bys = bytes(st1, encoding="utf8")
    i2 = 0
    sb2 = []
    ""
    i = 0
    while 1:
        i3 = i
        if (i2 >= len(bArr)):
            print(sb2)
            print("".join([str(c) for c in sb2]))
            return md5(bytes(md5(bytes("".join([str(c) for c in sb2]), encoding="utf8")).lower() + st1,
                             encoding="utf8")).lower()

        sb2.append(bArr[i2] ^ bys[i3])
        i = (i3 + 1) % len(bys)
        i2 += 1

if __name__ == "__main__":
    # url = "https://www.xiaohongshu.com/user/profile/5ea6909900000000010057a3"
    # headers = {
    #     "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
    #     "accept-encoding": "gzip, deflate",
    #     "accept-language": "zh-CN,zh;q=0.9",
    #     "cache-control": "no-cache",
    #     "cookie": "xhsTracker=url=user-profile&xhsshare=CopyLink; xhsTrackerId=38ec6dcb-d882-45e8-c539-834386696a14; xhsuid=Dv4OTnGbDg9LivGu; timestamp2=202101062497d4bed842476b2618e0ea; timestamp2.sig=-Jax1vd_iNZtToaWYMOMoFUmCJwojKQPnfP8iMeOpAc; xhs_spses.5dde=*; extra_exp_ids=gif_clt1,ques_clt1; xhs_spid.5dde=59a50d47116c4333.1609921946.3.1610074315.1609997760.3be8232b-0407-44f8-8036-d40a4c47b120",
    #     "pragma": "no-cache",
    #     "referer": "https://www.xiaohongshu.com/discovery/item/5f90384e000000000100b8f0",
    #     "sec-ch-ua": '"Google Chrome";v="87", " Not;A Brand";v="99", "Chromium";v="87"',
    #     "sec-ch-ua-mobile": "?0",
    #     "sec-fetch-dest": "document",
    #     "sec-fetch-mode": "navigate",
    #     "sec-fetch-site": "same-origin",
    #     "sec-fetch-user": "?1",
    #     "upgrade-insecure-requests": "1",
    #     "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36",
    #     "Hm_lvt_900d393eff703909946efe28447affd3": "1596187047",
    # }
    # res = retry_get_url(url, headers=headers, proxies=0)
    # print(res.text)
    rds = redis.StrictRedis(host='172.18.51.10', port=6379, db=17, decode_responses=True)
    pid_list = rds.hkeys("xiaohongshu")
    for pid in pid_list:
        res = rds.hget("xiaohongshu", pid)
        res_json = json.loads(res)

        if res_json["NoteView"].get("comments"):
            # print(res_json["NoteView"].get("data"))
            for comment in res_json["NoteView"]["comments"]["data"]:
               print(comment)
        elif res_json["NoteView"].get("commentInfo"):
            for comment in res_json["NoteView"]["commentInfo"]["comments"]:
                print(comment)