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

# coding=utf-8
"""
cron: 0 0 6 * * ?
new Env("有道云笔记")
"""

import requests
import notify
import utils.tool as TOOL
import utils.getCookie as getCookie

global msg

msg = """
名称|空间
:--:|:--:
"""



def main():
    users = getCookie.GetQlEnvs().getCookies('YDYBJ_COOKIE')
    print("🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉\n")
    print("获取到: "+str(len(users))+" 个用户")
    print("-------------------------------------------")

    for user in users:
        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['name'] + " ---> " +
                  str(sync + checkin + ad) + 'M\n')
            msg += user['name'] + "|" + \
                str(sync + checkin + ad) + 'M\n\n'
    else:
            print(user['name'] + " ---> " + 'cookie失效\n')
            msg += user['name'] + "|cookie失效\n\n"

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


main()

print("\n🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉")


notify.send("有道云笔记", msg)
