import requests
import json
import time
from datetime import datetime
import pytz
import hashlib
tz = pytz.timezone('Asia/Shanghai')
# 登录
def login(user, pwd):
global token, userId, roleKey
url = "https://api.moguding.net:9000/session/user/v1/login"
headers = {
'content-type': 'application/json; charset=utf-8'
}
data = {
"password": pwd,
"loginType": "android",
"uuid": "",
"phone": user
}
r = requests.post(url, data=json.dumps(data), headers=headers)
j = json.loads(r.text)
code = j['code']
name = j['data']['nikeName']
token = j['data']['token']
userId = j['data']['userId']
roleKey = j['data']['roleGroup'][0]['roleKey']
if code == 200:
print('login sign successful,welcome~ ')
else:
print('login failed')
# 获取学院信息
def getPlanByStu():
global planId
sign = getPlanByStuSign()
url = "https://api.moguding.net:9000/practice/plan/v3/getPlanByStu"
headers = {
'content-type': 'application/json; charset=utf-8',
"Host": "api.moguding.net:9000",
"Accept-Language": "zh-CN,zh;q=0.8",
"User-Agent": "Mozilla/5.0 (Linux; U; Android 7.1.2; zh-cn; SKW-A0 Build/N2G47H) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30",
"roleKey": str(roleKey),
"Sign": str(sign),
"Authorization": str(token),
"Accept-Encoding": "",
"Cache-Control": "no-cache"
}
data = {
"state": ""
}
r = requests.post(url, data=json.dumps(data), headers=headers)
j = json.loads(r.text)
j = json.loads(r.text)
code = j['code']
planId = j['data'][0]['planId']
if code == 200:
print('query successful!')
else:
print('query failed!')
# 获取学院信息接口的sign
def getPlanByStuSign():
screet = "3478cbbc33f84bd00d75d7dfa69e0daa"
s = str(userId)+str(roleKey)+screet
sign = getmd5(s)
print("sign1====> "+str(sign))
return sign
# md5加密
def getmd5(s):
md = hashlib.md5()
md.update(s.encode(encoding='utf-8'))
get_md5 = md.hexdigest()
return get_md5
# 获取签到sign为tosign_sign
def get_tosign(address):
# %Y-%m-%d %H:%M:%S
times = datetime.fromtimestamp(int(time.time()),
pytz.timezone('Asia/Shanghai')).strftime('%H')
type = ""
if int(times) < 12:
# 早上打卡
type = "START"
else:
# 下午打卡
type = "END"
s = "Android"+str(type)+str(planId)+str(userId) +
str(address)+"3478cbbc33f84bd00d75d7dfa69e0daa"
tosign_sign = getmd5(s)
print("sign2====> "+str(tosign_sign))
return tosign_sign
# 去签到任务
def tosign(description, address, province, city, longitude, latitude):
"""
description:打卡备注
address:地址
province:省份
city:城市
longitude:经度
latitude:纬度
"""
to_sign = get_tosign(address)
# %Y-%m-%d %H:%M:%S
times = datetime.fromtimestamp(int(time.time()),
pytz.timezone('Asia/Shanghai')).strftime('%H')
type = ""
if int(times) < 12:
# 早上打卡
type = "START"
else:
# 下午打卡
type = "END"
url = "https://api.moguding.net:9000/attendence/clock/v2/save"
data = {
"device": "Android",
"address": address,
"t": "",
"description": description,
"country": "中国",
"longitude": longitude,
"latitude": latitude,
"city": city,
"province": province,
"planId": planId,
"type": type
}
headers = {
'content-type': 'application/json; charset=utf-8',
"Host": "api.moguding.net:9000",
"Accept-Language": "zh-CN,zh;q=0.8",
"User-Agent": "Mozilla/5.0 (Linux; U; Android 7.1.2; zh-cn; SKW-A0 Build/N2G47H) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30",
"Sign": str(to_sign),
"Authorization": str(token),
"Accept-Encoding": "",
"Cache-Control": "no-cache"
}
r = requests.post(url, data=json.dumps(data), headers=headers)
j = json.loads(r.text)
j = json.loads(r.text)
code = j['code']
if code == 200:
print('sign successful!~')
else:
print('sign failed')
# 任务启动
def run():
# 填好配置,且不能注释下面调用的函数,否则报错.
user = '' # 账号
pwd = '' # 密码
description = "打卡"
province = "" # 实习所在省份
city = "" # 实习所在市区
longitude = "" # 经度
latitude = "" # 纬度
address = "" # 实习打卡所在的详细信息
login(user, pwd)
getPlanByStu()
get_tosign(address)
tosign(description, address, province, city, longitude, latitude)
# 云函数使用
def main_handler(event, context):
print("Received event: " + json.dumps(event, indent=2))
print("Received context: " + str(context))
run()
return("------ end ------")
if __name__ == '__main__':
run()