本文仅用于学习,请勿用于非法商业!转载请注明本文出处! 本文参考地址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,雷电模拟器
本文不叙述抓包过程,接口如下所示:
import requests
import json
if __name__ == "__main__":
Login_info = {}
login()
print(Login_info) # 打印获取的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']
import hashlib
if __name__ == "__main__":
Login_info = {}
# login()
GetPlanId()
print(Login_info) # 打印获取的token等信息
# 获取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 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"]
import time
import smtplib
from email.mime.text import MIMEText
if __name__ == "__main__":
Login_info = {}
Login_info['address'] = "上海市-嘉定区-宝安公路" #
get_time()
Sign_in()
print(Login_info)
# 根据现在时间判断上下班签到
# 上班 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邮件服务器地址"
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)
以上方法代码请修改自己的邮件服务器账号密码
# 签到
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 +"请登录并检查服务器配置信息是否正确!")
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版本,敬请期待!!