# Source: https://gitee.com/yangro/ql/blob/main/bak/ydybj.py
# Raw: https://gitee.com/yangro/ql/raw/main/bak/ydybj.py
# Repo: yangro/ql
# Path: bak/ydybj.py
# UploadedAt: 2023-10-25T14:45:53+08:00
# SHA256: ee4dc424b3f1ae06d0cabb02ed6ba632f06c9ba877716f279cf3368be1171dd3
# Category: APP版/抓包
# Evidence: cookie/token/authorization/header

# coding=utf-8
"""
cron: 0 0 6 * * ?
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_ydybj where status = 0")
    print("🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉\n")
    print("获取到: "+str(len(dbUser))+" 个用户")
    print("-------------------------------------------")

    for user in dbUser:
        signIn(user)


# 签到
def signIn(user):
    global msg

    ad = 0
    payload = 'yaohuo:id34976'
    headers = {'Cookie': user['cookie']}

    re = requests.request(
        "POST", "https://note.youdao.com/yws/api/daupromotion?method=sync", headers=headers, data=payload)
    if 'error' not in re.text:
        res = requests.request(
            "POST", "https://note.youdao.com/yws/mapi/user?method=checkin", headers=headers, data=payload)
        for i in range(3):
            resp = requests.request(
                "POST", "https://note.youdao.com/yws/mapi/user?method=adRandomPrompt", headers=headers, data=payload)
            ad += resp.json()['space'] // 1048576
        if 'reward' in re.text:
            sync = re.json()['rewardSpace'] // 1048576
            checkin = res.json()['space'] // 1048576
            # 签到成功
            print(user['user_name'] + " ---> " +
                  str(sync + checkin + ad) + 'M\n')
            msg += user['user_name'] + "|" + \
                str(sync + checkin + ad) + 'M\n\n'
    else:
        if sever != '0':
            print(user['user_name'] + " ---> " + 'cookie失效\n')
            msg += user['user_name'] + "|cookie失效\n\n"

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


main()

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


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