Skip to content

Commit

Permalink
feat: 刷流时间间隔支持多个时间段
Browse files Browse the repository at this point in the history
  • Loading branch information
linyuan0213 committed Nov 13, 2024
1 parent ad151f8 commit b631312
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
36 changes: 22 additions & 14 deletions app/brushtask.py
Original file line number Diff line number Diff line change
Expand Up @@ -1157,23 +1157,31 @@ def get_torrent_attr(self, site_info: dict, enclosure: str):
return torrent_url,torrent_attr

@staticmethod
def is_in_time_range(time_range=None):
if not time_range:
def is_in_time_range(time_range: str=""):
if not time_range.strip():
return True # 如果时间段字符串为空,返回 True,表示不限制
try:
# 解析时间段
start_str, end_str = time_range.split('-')
start_hour, start_minute = map(int, start_str.split(':'))
end_hour, end_minute = map(int, end_str.split(':'))
start_time = dtime(start_hour, start_minute)
end_time = dtime(end_hour, end_minute)

# 获取当前时间
now = datetime.now().time()
if start_time < end_time:
return start_time <= now <= end_time
else:
return now >= start_time or now <= end_time
periods = time_range.split(",")
for period in periods:
start_str, end_str = period.split('-')
start_hour, start_minute = map(int, start_str.split(':'))
end_hour, end_minute = map(int, end_str.split(':'))
start_time = dtime(start_hour, start_minute)
end_time = dtime(end_hour, end_minute)

# 获取当前时间
now = datetime.now().time()
if start_time < end_time:
# 非跨天
if start_time <= now <= end_time:
return True
else:
# 跨天
if now >= start_time or now <= end_time:
return True
# 所有时间段都不匹配
return False
except ValueError:
log.warn("【Brush】时间段格式错误,应为 'HH:MM-HH:MM'")
return False # 格式错误时返回 False,不执行任务
2 changes: 1 addition & 1 deletion web/templates/site/brushtask.html
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ <h5 class="modal-title" id="brushtask_modal_title">新建任务</h5>
<div class="col-lg-4">
<div class="mb-3">
<label class="form-label">开启时间段 <span class="form-help"
title="为该刷新任务设置独立的刷流时间段格式为HH:MM-HH:MM,为空不开启"
title="为该刷新任务设置独立的刷流时间段格式为HH:MM-HH:MM,支持多个时间段按逗号分隔,为空不开启"
data-bs-toggle="tooltip">?</span></label>
<input type="text" id="brushtask_time_range" class="form-control" placeholder="">
</div>
Expand Down

0 comments on commit b631312

Please sign in to comment.