# Source: https://gitee.com/yangro/ql/blob/main/sbjz.py
# Raw: https://gitee.com/yangro/ql/raw/main/sbjz.py
# Repo: yangro/ql
# Path: sbjz.py
# UploadedAt: 2023-10-25T14:45:53+08:00
# SHA256: c58dc13953a22db5fb9a805951182a32fad8d42d18d8beea20bcf644edc2f964
# Category: 微信小程序/抓包版
# Evidence: appid/openid/session_key/encryptedData等小程序字段

# coding=utf-8
"""
每日9:00
cron: 0 0 9 * * ?
new Env("瘦吧减脂")
"""
import urllib
from urllib import parse
import requests
import notify
import time
import re
import random

import utils.tool as TOOL
import utils.getCookie as getCookie

global msg

msg = """
名称|今日|总瘦点
:--:|:--:|:--:
"""

# 获取用户总瘦点及签到天数


def getAllsdAndSignin(user):
    t = TOOL.getTimeStamp()
    dataTime = TOOL.getDateTime(t)
    url = "https://gateway.shouba.cn/points/new/info/V2"
    data = '{"_":'+str(t)+'}'
    headers = {
        "Host": "gateway.shouba.cn",
        "Connection": "keep-alive",
        "Authorization": user['cookie'],
        "User-Agent": "Mozilla/5.0 (Linux; Android 11; M2011K2C Build/RKQ1.200826.002; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/108.0.5359.128 Mobile Safari/537.36-Android-ShouBa",
        "Content-Type": "application/json;charset=UTF-8",
        "Accept": "application/json, text/plain, */*",
        "timestamp": str(t),
        "Custom-Version": "3.11.98",
        "Custom-Client-Type": "H5",
        "appKey": "shouba_8BAV12P2a",
        "Origin": "//h5.shouba.cn",
        "X-Requested-With": "com.shoubakeji.shouba",
        "Sec-Fetch-Site": "same-site",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-Dest": "empty",
        "Accept-Language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7"
    }
    res = requests.post(url=url, data=data, headers=headers)
    json = res.json()
    if (json['code'] == '200'):
        num = int(json['data']['points'])
        print(user['name']+" ---> "+"已签到: "+str(json['data']['contSignInTotal']
                                                )+"天 连续签到: "+str(json['data']['contSignInReward'])+"天"+"\n")
    else:
        num = 0
        print(user['name']+" ---> "+"信息获取失败"+"\n")
        print(json)
    return num


# 获取全部任务
def getRenWuAll(user):
    global msg
    t = TOOL.getTimeStamp()
    dataTime = TOOL.getDateTime(t)
    url = "https://gateway.shouba.cn/points/new/task/info?_=" + str(t)
    headers = {
        "Host": "gateway.shouba.cn",
        "Connection": "keep-alive",
        "Authorization": user['cookie'],
        "User-Agent": "Mozilla/5.0 (Linux; Android 11; M2011K2C Build/RKQ1.200826.002; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/108.0.5359.128 Mobile Safari/537.36-Android-ShouBa",
        "Accept": "application/json, text/plain, */*",
        "timestamp": str(t),
        "Custom-Version": "3.11.98",
        "Custom-Client-Type": "H5",
        "appKey": "shouba_8BAV12P2a",
        "Origin": "//h5.shouba.cn",
        "X-Requested-With": "com.shoubakeji.shouba",
        "Sec-Fetch-Site": "same-site",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-Dest": "empty",
        "Referer": "//h5.shouba.cn/",
        "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['code'] != '200'):
        print(user['name'] + ' ---> ' + json['msg'])
        msg += user['name'] + "|" + "0" + "|" + json['msg'] + "\n\n"
        return []

    renWu = json['data']['newTabsContent'][1]['taskList']
    rwArray = []
    for rw in renWu:
        rwArray.append(
            {'name': rw['title'], 'msg': rw['btnInitName'], 'code': rw['code']})
    return rwArray


# 领取瘦点
def lqsd(user, code):
    global msg
    t = TOOL.getTimeStamp()
    dataTime = TOOL.getDateTime(t)
    url = "https://gateway.shouba.cn/points/new/dailyTask/gainIntegralV2"
    data = '{"code": '+code+', "_": '+str(t)+'}'
    headers = {
        "Host": "gateway.shouba.cn",
        "Connection": "keep-alive",
        "Authorization": user['cookie'],
        "User-Agent": "Mozilla/5.0 (Linux; Android 11; M2011K2C Build/RKQ1.200826.002; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/108.0.5359.128 Mobile Safari/537.36-Android-ShouBa",
        "Content-Type": "application/json;charset=UTF-8",
        "Accept": "application/json, text/plain, */*",
        "timestamp": str(t),
        "Custom-Client-Type": "H5",
        "appKey": "shouba_8BAV12P2a",
        "Origin": "//h5.shouba.cn",
        "X-Requested-With": "com.shoubakeji.shouba",
        "Sec-Fetch-Site": "same-site",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-Dest": "empty",
        "Referer": "//h5.shouba.cn/",
        "Accept-Language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7"
    }
    res = requests.post(url=url, data=data, headers=headers)
    json = res.json()

    if (json['code'] == '200'):
        return int(re.findall("\d+", json['msg'])[0])
    else:
        return 0


# 签到
def appSignIn(user):
    t = TOOL.getTimeStamp()
    dataTime = TOOL.getDateTime(t)
    url = "https://gateway.shouba.cn/points/new/info/V2"
    data = '{"_":'+str(t)+'}'
    headers = {
        "Host": "gateway.shouba.cn",
        "Connection": "keep-alive",
        "Authorization": user['cookie'],
        "User-Agent": "Mozilla/5.0 (Linux; Android 11; M2011K2C Build/RKQ1.200826.002; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/108.0.5359.128 Mobile Safari/537.36-Android-ShouBa",
        "Content-Type": "application/json;charset=UTF-8",
        "Accept": "application/json, text/plain, */*",
        "timestamp": str(t),
        "Custom-Version": "3.11.98",
        "Custom-Client-Type": "H5",
        "appKey": "shouba_8BAV12P2a",
        "Origin": "//h5.shouba.cn",
        "X-Requested-With": "com.shoubakeji.shouba",
        "Sec-Fetch-Site": "same-site",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-Dest": "empty",
        "Accept-Language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7"
    }
    res = requests.post(url=url, data=data, headers=headers)
    json = res.json()
    num = 0
    if (json['code'] == '200'):
        try:
            num = int(json['data']['bannerList']
                      ['signGiftbagConfig']['amount'])
            print(user['name']+" ---> "+"今日签到: "+str(num)+"\n")
        except Exception as e:
            print(user['name']+" ---> " + "今日签到 json没有找到")
    else:
        num = 0
        print(user['name']+" ---> "+"签到失败失败"+"\n")
        print(json)
    return num


# 记录体重
def jltz(user, code):
    t = TOOL.getTimeStamp()
    dataTime = TOOL.getDateTime(t)
    url = "https://gateway.shouba.cn/bodyfat/post/custom/upload"
    data = '{"age":"28","appVersion":"3.11.98","bodyDate":"' + \
        str(dataTime)+'","gender":"2","height":"160","historyId":"101","phoneType":"Redmi K20 Pro Premium Edition","systemVersion":"11","weight":"50.0"}'
    headers = {
        "Authorization": user['cookie'],
        "version": "3.11.98",
        "Custom-Version": "3.11.98",
        "Custom-Client-Type": "android",
        "Custom-Channel": "store",
        "Download-Channel": "1",
        "OSModel": "Xiaomi Redmi K20 Pro Premium Edition",
        "OSVersion": "android 11",
        "appKey": "shouba_zHmU1kOo9",
        "timestamp": str(t),
        "appId": "com.shoubakeji.shouba",
        "Content-Type": "application/json; charset=UTF-8",
        "Host": "gateway.shouba.cn",
        "Connection": "Keep-Alive",
        "User-Agent": "okhttp/3.14.7"
    }
    res = requests.post(url=url, data=data, headers=headers)
    json = res.json()
    if (json['code'] == '200'):
        #成功, 领取
        num = lqsd(user, code)
        print(user['name']+" ---> "+"记录体重"+str(num)+"\n")
    else:
        # 失败
        num = 0
        print(user['name']+" ---> "+"记录体重领取失败"+"\n")
        print(json)
    return num


# 便便打卡
def bbdk(user, code):
    t = TOOL.getTimeStamp()
    dataTime = time.strftime('%Y-%m-%d', time.localtime(t/1000))
    url = "https://gateway.shouba.cn/shouba/excrement/saveRecord"
    data = '{"colourId":"8","consumTime":"'+urllib.parse.quote(
        "小于5分钟")+'","eatNum":1,"eatTime":1,"feelingId":"22","isEatProbiotics":1,"quantityId":"15","recordDate":"2023-01-15","recordTime":"02:55","shapeId":"1","smellId":"19","timeType":"2"}'
    headers = {
        "Authorization": user['cookie'],
        "version": "3.11.98",
        "Custom-Version": "3.11.98",
        "Custom-Client-Type": "android",
        "Custom-Channel": "store",
        "Download-Channel": "1",
        "OSModel": "Xiaomi M2011K2C",
        "OSVersion": "android 11",
        "appKey": "shouba_zHmU1kOo9",
        "timestamp": str(t),
        "appId": "com.shoubakeji.shouba",
        "Content-Type": "application/json; charset=UTF-8",
        "Host": "gateway.shouba.cn",
        "Connection": "Keep-Alive",
        "User-Agent": "okhttp/3.14.7"
    }
    res = requests.post(url=url, data=data, headers=headers)
    json = res.json()
    if (json['code'] == '200'):
        #成功, 领取
        num = lqsd(user, code)
        print(user['name']+" ---> "+"便便打卡"+str(num)+"\n")
    else:
        # 失败
        num = 0
        print(user['name']+" ---> "+"便便打卡领取失败"+"\n")
        print(json)
    return num


# 心情打卡
def xqdk(user, code):
    t = TOOL.getTimeStamp()
    dataTime = time.strftime('%Y-%m-%d', time.localtime(t/1000))
    url = "https://gateway.shouba.cn/shouba/motion/mood/record"
    data = '{"moodStatus": "1", "recordDate": "'+str(dataTime)+'"}'
    headers = {
        "Authorization": user['cookie'],
        "version": "3.11.98",
        "Custom-Version": "3.11.98",
        "Custom-Client-Type": "android",
        "Custom-Channel": "store",
        "Download-Channel": "1",
        "OSModel": "Xiaomi M2011K2C",
        "OSVersion": "android 11",
        "appKey": "shouba_zHmU1kOo9",
        "appId": "com.shoubakeji.shouba",
        "Content-Type": "application/json;charset=UTF-8",
        "Host": "gateway.shouba.cn",
        "Connection": "Keep-Alive",
        "User-Agent": "okhttp/3.14.7"
    }
    res = requests.post(url=url, data=data, headers=headers)
    json = res.json()
    if (json['code'] == '200'):
        #成功, 领取
        num = lqsd(user, code)
        print(user['name']+" ---> "+"心情打卡"+str(num)+"\n")
    else:
        # 失败
        num = 0
        print(user['name']+" ---> "+"心情打卡领取失败"+"\n")
        print(json)
    return num


# 随眠打卡
def smdk(user, code):
    print("随眠打卡")
    return 0


# 运动打卡
def yddk(user, code):
    t = TOOL.getTimeStamp()
    dataTime = time.strftime('%Y-%m-%d', time.localtime(t/1000))
    url = "https://gateway.shouba.cn/shouba/exercise/post/video/clockIn"
    data = '{"exerciseId":"81","recordDate":"' + \
        str(dataTime)+'","validTime":"13"}'
    headers = {
        "Authorization": user['cookie'],
        "version": "3.11.98",
        "Custom-Version": "3.11.98",
        "Custom-Client-Type": "android",
        "Custom-Channel": "store",
        "Download-Channel": "1",
        "OSModel": "Xiaomi M2011K2C",
        "OSVersion": "android 11",
        "appKey": "shouba_zHmU1kOo9",
        "timestamp": str(t),
        "appId": "com.shoubakeji.shouba",
        "Content-Type": "application/json; charset=UTF-8",
        "Host": "gateway.shouba.cn",
        "Connection": "Keep-Alive",
        "User-Agent": "okhttp/3.14.7"
    }
    res = requests.post(url=url, data=data, headers=headers)
    json = res.json()
    if (json['code'] == '200'):
        #成功, 领取
        num = lqsd(user, code)
        print(user['name']+" ---> "+"运动打卡"+str(num)+"\n")
    else:
        # 失败
        num = 0
        print(user['name']+" ---> "+"运动打卡领取失败"+"\n")
        print(json)
    return num


# 饮水打卡
def yshuidk(user, code):
    t = TOOL.getTimeStamp()
    dataTime = time.strftime('%Y-%m-%d', time.localtime(t/1000))
    url = "https://gateway.shouba.cn/shouba/water/post/clockinV2"
    data = '{"waterIntake":"' + \
        str(random.randint(200, 400))+'","recordDate":"'+str(dataTime)+'"}'
    headers = {
        "Authorization": user['cookie'],
        "version": "3.11.98",
        "Custom-Version": "3.11.98",
        "Custom-Client-Type": "android",
        "Custom-Channel": "store",
        "Download-Channel": "1",
        "OSModel": "Xiaomi M2011K2C",
        "OSVersion": "android 11",
        "appKey": "shouba_zHmU1kOo9",
        "timestamp": str(t),
        "appId": "com.shoubakeji.shouba",
        "Content-Type": "application/json;charset=UTF-8",
        "Host": "gateway.shouba.cn",
        "Connection": "Keep-Alive",
        "User-Agent": "okhttp/3.14.7"
    }
    res = requests.post(url=url, data=data, headers=headers)
    json = res.json()
    if (json['code'] == '200'):
        #成功, 领取
        num = lqsd(user, code)
        print(user['name']+" ---> "+"饮水打卡"+str(num)+"\n")
    else:
        # 失败
        num = 0
        print(user['name']+" ---> "+"饮水打卡领取失败"+"\n")
        print(json)
    return num


# 饮食打卡
def yshidk(user, code):
    t = TOOL.getTimeStamp()
    dataTime = time.strftime('%Y-%m-%d', time.localtime(t/1000))
    url = "https://gateway.shouba.cn/shouba/dict/post/clockIn"
    data = '{"clockInType":"3","foodList":[{"calorie":"83.0","clockInType":0,"colour":"1","colourValue":"#21CE97","content":"'+urllib.parse.quote(
        "放心吃")+'","date":"'+str(dataTime)+'","foodId":"237873","foodName":"'+urllib.parse.quote(
        "脂20营养固体饮料")+'","imagePath":"https://thinbaross.shouba.cn/uploads/school/images/20210108/e0bb9758de826936b182faf9ffbfbde6.jpg","isZhi20":true,"num":"1.0","self":false,"singleCalorie":"83.0","title":"'+urllib.parse.quote(
        "早餐")+'","unit":"'+urllib.parse.quote(
        "袋")+'"}],"recordDate":"'+str(dataTime)+'","type":"1"}'
    headers = {
        "Authorization": user['cookie'],
        "version": "3.11.98",
        "Custom-Version": "3.11.98",
        "Custom-Client-Type": "android",
        "Custom-Channel": "store",
        "Download-Channel": "1",
        "OSModel": "Xiaomi M2011K2C",
        "OSVersion": "android 11",
        "appKey": "shouba_zHmU1kOo9",
        "timestamp": str(t),
        "appId": "com.shoubakeji.shouba",
        "Content-Type": "application/json; charset=UTF-8",
        "Content-Length": "442",
        "Host": "gateway.shouba.cn",
        "Connection": "Keep-Alive",
        "User-Agent": "okhttp/3.14.7"
    }
    res = requests.post(url=url, data=data, headers=headers)
    json = res.json()
    if (json['code'] == '200'):
        #成功, 领取
        num = lqsd(user, code)
        print(user['name']+" ---> "+"饮食打卡"+str(num)+"\n")
    else:
        # 失败
        num = 0
        print(user['name']+" ---> "+"饮食打卡领取失败"+"\n")
        print(json)
    return num


# 点赞
def likes(user, code):
    t = TOOL.getTimeStamp()
    dataTime = time.strftime('%Y-%m-%d', time.localtime(t/1000))
    for i in range(6):
        index = str(random.randint(1300, 1500))
        url = "https://gateway.shouba.cn/circle/celebrity/collectSystemArticles"
        data = '{"types":"2","aid":'+str(index)+',"isList":"0"}'
        headers = {
            "Authorization": user['cookie'],
            "version": "3.11.98",
            "Custom-Version": "3.11.98",
            "Custom-Client-Type": "android",
            "Custom-Channel": "store",
            "Download-Channel": "1",
            "OSModel": "Xiaomi M2011K2C",
            "OSVersion": "android 11",
            "appKey": "shouba_zHmU1kOo9",
            "timestamp": str(t),
            "appId": "com.shoubakeji.shouba",
            "Content-Type": "application/json;charset=UTF-8",
            "Host": "gateway.shouba.cn",
            "Connection": "Keep-Alive",
            "User-Agent": "okhttp/3.14.7"
        }
        res = requests.post(url=url, data=data, headers=headers)
        json = res.json()
        time.sleep(1.5)
    if (json['code'] == '200'):
        #成功, 领取
        num = lqsd(user, code)
        print(user['name']+" ---> "+"点赞五次内容"+str(num)+"\n")
    else:
        # 失败
        num = 0
        print(user['name']+" ---> "+"点赞五次内容领取失败"+"\n")
        print(json)
    return num

# 分享五次


def share(user, code):
    t = TOOL.getTimeStamp()
    dataTime = time.strftime('%Y-%m-%d', time.localtime(t/1000))
    for i in range(6):
        index = str(random.randint(225171, 225299))
        url = "https://gateway.shouba.cn/userShare/shareAddRecordCallback"
        data = '{"shareType":"3","contentId":"' + \
            str(index)+'","shareChannel":"101","authorType":"5","authorId":"188161","shareId":"","shareUrl":""}'
        headers = {
            "Authorization": user['cookie'],
            "version": "3.11.98",
            "Custom-Version": "3.11.98",
            "Custom-Client-Type": "android",
            "Custom-Channel": "store",
            "Download-Channel": "1",
            "OSModel": "Xiaomi Redmi K20 Pro Premium Edition",
            "OSVersion": "android 11",
            "appKey": "shouba_zHmU1kOo9",
            "timestamp": str(t),
            "appId": "com.shoubakeji.shouba",
            "Content-Type": "application/json;charset=UTF-8",
            "Host": "gateway.shouba.cn",
            "Connection": "Keep-Alive",
            "User-Agent": "okhttp/3.14.7"
        }
        res = requests.post(url=url, data=data, headers=headers)
        json = res.json()
        time.sleep(1.5)
    if (json['code'] == '200'):
        #成功, 领取
        num = lqsd(user, code)
        print(user['name']+" ---> "+"分享五次"+str(num)+"\n")
    else:
        # 失败
        num = 0
        print(user['name']+" ---> "+"分享五次领取失败"+"\n")
        print(json)
    return num


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

    for user in users:
        signIn(user)
        time.sleep(1.5)


# 签到
def signIn(user):
    global msg
    num = 0
    rwarray = getRenWuAll(user)
    num += appSignIn(user)
    for rw in rwarray:
        name = rw['name']
        _msg = rw['msg']
        code = rw['code']

        if (name == '记录体重' and _msg == '去完成' or _msg == '领取'):
            num += jltz(user, code)
        elif (name == '便便打卡' and _msg == '去打卡' or _msg == '领取'):
            num += bbdk(user, code)
        elif (name == '心情打卡' and _msg == '去打卡' or _msg == '领取'):
            num += xqdk(user, code)
        elif (name == '睡眠打卡' and _msg == '去打卡' or _msg == '领取'):
            num += smdk(user, code)
        elif (name == '运动打卡' and _msg == '去打卡' or _msg == '领取'):
            num += yddk(user, code)
        elif (name == '饮水打卡' and _msg == '去打卡' or _msg == '领取'):
            num += yshuidk(user, code)
        elif (name == '饮食打卡' and _msg == '去打卡' or _msg == '领取'):
            num += yshidk(user, code)
        elif (name == '点赞5次内容' and _msg == '去点赞' or _msg == '领取'):
            num += likes(user, code)
        elif (name == '分享5次内容' and _msg == '去分享' or _msg == '领取'):
            num += share(user, code)
        time.sleep(0.3)

    allsd = getAllsdAndSignin(user)

    print(user['name'] + " ---> " +
          "总领取["+str(num)+"]"+"总瘦点: "+str(allsd)+"\n")
    msg += user['name'] + "|" + str(num) + "|" + str(allsd) + "\n\n"
    print("-------------------------------------------\n")


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

notify.send("瘦吧减脂", msg)
