Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在用户指定的时间段内随机发送信息 #162

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions everyday_wechat/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import random
# from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler.schedulers.background import BackgroundScheduler
from datetime import datetime as dt, timedelta
import itchat
from itchat.content import (
TEXT
Expand Down Expand Up @@ -110,6 +111,18 @@ def init_data():
if alarm_dict:
init_alarm(alarm_dict) # 初始化定时任务

def set_daily_alarm(alarm_dict):
"""
设置每天随机发送消息时间
:param alarm_dict: 定时相关内容
"""
sched = BackgroundScheduler()
for key, value in alarm_dict.items():
start = dt.strptime(value['start'], '%H:%M').replace(year=dt.now().year, month=dt.now().month, day=dt.now().day)
end = dt.strptime(value['end'], '%H:%M').replace(year=dt.now().year, month=dt.now().month, day=dt.now().day)
run_time = start + timedelta(seconds=random.randint(0, int((end - start).total_seconds())),)
sched.add_job(send_alarm_msg, 'date', [key], run_date = run_time, id=key, misfire_grace_time=600)
sched.start()

def init_alarm(alarm_dict):
"""
Expand All @@ -118,9 +131,8 @@ def init_alarm(alarm_dict):
"""
# 定时任务
scheduler = BackgroundScheduler()
for key, value in alarm_dict.items():
scheduler.add_job(send_alarm_msg, 'cron', [key], hour=value['hour'],
minute=value['minute'], id=key, misfire_grace_time=600)
set_daily_alarm(alarm_dict)
scheduler.add_job(set_daily_alarm, 'cron', [alarm_dict], hour = 0, id='set_daily_alarm')
scheduler.start()
print('已开启定时发送提醒功能...')
# print(scheduler.get_jobs())
Expand Down
9 changes: 5 additions & 4 deletions everyday_wechat/utils/itchat_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,13 @@ def init_wechat_config():
ats = [ats]
if isinstance(ats, list):
for at in ats:
times = TIME_COMPILE.findall(at)
if not times:
at = at.replace('-','-').replace(':',':')
start = at.split('-')[0].strip()
end = at.split('-')[1].strip() if len(at.split('-')) == 2 else start
if not (TIME_COMPILE.findall(start) and TIME_COMPILE.findall(end)):
print('时间{}格式出错'.format(at))
continue
hour, minute = int(times[0][0]), int(times[0][1])
temp_dict = {'hour': hour, 'minute': minute, 'uuid_list': uuid_list}
temp_dict = {'start': start, 'end': end, 'uuid_list': uuid_list}
temp_dict.update(gi)
alarm_dict[md5_encode(str(temp_dict))] = temp_dict
# end---------------------------定时处理---------------------------end
Expand Down