Skip to content

Commit

Permalink
v0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
4444TENSEI committed Oct 18, 2024
1 parent c2869b6 commit e9fb6b6
Show file tree
Hide file tree
Showing 18 changed files with 719 additions and 761 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
user.json
.idea/
.venv/
script/
__pycache__/

user.json
*.zip
14 changes: 0 additions & 14 deletions MessagePush.py

This file was deleted.

250 changes: 133 additions & 117 deletions README.md

Large diffs are not rendered by default.

23 changes: 8 additions & 15 deletions example.user.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
[
{
"remark": "自定义数据体备注",
"enable": true,
"remark": "自定义用户名, 用于通知中标识用户",
"phone": "手机号",
"password": "密码",
"keepLogin": false,
"token": "",
"userId": "",
"planId": "",
"randomLocation": true,
"user-agent": "Dalvik/2.1.0 (Linux; U; Android 12; SM-G9860 Build/RP1A.200720.012) AppName/4.3.2",
"signCheck": true,
"country": "中国",
"province": "xx省",
"city": "xx市",
"area": "xx区",
"address": "最终打卡页面显示的文字, xx省 · xx市 · 某路边",
"longitude": "经度(精确到小数点后六位)",
"latitude": "纬度(精确到小数点后六位)",

"randomLocation": true,
"enable": true,
"desc": "打卡备注",
"type": "android",
"address": "xx省 · xx市",
"longitude": "666.252555",
"latitude": "66.567538",
"pushKey": "pushplus官网获取的key,需要实名"
"pushKey": "pushplus官网获取的key, 需要实名"
}
]
220 changes: 220 additions & 0 deletions index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
import json, random, requests
from modules.config import load_config
from modules.msg_pusher import push_to_pushplus
from modules.get_plan_id import get_plan_id
from modules.get_login_info import get_token
from modules.headers import header_user_agent
from datetime import datetime, timezone, timedelta
from modules.crypto import create_sign


# 设置重连次数
requests.adapters.DEFAULT_RETRIES = 5
# 时区
shanghai_tz = timezone(timedelta(hours=8))
gmt_time = datetime.now(shanghai_tz)
now = datetime.now(shanghai_tz)
# 检查当前时间是否小于 12 点判断上下班打卡
is_start = now.hour < 12


def save(
user,
userId,
token,
planId,
province,
address,
signType="START",
device="Android",
latitude=None,
longitude=None,
):
# 修正 create_sign 函数调用
text = device + signType + planId + userId + address
headers = {
"roleKey": "student",
"user-agent": header_user_agent(user),
"sign": create_sign(text),
"authorization": token,
"content-type": "application/json; charset=UTF-8",
}
data = {
"country": "中国",
"address": address,
"province": province,
"city": user["city"],
"area": user["area"],
"latitude": latitude,
"description": user["desc"],
"planId": planId,
"type": signType,
"device": device,
"longitude": longitude,
}
url = "https://api.moguding.net:9000/attendence/clock/v2/save"
res = requests.post(url=url, headers=headers, data=json.dumps(data))

# 检查响应是否为有效的 JSON 格式
try:
response_json = res.json()
except json.JSONDecodeError:
print(f"服务器返回了无效的 JSON 响应: {res.text}")
return False, "服务器返回了无效的 JSON 响应"

# 检查响应中是否包含 'code' 键
if "code" not in response_json:
print(f"服务器响应中没有 'code' 键: {response_json}")
return False, "服务器响应中没有 'code' 键"

# 正常处理响应
return response_json["code"] == 200, response_json["msg"]


# startType = 0 使用保持登录状态签到
# startType = 1 使用登录签到
def start_sign(userId, token, planId, user, startType):
hourNow = datetime.datetime.now(timezone(timedelta(hours=8))).hour
if hourNow < 12:
signType = "START"
else:
signType = "END"
phone = user["phone"]
print("-------------准备签到--------------")

latitude = user["latitude"]
longitude = user["longitude"]
if user["randomLocation"]:
latitude = latitude[0 : len(latitude) - 1] + str(random.randint(0, 10))
longitude = longitude[0 : len(longitude) - 1] + str(random.randint(0, 10))

signResp, msg = save(
user,
userId,
token,
planId,
user["province"],
user["address"],
signType=signType,
latitude=latitude,
longitude=longitude,
)
if signResp:
print("签到成功")
else:
print("签到失败")
if not startType:
print("-------------签到完成--------------")
return True

######################################
# 处理推送信息
pushSignType = "上班"
if signType == "END":
pushSignType = "下班"

pushSignIsOK = "成功!"
if not signResp:
pushSignIsOK = "失败!"

signStatus = "打卡"

hourNow = datetime.datetime.now(timezone(timedelta(hours=8))).hour
if hourNow == 11 or hourNow == 23:
signStatus = "补签"

# 推送消息内容构建

push_to_pushplus(
phone,
"工学云" + pushSignType + signStatus + pushSignIsOK,
"用户: " + phone + ", 工学云" + pushSignType + signStatus + pushSignIsOK,
user["pushKey"],
)

# 消息推送处理完毕
#####################################

print("-------------签到完成--------------")


def prepare_sign(users):
# 初始化打卡成功和失败的数量
success_count = 0
fail_count = 0

# 用来存储推送消息的字典, key为pushKey, value为消息内容
push_messages = {}

for user in users:
if not user["enable"]:
continue
userInfo = get_token(user)
phone = user["phone"]
remark = user.get("remark", "") # 从配置文件中读取remark值

user_info_prefix = f"[{remark}/{phone}]"

if userInfo["code"] != 200:
print(f"{user_info_prefix}打卡失败, 错误原因: {userInfo['msg']}")
fail_count += 1
continue

userId = userInfo["data"]["userId"]
token = userInfo["data"]["token"]
sign = create_sign(userId + "student")
planId = get_plan_id(user, token, sign)

# 使用上海时间判断是上班还是下班
signType = "START" if is_start else "END"
print(f"{user_info_prefix}准备{signType}打卡\n")

signResp, msg = save(
user,
userId,
token,
planId,
user["province"],
user["address"],
signType=signType,
latitude=user["latitude"],
longitude=user["longitude"],
)

# 推送消息内容构建
pushSignType = "上班" if signType == "START" else "下班"
pushSignIsOK = "成功!" if signResp else "失败!"
signStatus = "打卡"

hourNow = gmt_time.hour
if hourNow == 11 or hourNow == 23:
signStatus = "补签"

push_message = (
f"{user_info_prefix}工学云{pushSignType}{signStatus}{pushSignIsOK}\n"
)
push_messages.setdefault(user["pushKey"], []).append(push_message)
# 判断打卡是否成功并更新计数
if signResp:
success_count += 1
else:
fail_count += 1

# 合并同pushKey的消息并推送
for pushKey, messages in push_messages.items():
# 找到所有具有相同pushKey的用户
users_with_same_pushkey = [user for user in users if user["pushKey"] == pushKey]
title = "工学云打卡通知"
title += f"[{success_count}/{len(users)}]"
content = "".join(messages)
content += f"\n打卡时间: {gmt_time.strftime('%Y-%m-%d %H:%M:%S')}\n"
push_to_pushplus(users_with_same_pushkey, title, content, pushKey)


def handler(event, context):
users = load_config()
prepare_sign(users)


# 开发环境时解除
handler(0, 0)
Loading

0 comments on commit e9fb6b6

Please sign in to comment.