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

# coding=utf-8
"""
每日8:30
cron: 0 30 8 * * ?
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('BDTB_COOKIE')
    print("🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉\n")
    print("获取到: "+str(len(users))+" 个用户")
    print("-------------------------------------------")

    for user in users:
        signIn(user)


# 签到
def signIn(user):
    global msg
    url = 'http://c.tieba.baidu.com/c/c/forum/msign'
    data = user['cookie']
    res = requests.post(url=url, data=data)
    json = res.json()

    code = json['error_code']
    if (code == '0'):
        userMsg = json['error']['usermsg']
        if (userMsg == '成功'):
            print(user['name'] + " ---> " + "签到成功\n")
            msg += user['name'] + "|" + "签到成功" + "\n\n"
        else:
            if (userMsg == "你签得太快了，先看看贴子再来签吧:)"):
                print(user['name'] + " ---> " + "今日已签到" + "\n")
                msg += user['name'] + "|" + "今日已签到" + "\n\n"
            else:
                print(user['name'] + " ---> " + userMsg+"\n")
                msg += user['name'] + "|" + userMsg + "\n\n"
    else:
        errMsg = json['error_msg']
        print(user['name'] + " ---> " + errMsg + "\n")
        msg += user['name'] + "|" + errMsg + "\n\n"

    print("-------------------------------------------\n")


main()

print("🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉")

notify.send("百度贴吧", msg)
