Skip to content

Commit

Permalink
Merge branch 'NAStool:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
HateBaozi authored Apr 9, 2023
2 parents 1276ee0 + 8691362 commit 76f22b8
Show file tree
Hide file tree
Showing 72 changed files with 3,432 additions and 714 deletions.
22 changes: 19 additions & 3 deletions app/brushtask.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def get_brushtask_info(self, taskid=None):
"site_id": task.SITE,
"interval": task.INTEVAL,
"label": task.LABEL,
"savepath": task.SAVEPATH,
"state": True if task.STATE == "Y" else False,
"downloader": task.DOWNLOADER,
"downloader_name": downloader_info.get("name") if downloader_info else None,
Expand Down Expand Up @@ -156,6 +157,8 @@ def check_task_rss(self, taskid):
if not site_info:
log.error("【Brush】刷流任务 %s 的站点已不存在,无法刷流!" % task_name)
return
# 站点属性
site_id = site_info.get("id")
site_name = site_info.get("name")
site_proxy = site_info.get("proxy")

Expand Down Expand Up @@ -217,6 +220,7 @@ def check_task_rss(self, taskid):
torrent_url=page_url,
torrent_size=size,
pubdate=pubdate,
siteid=site_id,
cookie=cookie,
ua=ua,
proxy=site_proxy):
Expand Down Expand Up @@ -303,7 +307,7 @@ def __send_message(_task_name, _delete_type, _torrent_name):
set(torrent_ids).difference(
set([(torrent.get("hash")
if downloader_type == 'qbittorrent'
else str(torrent.id)) for torrent in torrents])))
else str(torrent.hashString)) for torrent in torrents])))
# 完成的种子
for torrent in torrents:
torrent_info = self.__get_torrent_dict(downloader_type=downloader_type,
Expand Down Expand Up @@ -345,7 +349,7 @@ def __send_message(_task_name, _delete_type, _torrent_name):
set(remove_torrent_ids).difference(
set([(torrent.get("hash")
if downloader_type == 'qbittorrent'
else str(torrent.id)) for torrent in torrents])))
else str(torrent.hashString)) for torrent in torrents])))
# 下载中的种子
for torrent in torrents:
torrent_info = self.__get_torrent_dict(downloader_type=downloader_type,
Expand Down Expand Up @@ -475,13 +479,17 @@ def __download_torrent(self,
"""
if not enclosure:
return False
# 站点流控
if self.sites.check_ratelimit(site_info.get("id")):
return False
taskid = taskinfo.get("id")
taskname = taskinfo.get("name")
transfer = taskinfo.get("transfer")
sendmessage = taskinfo.get("sendmessage")
downloader_id = taskinfo.get("downloader")
download_limit = rss_rule.get("downspeed")
upload_limit = rss_rule.get("upspeed")
download_dir = taskinfo.get("savepath")
tag = taskinfo.get("label").split(',') if taskinfo.get("label") else None
# 标签
if not transfer:
Expand All @@ -498,9 +506,11 @@ def __download_torrent(self,
media_info=meta_info,
tag=tag,
downloader_id=downloader_id,
download_dir=download_dir,
download_setting="-2",
download_limit=download_limit,
upload_limit=upload_limit,
is_auto=False if download_dir else None
)
if not download_id:
# 下载失败
Expand Down Expand Up @@ -536,6 +546,7 @@ def __check_rss_rule(self,
torrent_url,
torrent_size,
pubdate,
siteid,
cookie,
ua,
proxy):
Expand All @@ -546,6 +557,7 @@ def __check_rss_rule(self,
:param torrent_url: 种子页面地址
:param torrent_size: 种子大小
:param pubdate: 发布时间
:param siteid: 站点ID
:param cookie: Cookie
:param ua: User-Agent
:return: 是否命中
Expand Down Expand Up @@ -582,6 +594,10 @@ def __check_rss_rule(self,
if re.search(r"%s" % rss_rule.get("exclude"), title):
return False

# 站点流控
if self.sites.check_ratelimit(siteid):
return False

torrent_attr = self.siteconf.check_torrent_attr(torrent_url=torrent_url,
cookie=cookie,
ua=ua,
Expand Down Expand Up @@ -742,7 +758,7 @@ def __get_torrent_dict(downloader_type, torrent):
total_size = torrent.get("total_size")
else:
# ID
torrent_id = torrent.id
torrent_id = torrent.hashString
# 做种时间
date_done = torrent.date_done or torrent.date_added
# 下载耗时
Expand Down
1 change: 1 addition & 0 deletions app/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ class SITEBRUSHTASK(Base):
SEED_SIZE = Column(Text)
INTEVAL = Column(Text)
LABEL = Column(Text)
SAVEPATH = Column(Text)
DOWNLOADER = Column(Text)
TRANSFER = Column(Text)
DOWNLOAD_COUNT = Column(Integer)
Expand Down
7 changes: 7 additions & 0 deletions app/downloader/client/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,10 @@ def set_speed_limit(self, **kwargs):
设置速度限制
"""
pass

@abstractmethod
def recheck_torrents(self, ids):
"""
下载控制:重新校验
"""
pass
18 changes: 15 additions & 3 deletions app/downloader/client/qbittorrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@


class Qbittorrent(_IDownloadClient):

# 下载器ID
client_id = "qbittorrent"
# 下载器类型
Expand Down Expand Up @@ -317,6 +316,7 @@ def get_torrent_id_by_tag(self, tag, status=None):
def add_torrent(self,
content,
is_paused=False,
is_auto=None,
download_dir=None,
tag=None,
category=None,
Expand All @@ -331,6 +331,7 @@ def add_torrent(self,
添加种子
:param content: 种子urls或文件
:param is_paused: 添加后暂停
:param is_auto: 自动管理
:param tag: 标签
:param download_dir: 下载路径
:param category: 分类
Expand Down Expand Up @@ -379,7 +380,9 @@ def add_torrent(self,
else:
seeding_time_limit = None
try:
if self._torrent_management:
if is_auto is None:
is_auto = self._torrent_management
if is_auto:
save_path = None
qbc_ret = self.qbc.torrents_add(urls=urls,
torrent_files=torrent_files,
Expand All @@ -392,7 +395,7 @@ def add_torrent(self,
download_limit=download_limit,
ratio_limit=ratio_limit,
seeding_time_limit=seeding_time_limit,
use_auto_torrent_management=self._torrent_management,
use_auto_torrent_management=is_auto,
cookie=cookie)
return True if qbc_ret and str(qbc_ret).find("Ok") != -1 else False
except Exception as err:
Expand Down Expand Up @@ -545,3 +548,12 @@ def set_speed_limit(self, download_limit=None, upload_limit=None):
except Exception as err:
ExceptionUtils.exception_traceback(err)
return False

def recheck_torrents(self, ids):
if not self.qbc:
return False
try:
return self.qbc.torrents_recheck(torrent_hashes=ids)
except Exception as err:
ExceptionUtils.exception_traceback(err)
return False
79 changes: 40 additions & 39 deletions app/downloader/client/transmission.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,25 @@ def __login_transmission(self):
def get_status(self):
return True if self.trc else False

@staticmethod
def __parse_ids(ids):
"""
统一处理种子ID
"""
if isinstance(ids, list) and any([str(x).isdigit() for x in ids]):
ids = [int(x) for x in ids if str(x).isdigit()]
elif not isinstance(ids, list) and str(ids).isdigit():
ids = int(ids)
return ids

def get_torrents(self, ids=None, status=None, tag=None):
"""
获取种子列表
返回结果 种子列表, 是否有错误
"""
if not self.trc:
return [], True
if isinstance(ids, list):
ids = [int(x) for x in ids if str(x).isdigit()]
elif str(ids).isdigit():
ids = int(ids)
ids = self.__parse_ids(ids)
try:
torrents = self.trc.get_torrents(ids=ids, arguments=self._trarg)
except Exception as err:
Expand Down Expand Up @@ -151,10 +159,7 @@ def set_torrents_status(self, ids, tags=None):
"""
if not self.trc:
return
if isinstance(ids, list):
ids = [int(x) for x in ids if str(x).isdigit()]
elif str(ids).isdigit():
ids = int(ids)
ids = self.__parse_ids(ids)
# 合成标签
if tags:
if not isinstance(tags, list):
Expand All @@ -176,8 +181,9 @@ def set_torrent_tag(self, tid, tag):
"""
if not tid or not tag:
return
ids = self.__parse_ids(tid)
try:
self.trc.change_torrent(labels=tag, ids=int(tid))
self.trc.change_torrent(labels=tag, ids=ids)
except Exception as err:
ExceptionUtils.exception_traceback(err)

Expand All @@ -201,7 +207,7 @@ def change_torrent(self,
if not tid:
return
else:
ids = int(tid)
ids = self.__parse_ids(tid)
if tag:
if isinstance(tag, list):
labels = tag
Expand Down Expand Up @@ -270,7 +276,7 @@ def get_transfer_task(self, tag, match_path=None):
true_path = self.get_replace_path(path, self.download_dir)
trans_tasks.append({
'path': os.path.join(true_path, torrent.name).replace("\\", "/"),
'id': torrent.id,
'id': torrent.hashString,
'tags': torrent.labels
})
return trans_tasks
Expand Down Expand Up @@ -329,21 +335,21 @@ def get_remove_torrents(self, config=None):
if tr_error_key and not re.findall(tr_error_key, torrent.error_string, re.I):
continue
remove_torrents.append({
"id": torrent.id,
"id": torrent.hashString,
"name": torrent.name,
"site": torrent.trackers[0].get("sitename"),
"size": torrent.total_size
})
remove_torrents_ids.append(torrent.id)
remove_torrents_ids.append(torrent.hashString)
if config.get("samedata") and remove_torrents:
remove_torrents_plus = []
for remove_torrent in remove_torrents:
name = remove_torrent.get("name")
size = remove_torrent.get("size")
for torrent in torrents:
if torrent.name == name and torrent.total_size == size and torrent.id not in remove_torrents_ids:
if torrent.name == name and torrent.total_size == size and torrent.hashString not in remove_torrents_ids:
remove_torrents_plus.append({
"id": torrent.id,
"id": torrent.hashString,
"name": torrent.name,
"site": torrent.trackers[0].get("sitename") if torrent.trackers else "",
"size": torrent.total_size
Expand All @@ -364,11 +370,11 @@ def add_torrent(self, content,
download_dir=download_dir,
paused=is_paused,
cookies=cookie)
if ret and ret.id:
if ret and ret.hashString:
if upload_limit:
self.set_uploadspeed_limit(ret.id, int(upload_limit))
self.set_uploadspeed_limit(ret.hashString, int(upload_limit))
if download_limit:
self.set_downloadspeed_limit(ret.id, int(download_limit))
self.set_downloadspeed_limit(ret.hashString, int(download_limit))
return ret
except Exception as err:
ExceptionUtils.exception_traceback(err)
Expand All @@ -377,10 +383,7 @@ def add_torrent(self, content,
def start_torrents(self, ids):
if not self.trc:
return False
if isinstance(ids, list):
ids = [int(x) for x in ids if str(x).isdigit()]
elif str(ids).isdigit():
ids = int(ids)
ids = self.__parse_ids(ids)
try:
return self.trc.start_torrent(ids=ids)
except Exception as err:
Expand All @@ -390,10 +393,7 @@ def start_torrents(self, ids):
def stop_torrents(self, ids):
if not self.trc:
return False
if isinstance(ids, list):
ids = [int(x) for x in ids if str(x).isdigit()]
elif str(ids).isdigit():
ids = int(ids)
ids = self.__parse_ids(ids)
try:
return self.trc.stop_torrent(ids=ids)
except Exception as err:
Expand All @@ -405,10 +405,7 @@ def delete_torrents(self, delete_file, ids):
return False
if not ids:
return False
if isinstance(ids, list):
ids = [int(x) for x in ids if str(x).isdigit()]
elif str(ids).isdigit():
ids = int(ids)
ids = self.__parse_ids(ids)
try:
return self.trc.remove_torrent(delete_data=delete_file, ids=ids)
except Exception as err:
Expand Down Expand Up @@ -471,10 +468,7 @@ def set_uploadspeed_limit(self, ids, limit):
return
if not ids or not limit:
return
if not isinstance(ids, list):
ids = int(ids)
else:
ids = [int(x) for x in ids if str(x).isdigit()]
ids = self.__parse_ids(ids)
self.trc.change_torrent(ids, uploadLimit=int(limit))

def set_downloadspeed_limit(self, ids, limit):
Expand All @@ -485,10 +479,7 @@ def set_downloadspeed_limit(self, ids, limit):
return
if not ids or not limit:
return
if not isinstance(ids, list):
ids = int(ids)
else:
ids = [int(x) for x in ids if str(x).isdigit()]
ids = self.__parse_ids(ids)
self.trc.change_torrent(ids, downloadLimit=int(limit))

def get_downloading_progress(self, tag=None, ids=None):
Expand All @@ -515,7 +506,7 @@ def get_downloading_progress(self, tag=None, ids=None):
# 进度
progress = round(torrent.progress)
DispTorrents.append({
'id': torrent.id,
'id': torrent.hashString,
'name': torrent.name,
'speed': speed,
'state': state,
Expand Down Expand Up @@ -551,3 +542,13 @@ def set_speed_limit(self, download_limit=None, upload_limit=None):
except Exception as err:
ExceptionUtils.exception_traceback(err)
return False

def recheck_torrents(self, ids):
if not self.trc:
return False
ids = self.__parse_ids(ids)
try:
return self.trc.verify_torrent(ids=ids)
except Exception as err:
ExceptionUtils.exception_traceback(err)
return False
Loading

0 comments on commit 76f22b8

Please sign in to comment.