前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python实现工学云自动签到打卡

Python实现工学云自动签到打卡

作者头像
用户8988577
发布2022-12-27 16:02:01
3K0
发布2022-12-27 16:02:01
举报
文章被收录于专栏:言云纪

本文仅用于学习,请勿用于非法商业!转载请注明本文出处! 本文参考地址01:https://github.com/yecca/moguding/blob/1.0/login.py 本文参考地址02:https://blog.csdn.net/weixin_39953845/article/details/111074929

前言

顶岗实习期间学校要求工学云打卡满两百天。但是每天上下班已经很累了,如果再上班期间强制记起打卡的事情反而只会增加工作负担!本文将会以爬虫的方式来解放双手,实现工学云每日定时打卡并发送邮件进行推送!文章底部奉上完整代码!

开发环境

开发语言:Python 3.7.7 运行环境:windows10 工作版 pip库:requests,hashlib,json,time,smtplib,MIMEText 抓包工具:HTTPDebuggerUI,雷电模拟器

工学云接口

本文不叙述抓包过程,接口如下所示:

爬虫编写(Python版)

逻辑流程草图

模拟登录并保存token等信息

引入依赖库

代码语言:javascript
复制
import requests
import json

main入口

代码语言:javascript
复制
if __name__ == "__main__":
    Login_info = {}
    login()
    print(Login_info)   # 打印获取的token等信息

登录方法

代码语言:javascript
复制
def login():
    url = "https://api.moguding.net:9000/session/user/v1/login"
    headers = {
        "Content-Type": "application/json; charset=UTF-8",
        "User-Agent": "Mozilla/5.0 (Linux; U; Android 10; zh-cn; ELS-AN00 Build/HUAWEIELS-AN00) AppleWebKit/533.1 (KHTML, like Gecko) Version/5.0 Mobile Safari/533.1",
    }
    data = {
        "phone": "你的账号", 
        "password": "你的密码", 
        "loginType": "android"
        }
    res = requests.post(url,headers=headers,data=json.dumps(data)).json()
    if res['code'] != 200 :
        print("登录失败,错误信息为"+res['msg'])
    else:
        Login_info['token'] = res['data']['token']
        Login_info['userId'] = res['data']['userId']
        Login_info['moguNo'] = res['data']['moguNo']

运行效果

获取签到任务id

导入依赖库

代码语言:javascript
复制
import hashlib

main入口方法修改

代码语言:javascript
复制
if __name__ == "__main__":
    Login_info = {}
    # login()
    GetPlanId()
    print(Login_info)   # 打印获取的token等信息

生成加密参方法

代码语言:javascript
复制
# 获取planId的sign
def getPlanIdSign(userId):
    byStr = str(userId) + "student" + "3478cbbc33f84bd00d75d7dfa69e0daa"
    return getMd5(byStr)

# md5加密
def getMd5(byStr):
    encode = byStr.encode('utf-8')
    return hashlib.md5(encode).hexdigest()

获取任务id方法

代码语言:javascript
复制
def GetPlanId():
    login()
    url = "https://api.moguding.net:9000/practice/plan/v3/getPlanByStu"
    headers = {
        "Host": "api.moguding.net:9000",
        "Accept-Language": "zh-CN,zh;q=0.8",
        "User-Agent": "Mozilla/5.0 (Linux; Android 7.0; HTC M9e Build/EZG0TF) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/55.0.1566.54 Mobile Safari/537.36",
        "sign": getPlanIdSign(Login_info['userId']),
        "Connection": "keep-alive",
        "Authorization": Login_info['token'],
        "roleKey": "student",
        "Content-Type": "application/json; charset=UTF-8",
        "Accept-Encoding": "",
    }
    data = {
        "state": ""
    }
    res = requests.post(url, headers=headers, data=json.dumps(data)).json()
    Login_info['planId'] = res["data"][0]["planId"]

运行效果

签到并发送邮件

导入依赖库

代码语言:javascript
复制
import time
import smtplib
from email.mime.text import MIMEText

main入口方法修改

代码语言:javascript
复制
if __name__ == "__main__":
    Login_info = {}
    Login_info['address'] = "上海市-嘉定区-宝安公路" #
    get_time()  
    Sign_in()
    print(Login_info)

上下班判断方法

代码语言:javascript
复制
# 根据现在时间判断上下班签到
# 上班  8-16
def get_time():
    mytime = time.localtime()
    if mytime.tm_hour < 8 or mytime.tm_hour > 16:
        Login_info['cardType'] = 'END'  # 上班
    else:
        Login_info['cardType'] = 'START'    # 下班

签到请求加密参生成

代码语言:javascript
复制
# 获取签到sign加密参
def getSign(cardType, planId, userId, address):
    byStr = "Android" + str(cardType) + str(planId) + str(userId) + str(address) + "3478cbbc33f84bd00d75d7dfa69e0daa"
    return getMd5(byStr)

发送邮件方法

代码语言:javascript
复制
# 发送邮件
def send_email(text):
    mail_host = "smtp邮件服务器地址"  
    mail_user = "smtp邮件服务器账号" 
    mail_pass = "smtp邮件服务器授权码"
    sender = mail_user	# 发件人邮箱地址
    receivers = ['收件人邮箱地址']
    content = text
    title = '工学云每日签到信息'
    message = MIMEText(content, 'plain', 'utf-8')
    message['From'] = "{}".format(sender)
    message['To'] = ",".join(receivers)
    message['Subject'] = title
    try:
        smtpObj = smtplib.SMTP_SSL(mail_host, 465)
        smtpObj.login(mail_user, mail_pass)
        smtpObj.sendmail(sender, receivers, message.as_string())
    except smtplib.SMTPException as e:
        print(e)

以上方法代码请修改自己的邮件服务器账号密码

发送签到请求

代码语言:javascript
复制
# 签到
def Sign_in():
    GetPlanId()
    url = "https://api.moguding.net:9000/attendence/clock/v2/save"
    headers = {
        "Host": "api.moguding.net:9000",
        "Accept-Language": "zh-CN,zh;q=0.8",
        "User-Agent": "Mozilla/5.0 (Linux; Android 7.0; HTC M9e Build/EZG0TF) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/55.0.1566.54 Mobile Safari/537.36",
        "sign": getSign(Login_info['cardType'], Login_info['planId'], Login_info['userId'], Login_info['address']),
        "Connection": "keep-alive",
        "Authorization": Login_info['token'],
        "roleKey": "student",
        "Content-Type": "application/json; charset=UTF-8",
        "Accept-Encoding": "",
    }
    # 签到打卡信息,根据自己需求更改
    data = {
        "country": "中国", 
        "address": Login_info['address'], 
        "province": "上海市", 
        "city": "上海市", 
        "latitude": "39.910924547299565", # 纬度
        "description": "", 
        "planId": Login_info['planId'], 
        "type": Login_info['cardType'], 
        "device": "Android", 
        "longitude": "116.4133836971231"  # 经度
    }
    res = requests.post(url,headers=headers,data=json.dumps(data)).json()
    if res['code'] == 200 :
        print("签到成功!")
        send_email("工学云签到成功!")
    else :
        print("签到失败,错误原因:" + res.msg)
        send_email("工学云签到失败,错误原因:" + res.msg +"请登录并检查服务器配置信息是否正确!")

完整脚本1

代码语言:javascript
复制
import requests
import hashlib
import json
import time
import smtplib
from email.mime.text import MIMEText

# 模拟登录获取token等信息
def login():
    url = "https://api.moguding.net:9000/session/user/v1/login"
    headers = {
        "Content-Type": "application/json; charset=UTF-8",
        "User-Agent": "Mozilla/5.0 (Linux; U; Android 10; zh-cn; ELS-AN00 Build/HUAWEIELS-AN00) AppleWebKit/533.1 (KHTML, like Gecko) Version/5.0 Mobile Safari/533.1",
    }
    data = {
        "phone": "工学云账号", 
        "password": "工学云密码", 
        "loginType": "android"
        }
    res = requests.post(url,headers=headers,data=json.dumps(data)).json()
    if res['code'] != 200 :
        print("登录失败,错误信息为"+res['msg'])
    else:
        Login_info['token'] = res['data']['token']
        Login_info['userId'] = res['data']['userId']
        Login_info['moguNo'] = res['data']['moguNo']

# 获取签到任务ID
def GetPlanId():
    login()
    url = "https://api.moguding.net:9000/practice/plan/v3/getPlanByStu"
    headers = {
        "Host": "api.moguding.net:9000",
        "Accept-Language": "zh-CN,zh;q=0.8",
        "User-Agent": "Mozilla/5.0 (Linux; Android 7.0; HTC M9e Build/EZG0TF) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/55.0.1566.54 Mobile Safari/537.36",
        "sign": getPlanIdSign(Login_info['userId']),
        "Connection": "keep-alive",
        "Authorization": Login_info['token'],
        "roleKey": "student",
        "Content-Type": "application/json; charset=UTF-8",
        "Accept-Encoding": "",
    }
    data = {
        "state": ""
    }
    res = requests.post(url, headers=headers, data=json.dumps(data)).json()
    Login_info['planId'] = res["data"][0]["planId"]


# 获取planId的sign
def getPlanIdSign(userId):
    byStr = str(userId) + "student" + "3478cbbc33f84bd00d75d7dfa69e0daa"
    return getMd5(byStr)

# md5加密
def getMd5(byStr):
    encode = byStr.encode('utf-8')
    return hashlib.md5(encode).hexdigest()

# 签到
def Sign_in():
    GetPlanId()
    url = "https://api.moguding.net:9000/attendence/clock/v2/save"
    headers = {
        "Host": "api.moguding.net:9000",
        "Accept-Language": "zh-CN,zh;q=0.8",
        "User-Agent": "Mozilla/5.0 (Linux; Android 7.0; HTC M9e Build/EZG0TF) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/55.0.1566.54 Mobile Safari/537.36",
        "sign": getSign(Login_info['cardType'], Login_info['planId'], Login_info['userId'], Login_info['address']),
        "Connection": "keep-alive",
        "Authorization": Login_info['token'],
        "roleKey": "student",
        "Content-Type": "application/json; charset=UTF-8",
        "Accept-Encoding": "",
    }
    # 签到打卡信息,根据自己需求更改
    data = {
        "country": "中国", 
        "address": Login_info['address'], 
        "province": "上海市", 
        "city": "上海市", 
        "latitude": "39.910924547299565", # 纬度
        "description": "", 
        "planId": Login_info['planId'], 
        "type": Login_info['cardType'], 
        "device": "Android", 
        "longitude": "116.4133836971231"  # 经度
    }
    res = requests.post(url,headers=headers,data=json.dumps(data)).json()
    if res['code'] == 200 :
        print("签到成功!")
        send_email("工学云签到成功!")
    else :
        print("签到失败,错误原因:" + res.msg)
        send_email("工学云签到失败,错误原因:" + res.msg +"请登录并检查服务器配置信息是否正确!")
    
# 根据现在时间判断上下班签到
# 上班  8-16
def get_time():
    mytime = time.localtime()
    if mytime.tm_hour < 8 or mytime.tm_hour > 16:
        Login_info['cardType'] = 'END'  # 上班
    else:
        Login_info['cardType'] = 'START'    # 下班

# 获取签到sign加密参
def getSign(cardType, planId, userId, address):
    byStr = "Android" + str(cardType) + str(planId) + str(userId) + str(address) + "3478cbbc33f84bd00d75d7dfa69e0daa"
    return getMd5(byStr)

# 发送邮件
def send_email(text):
    mail_host = "smtp.qq.com"  
    mail_user = "14312400@qq.com" 
    mail_pass = "授权码"
    sender = mail_user
    receivers = ['收件人邮箱']
    content = text
    title = '工学云每日签到信息'
    message = MIMEText(content, 'plain', 'utf-8')
    message['From'] = "{}".format(sender)
    message['To'] = ",".join(receivers)
    message['Subject'] = title
    try:
        smtpObj = smtplib.SMTP_SSL(mail_host, 465)
        smtpObj.login(mail_user, mail_pass)
        smtpObj.sendmail(sender, receivers, message.as_string())
    except smtplib.SMTPException as e:
        print(e)
        
if __name__ == "__main__":
    Login_info = {}
    Login_info['address'] = "上海市-嘉定区-宝安公路99" #签到地址
    get_time()  
    Sign_in()
    print(Login_info)

将该脚本设置为每日定时任务即可,本文不再叙述windows/centos系统如何设置定时任务

完整程序运行效果

对本文有任何疑问的童鞋可以本文留言或邮箱联系我。后续将会尝试Springboot版本,敬请期待!!

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-09-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 开发环境
  • 工学云接口
  • 爬虫编写(Python版)
    • 逻辑流程草图
      • 模拟登录并保存token等信息
        • 引入依赖库
        • main入口
        • 登录方法
        • 运行效果
      • 获取签到任务id
        • 导入依赖库
        • main入口方法修改
        • 生成加密参方法
        • 获取任务id方法
        • 运行效果
      • 签到并发送邮件
        • 导入依赖库
        • main入口方法修改
        • 上下班判断方法
        • 签到请求加密参生成
        • 发送邮件方法
        • 发送签到请求
    • 完整脚本1
    • 完整程序运行效果
    相关产品与服务
    云服务器
    云服务器(Cloud Virtual Machine,CVM)提供安全可靠的弹性计算服务。 您可以实时扩展或缩减计算资源,适应变化的业务需求,并只需按实际使用的资源计费。使用 CVM 可以极大降低您的软硬件采购成本,简化 IT 运维工作。
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档