Skip to content
This repository has been archived by the owner on Sep 1, 2024. It is now read-only.

Commit

Permalink
添加Bark推送
Browse files Browse the repository at this point in the history
  • Loading branch information
lioY3 committed Feb 15, 2022
1 parent 88e534d commit a2211d0
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 24 deletions.
19 changes: 19 additions & 0 deletions api/wanxiao_push.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import datetime
import json

from utils.bark import bark_push
from utils.server_chan import server_push
from utils.wechat_enterprise import wechat_enterprise_push
from utils.email_push import email_push
Expand Down Expand Up @@ -201,3 +202,21 @@ def wanxiao_wechat_enterprise_push(corp_id, corp_secret, agent_id, to_user, chec
else:
push_list.append(check_info['errmsg'])
return wechat_enterprise_push(corp_id, corp_secret, agent_id, to_user, "\n".join(push_list))


def wanxiao_bark_push(device_key, group, check_info_list):
utc8_time = datetime.datetime.utcnow() + datetime.timedelta(hours=8)
title = f"""{utc8_time.strftime("%Y-%m-%d")} 健康打卡"""
push_list = []
for check_info in check_info_list:
if check_info["status"]:
name = check_info["post_dict"].get("username")
if not name:
name = check_info["post_dict"]["name"]
if check_info['res']['code'] == "10000":
push_list.append(f"""{name}:打卡{check_info['res']['msg']}😄😄😄""")
else:
push_list.append(f"""{name}{check_info["res"]["data"]}😢😢😢""")
else:
push_list.append(check_info['errmsg'])
return bark_push(device_key, "\n".join(push_list), title, group)
5 changes: 5 additions & 0 deletions conf/push.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,10 @@
"corp_secret": "", // 自建应用 Secret
"agent_id": "", // 自建应用 ID
"to_user": "" // 接收者用户,多用户用|分割,所有用户填写 @all
},
"bark": { // Bark: https://github.com/Finb/Bark
"enable": false,
"device_key": "", // Bark device_key
"group": "健康打卡" // 推送消息分组
}
}
10 changes: 10 additions & 0 deletions conf/user.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@
"corp_secret": "", // 自建应用 Secret
"agent_id": "", // 应用 ID
"to_user": "" // 接收者用户,多用户用|分割,所有用户填写 @all
},
"bark": { // Bark: https://github.com/Finb/Bark
"enable": false,
"device_key": "", // Bark device_key
"group": "健康打卡" // 推送消息分组
}
}
},
Expand Down Expand Up @@ -169,6 +174,11 @@
"corp_secret": "", // 自建应用 Secret
"agent_id": "", // 自建应用 ID
"to_user": "" // 接收者用户,多用户用|分割,所有用户填写 @all
},
"bark": { // Bark: https://github.com/Finb/Bark
"enable": false,
"device_key": "", // Bark device_key
"group": "健康打卡" // 推送消息分组
}
}
}
Expand Down
49 changes: 25 additions & 24 deletions index.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from api.healthy2_check import get_healthy2_check_posh_json, healthy2_check_in
from api.user_info import get_user_info
from api.wanxiao_push import wanxiao_qmsg_push, wanxiao_server_push, wanxiao_email_push, wanxiao_pipe_push, \
wanxiao_wechat_enterprise_push
wanxiao_wechat_enterprise_push, wanxiao_bark_push
from api.ykt_score import get_score_list, get_active_score, get_task_list, get_article_id, get_all_score, \
get_article_score, get_class_score, ykt_check_in
from login import CampusLogin
Expand Down Expand Up @@ -50,13 +50,14 @@ def info_push(push_dict, raw_info):
"wechat": wanxiao_server_push,
"qmsg": wanxiao_qmsg_push,
"pipehub": wanxiao_pipe_push,
"wechat_enterprise": wanxiao_wechat_enterprise_push
"wechat_enterprise": wanxiao_wechat_enterprise_push,
"bark": wanxiao_bark_push
}

push_raw_info = {
"check_info_list": raw_info
}

for push_name, push_func in push_funcs.items():
enable = push_dict.get(push_name, {}).get("enable")
if not enable:
Expand All @@ -74,16 +75,16 @@ def info_push(push_dict, raw_info):

def check_in(user):
check_dict_list = []

# 登录获取token用于打卡
token = get_token(user['phone'], user['password'], user['device_id'])

if not token:
errmsg = f"{user['phone'][:4]},获取token失败,打卡失败,"
log.warning(errmsg)
check_dict_list.append({"status": 0, "errmsg": errmsg})
return check_dict_list

# 获取个人信息
user_info = get_user_info(token)
if not user_info:
Expand All @@ -92,26 +93,26 @@ def check_in(user):
check_dict_list.append({"status": 0, "errmsg": errmsg})
return check_dict_list
log.info(f'{user_info["username"][0]}-{user_info["school"]},获取个人信息成功')

healthy1_check_config = user.get('healthy_checkin', {}).get('one_check')
healthy2_check_config = user.get('healthy_checkin', {}).get('two_check')
if healthy1_check_config.get('enable'):
# 第一类健康打卡

# 获取第一类健康打卡的参数
post_dict = get_healthy1_check_post_json(token, healthy1_check_config.get('templateid', "pneumonia"))

# 合并配置文件的打卡信息
merge_post_json(post_dict, healthy1_check_config.get('post_json', {}))

healthy1_check_dict = healthy1_check_in(token, user['phone'], post_dict)
check_dict_list.append(healthy1_check_dict)
elif healthy2_check_config.get('enable'):
# 第二类健康打卡

# 获取第二类健康打卡参数
post_dict = get_healthy2_check_posh_json(token)

# 合并配置文件的打卡信息
if not healthy2_check_config['post_json']['latitude'] and not healthy2_check_config['post_json']['longitude']:
post_dict['latitude'] = ""
Expand All @@ -121,11 +122,11 @@ def check_in(user):
if j:
post_dict[i] = j
healthy2_check_dict = healthy2_check_in(token, user_info["customerId"], post_dict)

check_dict_list.append(healthy2_check_dict)
else:
log.info('当前并未配置健康打卡方式,暂不进行打卡操作')

# 校内打卡
campus_check_config = user.get('campus_checkin', {})
if not campus_check_config.get('enable'):
Expand All @@ -137,14 +138,14 @@ def check_in(user):
id_list = get_id_list_v2(token, custom_type_id)
else:
id_list = get_id_list_v1(token)

if not id_list:
log.warning('当前未获取到校内打卡ID,请尝试重新运行,如仍未获取到,请反馈')
return check_dict_list
for index, i in enumerate(id_list):
start_end = f'{i["templateid"]} ({i.get("startTime", "")}-{i.get("endTime", "")})'
log.info(f"{start_end:-^40}")

# 获取校内打卡参数
campus_dict = get_campus_check_post(
template_id=i['templateid'],
Expand All @@ -154,12 +155,12 @@ def check_in(user):
)
# 合并配置文件的打卡信息
merge_post_json(campus_dict, campus_check_config['post_json'])

# 校内打卡
campus_check_dict = campus_check_in(user['phone'], token, campus_dict, i['id'])
check_dict_list.append(campus_check_dict)
log.info("-" * 40)

# 粮票收集
if user.get('ykt_score'):
ykt_check_in(token)
Expand All @@ -182,17 +183,17 @@ def check_in(user):
log.info("查看课表任务已完成")
# 获取活跃奖励
get_active_score(token, get_score_list(token)['active'][0])

# 获取其他奖励
get_all_score(token)

return check_dict_list


def main_handler(*args, **kwargs):
# 推送数据
raw_info = []

# 加载用户配置文件
user_config_path = kwargs['user_config_path'] if kwargs.get('user_config_path') else './conf/user.json'
push_config_path = kwargs['push_config_path'] if kwargs.get('push_config_path') else './conf/push.json'
Expand All @@ -201,13 +202,13 @@ def main_handler(*args, **kwargs):
if not user_config['phone']:
continue
log.info(user_config.get('welcome'))

# 单人打卡
check_dict = check_in(user_config)
# 单人推送
info_push(user_config['push'], check_dict)
raw_info.extend(check_dict)

# 统一推送
all_push_config = load_config(push_config_path)
info_push(all_push_config, raw_info)
Expand Down
24 changes: 24 additions & 0 deletions utils/bark.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import json
import requests


def bark_push(device_key, msg, title, group):
try:
response = requests.post(
url="https://api.day.app/push",
headers={
"Content-Type": "application/json; charset=utf-8",
},
data=json.dumps({
"body": msg,
"device_key": device_key,
"title": title,
"icon": "https://www.17wanxiao.com/new/images/logo.png",
"group": group
})
).json()
if response['code']:
return {"status": 1, "msg": "Bark推送成功"}
return {"status": 0, "errmsg": f"Bark推送失败,{response['message']}"}
except Exception as e:
return {"status": 0, "errmsg": f"Bark推送失败,{e}"}

0 comments on commit a2211d0

Please sign in to comment.