Skip to content

Commit

Permalink
send the message in the user specified time interval
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangchi9 committed Jul 12, 2019
1 parent 3d59bc6 commit 9ff3831
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion everyday_wechat/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# import json
import platform
import random
import datetime
# from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler.schedulers.background import BackgroundScheduler
import itchat
Expand Down Expand Up @@ -159,7 +160,7 @@ def init_wechat():
ats = [ats]
if isinstance(ats, list):
for at in ats:
times = TIME_COMPILE.findall(at)
times = generate_random_time(at)
if not times:
print('时间{}格式出错'.format(at))
continue
Expand All @@ -178,6 +179,19 @@ def init_wechat():
if alarm_dict:
init_alarm(alarm_dict) # 初始化定时任务

def generate_random_time(at):
if len(at.replace('-','-').split('-')) == 2:
start , end = at.replace('-','-').split('-')
try:
start = datetime.datetime.strptime(start.strip(), '%H:%M')
end = datetime.datetime.strptime(end.strip(), '%H:%M')
random_time = (start + datetime.timedelta(seconds=random.randint(0, int((end - start).total_seconds())),
)).strftime('%H:%M')
return TIME_COMPILE.findall(random_time)
except:
return []
else:
return TIME_COMPILE.findall(at)

def init_alarm(alarm_dict):
# 定时任务
Expand Down

0 comments on commit 9ff3831

Please sign in to comment.