Skip to content

Commit 55b44be

Browse files
authored
feat(brushtask): 支持当前站点任务数限制 (#564)
* feat(brushtask): 支持当前站点任务数限制 + 新增:刷流任务 支持当前站点任务总数限制(需为站点或刷流任务添加标签) + 新增:刷流任务 支持当前站点下载中任务数限制(需为站点或刷流任务添加标签) * fix(brushtask): 修复站点标签判断
1 parent fe7a94b commit 55b44be

File tree

4 files changed

+105
-11
lines changed

4 files changed

+105
-11
lines changed

app/brushtask.py

+63-11
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,10 @@ def check_task_rss(self, taskid):
200200
log.info("【Brush】开始站点 %s 的刷流任务:%s..." % (site_name, task_name))
201201
# 检查是否达到保种体积
202202
if not self.__is_allow_new_torrent(taskinfo=taskinfo,
203-
dlcount=rss_rule.get("dlcount")):
203+
dlcount=rss_rule.get("dlcount"),
204+
current_site_count=rss_rule.get("current_site_count"),
205+
current_site_dlcount=rss_rule.get("current_site_dlcount"),
206+
site_info=site_info):
204207
return
205208

206209
rss_result = self.rsshelper.parse_rssxml(url=rss_url, proxy=site_proxy)
@@ -222,6 +225,11 @@ def check_task_rss(self, taskid):
222225
downloading_count = self.__get_downloading_count(downloader_id) or 0
223226
new_torrent_count = int(max_dlcount) - int(downloading_count)
224227

228+
# 当前站点任务总数
229+
current_site_count = rss_rule.get("current_site_count")
230+
# 当前站点下载任务数
231+
current_site_dlcount = rss_rule.get("current_site_dlcount")
232+
225233
for res in rss_result:
226234
try:
227235
# 种子名
@@ -253,7 +261,10 @@ def check_task_rss(self, taskid):
253261
# 检查能否添加当前种子,判断是否超过保种体积大小
254262
if not self.__is_allow_new_torrent(taskinfo=taskinfo,
255263
dlcount=max_dlcount,
256-
torrent_size=size):
264+
torrent_size=size,
265+
current_site_count=current_site_count,
266+
current_site_dlcount=current_site_dlcount,
267+
site_info=site_info):
257268
continue
258269
# 检查是否已处理过
259270
if self.is_torrent_handled(enclosure=enclosure):
@@ -275,7 +286,10 @@ def check_task_rss(self, taskid):
275286

276287
# 再判断一次
277288
if not self.__is_allow_new_torrent(taskinfo=taskinfo,
278-
dlcount=max_dlcount):
289+
dlcount=max_dlcount,
290+
current_site_count=current_site_count,
291+
current_site_dlcount=current_site_dlcount,
292+
site_info=site_info):
279293
break
280294
self._torrents_cache.append(enclosure)
281295
except Exception as err:
@@ -529,7 +543,7 @@ def __send_message(_task_name, _delete_type, _torrent_name, _download_name, _tor
529543
except Exception as e:
530544
ExceptionUtils.exception_traceback(e)
531545

532-
def __is_allow_new_torrent(self, taskinfo, dlcount, torrent_size=None):
546+
def __is_allow_new_torrent(self, taskinfo, dlcount, current_site_dlcount, current_site_count, site_info, torrent_size=None):
533547
"""
534548
检查是否还能添加新的下载
535549
"""
@@ -570,21 +584,59 @@ def __is_allow_new_torrent(self, taskinfo, dlcount, torrent_size=None):
570584

571585
# 检查正在下载的任务数
572586
if dlcount:
573-
downloading_count = self.__get_downloading_count(downloader_id)
574-
if downloading_count is None:
587+
downloading_total_count = self.__get_downloading_count(downloader_id)
588+
if downloading_total_count is None:
575589
log.error("【Brush】任务 %s 下载器 %s 无法连接" % (task_name, downloader_name))
576590
return False
577-
if int(downloading_count) >= int(dlcount):
591+
if int(downloading_total_count) >= int(dlcount):
578592
log.warn("【Brush】下载器 %s 正在下载任务数:%s,超过设定上限,暂不添加下载" % (
579-
downloader_name, downloading_count))
593+
downloader_name, downloading_total_count))
594+
return False
595+
596+
# 检查是否添加标签
597+
label = list(set((taskinfo.get("label").split(',') if taskinfo.get("label") else []) +
598+
(site_info.get("tags").split(',') if site_info.get("tags") else [])))
599+
if label is None or len(label) <= 0:
600+
return True
601+
602+
site_name = site_info.get("name")
603+
604+
# 检查当前站点正在下载的任务数量
605+
if current_site_dlcount:
606+
current_site_count_downloading = self.__get_downloading_count(downloader_id, tag=label)
607+
if current_site_count_downloading is None:
608+
log.error("【Brush】任务 %s 下载器 %s 无法连接" % (task_name, downloader_name))
609+
return False
610+
if int(current_site_count_downloading) >= int(current_site_count):
611+
log.warn("【Brush】站点 %s 正在下载任务数:%s,超过设定上限,暂不添加下载" % (
612+
site_name, current_site_count_downloading))
580613
return False
614+
615+
# 检查当前站点任务数量
616+
if current_site_count:
617+
current_site_count_total = self.__get_task_count(downloader_id, tag=label)
618+
if current_site_count_total is None:
619+
log.error("【Brush】任务 %s 下载器 %s 无法连接" % (task_name, downloader_name))
620+
return False
621+
if int(current_site_count_total) >= int(current_site_count):
622+
log.warn("【Brush】站点 %s 任务总数:%s,超过设定上限,暂不添加下载" % (
623+
site_name, current_site_count_total))
624+
return False
625+
581626
return True
582627

583-
def __get_downloading_count(self, downloader_id):
628+
def __get_downloading_count(self, downloader_id, tag=None):
629+
"""
630+
查询当前正在下载的任务数,支持限定 tag
631+
"""
632+
torrents = self.downloader.get_downloading_torrents(downloader_id=downloader_id, tag=tag) or []
633+
return len(torrents)
634+
635+
def __get_task_count(self, downloader_id, tag):
584636
"""
585-
查询当前正在下载的任务数
637+
查询当前任务总数,限定 tag
586638
"""
587-
torrents = self.downloader.get_downloading_torrents(downloader_id=downloader_id) or []
639+
torrents = self.downloader.get_torrents(downloader_id=downloader_id, tag=tag) or []
588640
return len(torrents)
589641

590642
def __download_torrent(self,

web/action.py

+4
Original file line numberDiff line numberDiff line change
@@ -1967,6 +1967,8 @@ def __add_brushtask(data):
19671967
brushtask_include = data.get("brushtask_include")
19681968
brushtask_exclude = data.get("brushtask_exclude")
19691969
brushtask_dlcount = data.get("brushtask_dlcount")
1970+
brushtask_current_site_count = data.get("brushtask_current_site_count")
1971+
brushtask_current_site_dlcount = data.get("dl")
19701972
brushtask_peercount = data.get("brushtask_peercount")
19711973
brushtask_seedtime = data.get("brushtask_seedtime")
19721974
brushtask_seedratio = data.get("brushtask_seedratio")
@@ -1985,6 +1987,8 @@ def __add_brushtask(data):
19851987
"include": brushtask_include,
19861988
"exclude": brushtask_exclude,
19871989
"dlcount": brushtask_dlcount,
1990+
"current_site_count": brushtask_current_site_count,
1991+
"current_site_dlcount": brushtask_current_site_dlcount,
19881992
"peercount": brushtask_peercount,
19891993
"pubdate": brushtask_pubdate,
19901994
"upspeed": brushtask_upspeed,

web/apiv1.py

+2
Original file line numberDiff line numberDiff line change
@@ -1683,6 +1683,8 @@ class BrushTaskUpdate(ClientResource):
16831683
parser.add_argument('brushtask_include', type=str, help='包含', location='form')
16841684
parser.add_argument('brushtask_exclude', type=str, help='排除', location='form')
16851685
parser.add_argument('brushtask_dlcount', type=int, help='同时下载任务数', location='form')
1686+
parser.add_argument('brushtask_current_site_count', type=int, help='当前站点任务总数', location='form')
1687+
parser.add_argument('brushtask_current_site_dlcount', type=int, help='当前站点下载任务数', location='form')
16861688
parser.add_argument('brushtask_peercount', type=int, help='做种人数限制', location='form')
16871689
parser.add_argument('brushtask_seedtime', type=float, help='做种时间(小时)', location='form')
16881690
parser.add_argument('brushtask_seedratio', type=float, help='分享率', location='form')

web/templates/site/brushtask.html

+36
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,22 @@ <h5 class="modal-title" id="brushtask_modal_title">新建任务</h5>
446446
<input type="text" id="brushtask_dlcount" class="form-control" placeholder="10">
447447
</div>
448448
</div>
449+
<div class="col-lg-4">
450+
<div class="mb-3">
451+
<label class="form-label">当前站点下载任务数 <span class="form-help"
452+
title="当前站点下载任务数超过此值时不再添加下载,为站点或刷流任务添加标签后生效"
453+
data-bs-toggle="tooltip">?</span></label>
454+
<input type="text" id="brushtask_current_site_dlcount" class="form-control" placeholder="5">
455+
</div>
456+
</div>
457+
<div class="col-lg-4">
458+
<div class="mb-3">
459+
<label class="form-label">当前站点任务总数 <span class="form-help"
460+
title="当前站点任务总数超过此值时不再添加下载,为站点或刷流任务添加标签后生效"
461+
data-bs-toggle="tooltip">?</span></label>
462+
<input type="text" id="brushtask_current_site_count" class="form-control" placeholder="10">
463+
</div>
464+
</div>
449465
<div class="col-lg-4">
450466
<div class="mb-3">
451467
<label class="form-label">包含 <span class="form-help"
@@ -819,6 +835,8 @@ <h5 class="modal-title">刷流种子明细</h5>
819835
$("#brushtask_sendmessage").prop("checked", false);
820836
}
821837
$("#brushtask_dlcount").val(ret.task.rss_rule.dlcount)
838+
$("#brushtask_current_site_count").val(ret.task.rss_rule.current_site_count)
839+
$("#brushtask_current_site_dlcount").val(ret.task.rss_rule.current_site_dlcount)
822840
$("#brushtask_peercount").val(ret.task.rss_rule.peercount)
823841
$("#brushtask_include").val(ret.task.rss_rule.include);
824842
$("#brushtask_exclude").val(ret.task.rss_rule.exclude);
@@ -1025,6 +1043,22 @@ <h5 class="modal-title">刷流种子明细</h5>
10251043
} else {
10261044
$("#brushtask_dlcount").removeClass("is-invalid");
10271045
}
1046+
//当前站点任务总数
1047+
let brushtask_current_site_count = $("#brushtask_current_site_count").val();
1048+
if (brushtask_current_site_count && isNaN(brushtask_current_site_count)) {
1049+
$("#brushtask_current_site_count").addClass("is-invalid");
1050+
return;
1051+
} else {
1052+
$("#brushtask_current_site_count").removeClass("is-invalid");
1053+
}
1054+
//当前站点任务下载数
1055+
let brushtask_current_site_dlcount = $("#brushtask_current_site_dlcount").val();
1056+
if (brushtask_current_site_dlcount && isNaN(brushtask_current_site_dlcount)) {
1057+
$("#brushtask_current_site_dlcount").addClass("is-invalid");
1058+
return;
1059+
} else {
1060+
$("#brushtask_current_site_dlcount").removeClass("is-invalid");
1061+
}
10281062
//上传限速
10291063
let brushtask_upspeed = $("#brushtask_upspeed").val();
10301064
if (brushtask_upspeed && isNaN(brushtask_upspeed)) {
@@ -1174,6 +1208,8 @@ <h5 class="modal-title">刷流种子明细</h5>
11741208
brushtask_include: brushtask_include,
11751209
brushtask_exclude: brushtask_exclude,
11761210
brushtask_dlcount: brushtask_dlcount,
1211+
brushtask_current_site_count: brushtask_current_site_count,
1212+
brushtask_current_site_dlcount: brushtask_current_site_dlcount,
11771213
brushtask_peercount: brushtask_peercount_do + "#" + brushtask_peercount,
11781214
brushtask_seedtime: brushtask_seedtime_do + "#" + brushtask_seedtime,
11791215
brushtask_seedratio: brushtask_seedratio_do + "#" + brushtask_seedratio,

0 commit comments

Comments
 (0)