qnloft-spider/OA/oa_clock_in.py

249 lines
8.0 KiB
Python
Raw Permalink Normal View History

2023-11-26 12:31:18 +00:00
import sys
import requests
import time
import json
import random
# 定义的全局变量
LOGIN_NAME = "dl-renmeng"
LOGIN_PASSWD = "1111"
IS_WORK_DAY = 0
def get_clock_in_data(clock_in_time):
"""根据当前的小时,返回打卡的入参"""
print("当前时间-小时:", clock_in_time.tm_hour)
# 定义时间类型
time_type_one = ["00:00", "09:00", "18:00", "23:59"]
time_type_two = ["00:00:00", "09:00:00", "18:00:00", "23:59:59"]
clock_in_data = {}
if clock_in_time.tm_hour > 9:
# 下午打卡
clock_in_data = {
"time": time_type_one[2],
"belongtime": time_type_one[2],
"canSignTime": time_type_one[3],
"signTime": time.strftime("%H:%M:%S", clock_in_time),
"date": time.strftime("%Y-%m-%d", clock_in_time),
"belongdate": time.strftime("%Y-%m-%d", clock_in_time),
"datetime": f'{time.strftime("%Y-%m-%d", clock_in_time)} {time_type_two[2]}',
"signSectionTime": f'{time.strftime("%Y-%m-%d", clock_in_time)} {time_type_two[3]}',
"signSection": f'{time.strftime("%Y-%m-%d", clock_in_time)} {time_type_two[0]}#{time.strftime("%Y-%m-%d", struct_time)} {time_type_two[3]}',
"min": "359",
"workmins": "480",
"type": "off",
"across": "0",
"islastsign": "1",
"isYellow": "1",
"isPunchOpen": "1",
"isacross": "0",
"pre": "0",
"active": "0",
"needSign": "0",
"reSign": "1",
"min_next": "-1",
"signfrom": "e9pc",
"serialid": "1",
"signAcross": "0",
"signAcross_next": "0",
"signbelong": "今天",
"signbelongspan": "今天",
}
else:
# 上午打卡
clock_in_data = {
"time": time_type_one[1],
"belongtime": time_type_one[1],
"canSignTime": time_type_one[0],
"date": time.strftime("%Y-%m-%d", clock_in_time),
"belongdate": time.strftime("%Y-%m-%d", clock_in_time),
"datetime": f'{time.strftime("%Y-%m-%d", clock_in_time)} {time_type_two[1]}',
"signSectionTime": f'{time.strftime("%Y-%m-%d", clock_in_time)} {time_type_two[0]}',
"signSection": f'{time.strftime("%Y-%m-%d", clock_in_time)} {time_type_two[0]}#{time.strftime("%Y-%m-%d", struct_time)} {time_type_two[3]}',
"min": "540",
"workmins": "480",
"isfirstsign": "1",
"type": "on",
"across": "0",
"islastsign": "1",
"isYellow": "0",
"isPunchOpen": "1",
"isacross": "0",
"pre": "0",
"active": "1",
"needSign": "1",
"min_next": "-1",
"serialid": "1",
"signAcross": "0",
"signAcross_next": "0",
}
return clock_in_data
def trusty_sleep(sleep_time):
"""睡觉方法"""
print(f"开始休眠:{sleep_time}")
start = time.time()
while time.time() - start < sleep_time:
time.sleep(sleep_time - (time.time() - start))
struct_time = time.localtime(time.time())
# 得到结构化时间格式
now_time = time.strftime("%Y%m%d", struct_time)
# now_time = 20220131
print("当前时间:", now_time)
url = f"https://api.apihubs.cn/holiday/get?field=workday&date={now_time}&workday=1&cn=1&size=31"
print("获取工作日信息url", url)
print("开始发送请求----->>>>>>")
request_result = requests.get(url)
print("请求结果返回----->>>>>>", request_result)
# 请求返回成功
if request_result.status_code == 200:
# 解析json
is_work = json.loads(request_result.text)
# 数据获取成功
if is_work["code"] == 0:
data_list = is_work["data"]["list"][0] if is_work["data"]["list"] else []
IS_WORK_DAY = data_list["workday"] if data_list else 0
print("当前日期是否是工作日10", IS_WORK_DAY)
if IS_WORK_DAY == 1:
header = {
"Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36",
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9",
}
print("开始OA登录----->>>")
login_form_data = {
"loginid": LOGIN_NAME,
"langid": "7",
}
login_form_url = "http://oa.njhgroup.cn/api/hrm/login/getLoginForm"
login_form_result = requests.post(
login_form_url, headers=header, data=login_form_data
)
print(requests.utils.dict_from_cookiejar(login_form_result.cookies))
randcode = json.loads(login_form_result.text)["qrcode"]["loginkey"]
print("获取randcode结果---->>>", randcode)
login_data = {
"loginid": LOGIN_NAME,
"userpassword": LOGIN_PASSWD,
"logintype": "1",
"isie": "false",
}
login_cookie = login_form_result.cookies
# 开始进行OA登录
oa_login_url = "http://oa.njhgroup.cn/api/hrm/login/checkLogin"
login_result = requests.post(
oa_login_url,
headers=header,
data=login_data,
cookies=requests.utils.dict_from_cookiejar(login_form_result.cookies),
)
print(requests.utils.dict_from_cookiejar(login_result.cookies))
print(login_result.text)
print("OA登录结束----->>>", login_result.text)
# 休眠10秒
time.sleep(10)
print("OA开始刷新randcode----->>>")
ts = int(round(time.time() * 1000))
refresh_code_url = f"http://oa.njhgroup.cn/rsa/weaver.rsa.GetRsaInfo?ts={ts}"
refresh_code_result = requests.get(refresh_code_url, headers=header)
print(refresh_code_result.cookies)
print("OA刷新randcode结束----->>>")
# 组装最后的cookie
clock_in_cookie = requests.utils.dict_from_cookiejar(
login_form_result.cookies)
clock_in_cookie.update(
requests.utils.dict_from_cookiejar(refresh_code_result.cookies)
)
clock_in_cookie.update(
requests.utils.dict_from_cookiejar(login_result.cookies))
print("开始检查当日是否请假----->>>")
check_is_work_url = (
"http://oa.njhgroup.cn/api/kq/myattendance/getHrmKQMonthReportInfo"
)
check_is_work_result = requests.post(
check_is_work_url,
headers=header,
data={
"typevalue": time.strftime("%Y-%m", struct_time),
"loaddata": "1",
"type": "2",
},
cookies=clock_in_cookie,
).text
# 解析json
is_work = json.loads(check_is_work_result)
print("结束检查当日是否请假----->>>")
print(f"{struct_time.tm_mday}")
isWorkDay = is_work["result"][f"{struct_time.tm_mday}"]["isWorkDay"]
workflow = len(is_work["result"][f"{struct_time.tm_mday}"]["workflow"])
print(f"今天是否是工作日:{isWorkDay},今天是否请假:{workflow}")
needSign = False
if isWorkDay and workflow <= 0:
needSign = True
else:
print("今日有请假,不打卡~")
sys.exit()
check_is_need_sign_url = "http://oa.njhgroup.cn/api/hrm/kq/attendanceButton/getButtons"
check_is_need_sign_result = requests.post(
check_is_need_sign_url,
headers=header,
cookies=clock_in_cookie,
).text
check_is_need_sign_timeline = json.loads(check_is_need_sign_result)["timeline"]
# 0代表不需要打卡
need_sign = 0
# sign_time 为空也代表不需要打卡
sign_time = ""
if struct_time.tm_hour < 9:
# 上午走第一个集合,上午卡
need_sign = check_is_need_sign_timeline[0]["needSign"]
if "signTime" in check_is_need_sign_timeline[0]:
sign_time = check_is_need_sign_timeline[0]["signTime"]
print(f"上午打卡情况:---{need_sign} ----- {sign_time} --- {len(sign_time)}")
else:
# 下午走第二个集合,下午卡
need_sign = check_is_need_sign_timeline[1]["needSign"]
if "signTime" in check_is_need_sign_timeline[1]:
sign_time = check_is_need_sign_timeline[1]["signTime"]
print(f"下午打卡情况:---{need_sign} ----- {sign_time} --- {len(sign_time)}")
# 可以打卡
if need_sign == "1" and len(sign_time) == 0:
needSign = True
else:
print("已经打卡,无需再打~")
sys.exit()
# 检查是否已经打过卡,如果没有,则继续
if needSign:
# 开始打卡,如果是下午,则休眠一下再打卡
if struct_time.tm_hour > 9:
# 休眠 5分-15分
trusty_sleep(random.randint(300, 900))
print("OA开始打卡----->>>")
# clock_in_cookie["__randcode__"] =
# 刷新时间
struct_time = time.localtime(time.time())
sign_time = time.strftime("%H:%M:%S", struct_time)
get_clock_in_data(struct_time)
clock_in_url = "http://oa.njhgroup.cn/api/hrm/kq/attendanceButton/punchButton"
print(
"OA打卡结束----->>>",
requests.post(
clock_in_url,
headers=header,
data=get_clock_in_data(struct_time),
cookies=clock_in_cookie,
).text,
)