中国移动九天毕昇Python签到脚本(适用青龙面板)

本文介绍了一种使用Python编写的签到脚本,适用于青龙面板,包括安装Python依赖、添加账号信息(手动和青龙环境变量方式)、加密处理以及自动化签到流程。
摘要由CSDN通过智能技术生成

中国移动九天毕昇Python签到脚本(适用青龙面板)


移动送GPU非常大方,但是每天签到太费劲了,写个脚本开心一下

安装Python依赖

pip install requests rsa

添加账号信息

  • 手动方式
    代码末尾main函数中的usernamepassword手动添加账号密码
  • 青龙方式
    青龙环境变量:JIUTIAN_USERNAME=xxx,JIUTIAN_PASSWORD=xxx

签到代码

import base64
import os
import requests
import uuid
import re
import rsa

headers = {
    "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
    "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
    "cache-control": "max-age=0",
    "Content-type": "application/x-www-form-urlencoded",
    "origin": "null",
    "sec-ch-ua": "\"Microsoft Edge\";v=\"123\", \"Not:A-Brand\";v=\"8\", \"Chromium\";v=\"123\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\"",
    "sec-fetch-dest": "document",
    "sec-fetch-mode": "navigate",
    "sec-fetch-site": "same-origin",
    "sec-fetch-user": "?1",
    "upgrade-insecure-requests": "1",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.1559.37 Safari/537.36 Edge/13.10586"
}


def get_login_url():
    redirect_uri = "https://jiutian.10086.cn/auth/realms/TechnicalMiddlePlatform/protocol/openid-connect/auth?client_id=dlp-train-front&redirect_uri=https%3A%2F%2Fjiutian.10086.cn%2Fedu%2Fconsole%23%2Fhome%2Fcontrol&state={}&response_mode=fragment&response_type=code&scope=openid&nonce={}"
    res = session.get(redirect_uri.format(str(uuid.uuid4()), str(uuid.uuid4())), headers=headers)
    pattern = re.compile(r'action="(.*?)"')
    return pattern.findall(res.text)[0].replace("&", "&")


def get_public_key():
    public_key_url = 'https://jiutian.10086.cn/auth/realms/TechnicalMiddlePlatform/themeapi/getKey'
    response = session.get(public_key_url, headers=headers)
    return response.json()['body']


def encrypt(public_key, text):
    public_key = '''-----BEGIN PUBLIC KEY-----
    {}
    -----END PUBLIC KEY-----'''.format(public_key)
    public_key = rsa.PublicKey.load_pkcs1_openssl_pem(public_key.encode())
    return base64.b64encode(rsa.encrypt(text.encode(), public_key)).decode()


def login(username, password):
    login_url = get_login_url()
    public_key = get_public_key()
    data = {"rememberMe": "true", "loginType": "password",
            'username': encrypt(public_key, username),
            "password": encrypt(public_key, password)}
    response = session.post(login_url, headers=headers, data=data)
    return response.url


def get_code(code_url):
    try:
        return code_url.split("code=")[1].split("&")[0]
    except Exception as e:
        print(e)
        raise Exception("获取code失败,请检查账号密码是否正确")


def get_access_token(code):
    token_url = 'https://jiutian.10086.cn/auth/realms/TechnicalMiddlePlatform/protocol/openid-connect/token'
    data = {
        'code': code,
        'grant_type': 'authorization_code',
        'client_id': 'dlp-train-front',
        'redirect_uri': 'https://jiutian.10086.cn/edu/console#/home/control'
    }
    response = session.post(token_url, headers=headers, data=data)
    return response.json()['access_token']


def get_userinfo():
    userinfo_url = 'https://jiutian.10086.cn/edu/keycloak/web/user/getuserinfo'
    return session.get(userinfo_url, headers=headers).json()


def checkin():
    checkin_url = 'https://jiutian.10086.cn/edu/marketing/web/checkin/set'
    response = session.get(checkin_url, headers=headers)
    return response.json()


if __name__ == '__main__':
    session = requests.session()
    username = ''
    password = ''
    username = username if username else os.getenv('JIUTIAN_USERNAME', username)
    password = password if password else os.getenv('JIUTIAN_PASSWORD', password)
    if not username or not password:
        raise Exception('未设置用户名或密码')
    url = login(username, password)  # 登录
    code = get_code(url)  # 获取code
    access_token = get_access_token(code)  # 获取access_token
    headers['Authorization'] = 'Bearer ' + access_token  # 设置请求头
    print(f'登录用户: {get_userinfo()["body"]["userName"]}')
    checkin_info = checkin()  # 签到
    result = checkin_info["state"]
    if result == 'OK':
        print(f'签到成功, 签到信息: {checkin_info["body"]}')
    else:
        print(f'签到失败, 签到信息: {checkin_info["errorMessage"]}')

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值