# Source: https://gitee.com/yangro/ql/blob/main/bak/yys.py
# Raw: https://gitee.com/yangro/ql/raw/main/bak/yys.py
# Repo: yangro/ql
# Path: bak/yys.py
# UploadedAt: 2023-10-25T14:45:53+08:00
# SHA256: 0388fc4a4dd83948dc28c5475ac1561ed3af5aea4bf641cfe788542591cd1284
# Category: web版/抓包
# Evidence: web/H5关键词 + cookie/token/header

# coding=utf-8
"""
每日8点
cron: 0 0 8 * * ?
new Env("药研社")
"""
import pymysql
import requests
import utils.db as DB
import utils.tool as TOOL

global msg

msg = """
名称|今日|总研值
:--:|:--:|:--:
"""
mysql = DB.mysql()


def main():
    dbUser = mysql.select_all("select * from tb_yys where status = 0")
    print("🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉\n")
    print("获取到: "+str(len(dbUser))+" 个用户")
    print("-------------------------------------------")

    for user in dbUser:
        signIn(user)


# 签到
def signIn(user):
    global msg
    url = "https://m.yaoyanshe.com/web-api/mdUser/v2/sign"
    headers = {
        "Host": "m.yaoyanshe.com",
        "Connection": "keep-alive",
        "accessToken": user['token'],
        "app-key": "MODULE_YYS_H5_201903161",
        "version": "3.9.4",
        "app-type": "",
        "User-Agent": "Mozilla/5.0 (Linux; Android 11; Redmi K20 Pro Premium Edition Build/RKQ1.200826.002; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/87.0.4280.141 Mobile Safari/537.36",
        "Accept": "application/json, text/plain, */*",
        "sign": "16892536F31504A9874AE85846B741D6",
        "product-code": "yys-h5",
        "devcie": "iOS",
        "app-version": "3.9.4",
        "X-Requested-With": "com.healthmudi.dia",
        "Sec-Fetch-Site": "same-origin",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-Dest": "empty",
        "Referer": "https://m.yaoyanshe.com/h/fs/sign_in",
        "Accept-Language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7"
    }
    res = requests.get(url=url, headers= headers)
    json = res.json()

    if json['status'] != 0:
        print(user['user_name'] + " ---> " + "今日签到研值: " +
              str(json['subMessage']) + "  总研值: " + str(json['subMessage']))
        msg += user['user_name'] + "|" + "失效" + "|" + "失效" + "\n\n"
        return

    oneDayReward = json['data']['oneDayReward']
    # 签到成功 获取 药研值
    url = "https://yys.yaoyanshe.com/app-api/mdUserPoints/getUserPoints"
    res = requests.get(url=url, headers=headers)
    json = res.json()

    allDayReward = json['data']

    print(user['user_name'] + " ---> " + "今日签到研值: " +
          str(oneDayReward) + "  总研值: " + str(allDayReward))

    print("-------------------------------------------")

    msg += user['user_name'] + "|" + \
        str(oneDayReward)+"|"+str(allDayReward)+"\n\n"


main()

# 一定要关闭数据库
print("\n🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉")
mysql.close()


TOOL.msgSend("b5fd9aaa249242e3aea2ca4bbebb6bfa", " ", msg, True)


print("dfdsf4ds5f4d56")