# Source: https://github.com/xfmeng970526/ql/blob/main/mxbc.py
# Raw: https://raw.githubusercontent.com/xfmeng970526/ql/main/mxbc.py
# Repo: xfmeng970526/ql
# Path: mxbc.py
# UploadedAt: 2023-09-20T12:13:28+08:00
# SHA256: 5680c66085d13cc9bb6b3fe331c65dfe0c62e56d275a612ac9b92584c38946b3
# Category: 微信小程序/抓包版
# Evidence: appid/openid/session_key/encryptedData等小程序字段

#!/usr/bin/python3
# -- coding: utf-8 --
# -------------------------------
# @Time : 2023/1/7 10:45
# cron "4 8 * * *" script-path=mxbc.py,tag=匹配cron
# const $ = new Env('蜜雪冰城');
# 环境变量名 MXBC_TOKEN 多账号用#隔开   
# pyhton依赖 先安装 wheel requests  pycryptodome
# new Env('51-蜜雪冰城');
# 项目名称：蜜雪冰城
# -------------------------------
from requests import get
from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_v1_5
from Crypto.Hash import SHA256
from time import time
from base64 import b64encode, b64decode
from os import environ
try:
    from notify import send
    push = True
except:
    print(f"青龙无法找到notify.py文件 无法推送")
    push = False

def timestamp():
    return int(round(time() * 1000))


def RSA_sign(data):
    privateKey = '''MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCtypUdHZJKlQ9LL6lIJSphnhqjke7HclgWuWDRWvzov30du235cCm13mqJ3zziqLCwstdQkuXo9sOPIh94t6nzBHTuqYA1whrUnQrKfv9X4/h3QVkzwT+xWflE+KubJZoe+daLKkDeZjVWnUku8ov0E5vwADACfntEhAwiSZUALX9UgNDTPbj5ESeII+VztZ/KOFsRHMTfDb1GIR/dAc1mL5uYbh0h2Fa/fxRPgf7eJOeWGiygesl3CWj0Ue13qwX9PcG7klJXfToI576MY+A7027a0aZ49QhKnysMGhTdtFCksYG0lwPz3bIR16NvlxNLKanc2h+ILTFQbMW/Y3DRAgMBAAECggEBAJGTfX6rE6zX2bzASsu9HhgxKN1VU6/L70/xrtEPp4SLSpHKO9/S/Y1zpsigr86pQYBx/nx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JhuyCupS50aGKGqTxKYtiPHpWa8jZyjmanMKnEdOGyw/X4SFyodv8AEloqd81yGg=='''
    private_keyBytes = b64decode(privateKey)
    priKey = RSA.importKey(private_keyBytes)
    signer = PKCS1_v1_5.new(priKey)
    hash_obj = SHA256.new(data.encode('utf-8'))

    signature = b64encode(signer.sign(hash_obj)).decode()
    return signature


class MXBC:
    msg = ""

    def __init__(self, token):
        self.token = token
        self.appid = "d82be6bbc1da11eb9dd000163e122ecb"

    def check_in(self):
        url = f"https://mxsa.mxbc.net/api/v1/customer/signin"
        params = f"appId={self.appid}&t={timestamp()}"
        params += "&sign=" + RSA_sign(params).replace("/", "_").replace("+", "-")
        headers = {
            "app": "mxbc",
            "appchannel": "huawei",
            "appversion": "3.0.3",
            "Access-Token": self.token,
            "Host": "mxsa.mxbc.net",
            "Connection": "Keep-Alive",
            "Accept-Encoding": "gzip",
            "User-Agent": "okhttp/4.4.1",
            "Pragma": "no-cache",
            "Cache-Control": "no-cache"
        }
        data = get(url, headers=headers, params=params).json()
        if data.get("msg") == "":
            print(f"签到成功")
            MXBC.msg += "签到成功 "
        else:
            print(f"签到失败, 原因是{data.get('msg')}")
            MXBC.msg += f"签到失败, 原因是{data.get('msg')} "


    def get_userinfo(self):
        url = "https://mxsa.mxbc.net/api/v1/customer/info"
        params = f"appId={self.appid}&t={timestamp()}"
        params += "&sign=" + RSA_sign(params).replace("/", "_").replace("+", "-")
        headers = {
            "app": "mxbc",
            "appchannel": "huawei",
            "appversion": "3.0.3",
            "Access-Token": self.token,
            "Host": "mxsa.mxbc.net",
            "Connection": "Keep-Alive",
            "Accept-Encoding": "gzip",
            "User-Agent": "okhttp/4.4.1",
            "Pragma": "no-cache",
            "Cache-Control": "no-cache"
        }
        data = get(url, headers=headers, params=params).json()
        account = data.get('data').get('mobilePhone')
        coin = data.get('data').get('customerPoint')
        exp = data.get('data').get('growthValue')
        level_exp = data.get('data').get('customerLevelVo').get('growthValueMax') - data.get('data').get('growthValue')
        MXBC.msg += f"查询成功, 账号{account}, 共有雪王币{coin}, 共有成长值{exp}, 还差{level_exp}升级\n"
    def main(self):
        self.check_in()
        self.get_userinfo()

if __name__ == '__main__':
    token_list = environ.get("MXBC_TOKEN")
    [MXBC(token).main() for token in token_list.split("#")]
    if push:
        send("蜜雪冰城签到", MXBC.msg)
