基于Python实现工学云自动签到打卡文章做的脚本优化
创建文件名为sign.py并拷入以下代码保存
import requests
import hashlib
import json
import time
import smtplib
from email.mime.text import MIMEText
class gxy_sign:
# 初始化
def __init__(self,user_account_info,sign_info,email_info):
self.user_account_info = user_account_info
self.sign_info = sign_info
self.url_all = {
"login": "https://api.moguding.net:9000/session/user/v1/login",
"planid": "https://api.moguding.net:9000/practice/plan/v3/getPlanByStu",
"sign": "https://api.moguding.net:9000/attendence/clock/v2/save"
}
self.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",
"Connection": "keep-alive",
"Content-Type": "application/json; charset=UTF-8",
"Accept-Encoding": "",
}
self.user_info = {}
if email_info:
self.email_info = email_info
self.send_email(self.Sign_in())
else:
self.Sign_in()
# 根据现在时间判断上下班签到
# 上班 8-16
def get_time(self):
mytime = time.localtime()
if mytime.tm_hour < 8 or mytime.tm_hour > 16:
self.sign_info['type'] = 'END' # 上班
self.sign_info['types'] = '下班' # 上班
else:
self.sign_info['type'] = 'START' # 下班
self.sign_info['types'] = '上班' # 上班
return self.sign_info['type']
# 发送请求
def Post_data(self,url,data):
return requests.post(url=url,headers=self.headers,data=json.dumps(data)).json()
# 获取planid签名
def PlanIdSign(self):
byStr = str(self.user_info["userId"]) + "student" + "3478cbbc33f84bd00d75d7dfa69e0daa"
return self.getMd5(byStr)
# 获取签到签名
def Sign_num(self):
byStr = "Android" + str(self.get_time()) + str(self.user_info["planId"]) + str(self.user_info["userId"]) + str(self.sign_info["address"]) + "3478cbbc33f84bd00d75d7dfa69e0daa"
return self.getMd5(byStr)
# md5加密
def getMd5(self,byStr):
encode = byStr.encode('utf-8')
return hashlib.md5(encode).hexdigest()
# 登录
def login(self):
res = self.Post_data(self.url_all["login"],self.user_account_info)
if res["code"] != 200 :
return "登录失败,错误信息为"+res["msg"]
else:
self.headers["Authorization"] = res["data"]["token"]
self.headers["roleKey"] = "student"
self.user_info["userId"] = res["data"]["userId"]
self.user_info["moguNo"] = res["data"]["moguNo"]
self.user_info["planid_sign"] = self.PlanIdSign()
# 获取任务id
def Get_PlanId(self):
self.login()
self.headers["sign"] = self.user_info["planid_sign"]
data = {
"state": ""
}
res = self.Post_data(self.url_all["planid"],data)
self.user_info["planId"] = res["data"][0]["planId"]
self.sign_info["planId"] = res["data"][0]["planId"]
self.user_info["sign_num"] = self.Sign_num()
def Sign_in(self):
self.Get_PlanId()
self.headers["sign"] = self.user_info["sign_num"]
res = self.Post_data(self.url_all["sign"],self.sign_info)
if res["code"] == 200:
return self.sign_info['types'] + "签到成功!\n系统返回结果" + str(res)
else:
return self.sign_info['types'] + "签到失败!\n错误原因为" + str(res)
# 发送邮件
def send_email(self,text):
mail_host = self.email_info["mail_host"]
mail_user = self.email_info["mail_user"]
mail_pass = self.email_info["mail_pass"]
sender = self.email_info["sender"]
receivers = self.email_info["receivers"]
title = '工学云每日签到信息'
message = MIMEText(text, '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)
在和sign.py文件同目录下创建自定义名文件并保存如下代码,最后根据自己的信息做修改即可正常运行 点击跳转经纬度查询地址
from sign import gxy_sign
def main():
# 账号信息
user_account_info = {
"phone": "工学云账号",
"password": "工学云密码",
"loginType": "android"
}
# 签到打卡信息
sign_info = {
"country": "中国",
"address": "上海市·上海家里蹲大学",
"province": "上海市",
"city": "上海市",
"latitude": "31.350884", # 纬度
"description": "",
"device": "Android",
"longitude": "121.291435" # 经度
}
# 邮箱配置
email_info = {
"mail_host" : "smtp.qq.com" ,
"mail_user" : "14312400@qq.com" ,
"mail_pass" : "邮箱授权码",
"sender" : "14312400@qq.com",
"receivers" : ['接收者邮箱1','接收者邮箱2'],
"title" : '工学云每日签到信息'
}
gxy_sign(user_account_info,sign_info,email_info)
# 如果不需要发送邮箱服务输入False即可。
# gxy_sign(user_account_info,sign_info,False)
if __name__ == "__main__" :
main()