Skip to content

Commit

Permalink
refactor: 优化消息推送,突出失败
Browse files Browse the repository at this point in the history
之前都是统一推送标题,无法及时获取到是否成功还是失败,要点到消息详情才能看到

BREAKING CHANGE: 增加判断,只要有1个失败,标题就是预约失败
  • Loading branch information
397179459 committed Aug 17, 2023
1 parent 8687846 commit aa010e4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
18 changes: 11 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
logging.error("配置文件未找到配置")
sys.exit(1)

isSuccess = True
successMsg = ""
s_title = '茅台预约成功'
s_content = ""

for section in configs.sections():
mobile = section
Expand Down Expand Up @@ -56,13 +56,17 @@
shopInfo = f'商品:{title};门店:{shop_info["name"]}'
logging.info(shopInfo)
reservation_params = process.act_params(max_shop_id, item)
successMsg = successMsg + process.reservation(reservation_params, mobile) + shopInfo + "\n"
# 核心预约步骤
r_success, r_content = process.reservation(reservation_params, mobile)
# 为了防止漏掉推送异常,所有只要有一个异常,标题就显示失败
if not r_success:
s_title = '!!失败!!茅台预约'
s_content = s_content + r_content + shopInfo + "\n"
# 领取小茅运和耐力值
process.getUserEnergyAward(mobile)
except BaseException as e:
isSuccess = False
print(e)
logging.error(e)

if isSuccess:
print('要推送的消息:' + "\n" + successMsg)
process.send_email(successMsg)
# 推送消息
process.send_msg(s_title, s_content)
28 changes: 13 additions & 15 deletions process.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,35 +241,33 @@ def act_params(shop_id: str, item_id: str):
return params


def send_email(msg: str):
# 消息推送
def send_msg(title, content):
if config.PUSH_TOKEN is None:
return
title = 'i茅台预约' # 改成你要的标题内容
content = msg # 改成你要的正文内容
url = 'http://www.pushplus.plus/send'
r = requests.get(url, params={'token': config.PUSH_TOKEN,
'title': title,
'content': content})
logging.info(f'通知推送结果:{r.status_code, r.text}')


# 执行预约
# 核心代码,执行预约
def reservation(params: dict, mobile: str):
params.pop('userId')
responses = requests.post("https://app.moutai519.com.cn/xhr/front/mall/reservation/add", json=params,
headers=headers)
if responses.status_code == 401:
send_email(f'[{mobile}],登录token失效,需要重新登录')
raise RuntimeError
if '您的实名信息未完善或未通过认证' in responses.text:
send_email(f'[{mobile}],{responses.text}')
raise RuntimeError
msg = f'预约:mobile:{mobile};response code:{responses.status_code};response body:{responses.text};'
logging.info(msg)
# if responses.status_code == 401:
# send_msg('!!失败!!茅台预约', f'[{mobile}],登录token失效,需要重新登录')
# raise RuntimeError
if responses.status_code == 200:
msg = f'预约:mobile:{mobile};response code:{responses.status_code};'
return msg
# send_email(f'预约 : mobile:{mobile} : response code : {responses.status_code}, response body : {responses.text}')
r_success = True
else:
r_success = False

msg = f'预约:{mobile};Code:{responses.status_code};Body:{responses.text};'
logging.info(msg)
return r_success, msg


# 用高德api获取地图信息
Expand Down

0 comments on commit aa010e4

Please sign in to comment.