Skip to content

Commit

Permalink
fix transmission种子ID改为使用hashString,对存量刷流任务会有影响
Browse files Browse the repository at this point in the history
  • Loading branch information
jxxghp committed Apr 6, 2023
1 parent de9d1c7 commit 39e6299
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 52 deletions.
6 changes: 3 additions & 3 deletions app/brushtask.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,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 +345,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 @@ -742,7 +742,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
69 changes: 30 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) and any([str(x).isdigit() for x in ids]):
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
2 changes: 1 addition & 1 deletion app/downloader/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ def __download_fail(msg):
download_dir=download_dir,
cookie=site_info.get("cookie"))
if ret:
download_id = ret.id
download_id = ret.hashString
downloader.change_torrent(tid=download_id,
tag=tags,
upload_limit=upload_limit,
Expand Down
19 changes: 10 additions & 9 deletions app/helper/iyuu_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@singleton
class IyuuHelper(object):
_version = "2.0.0"
_api_base = "https://api.iyuu.cn/index.php?s=%s"
_api_base = "https://api.iyuu.cn/%s"
_sites = []
_token = None

Expand Down Expand Up @@ -82,7 +82,7 @@ def __get_sites(self):
}
}
"""
result, msg = self.__request_iyuu(url=self._api_base % 'App.Api.Sites')
result, msg = self.__request_iyuu(url=self._api_base % 'api/sites')
if result:
return result.get('sites')
else:
Expand Down Expand Up @@ -112,16 +112,17 @@ def get_seed_info(self, info_hashs: list):
"""
# FIXME 非法请求:做种列表sha1校验失败
info_hashs.sort()
hashs_str = json.dumps(info_hashs, ensure_ascii=False)
result, msg = self.__request_iyuu(url=self._api_base % 'App.Api.Infohash',
json_str = json.dumps(info_hashs, ensure_ascii=False)
sha1 = self.get_sha1(json_str)
result, msg = self.__request_iyuu(url=self._api_base % 'api/infohash',
method="post",
params={
"timestamp": int(time.time()),
"hash": hashs_str,
"sha1": self.get_sha1(hashs_str)
"timestamp": time.time(),
"hash": json_str,
"sha1": sha1
})
return result, msg

@staticmethod
def get_sha1(text) -> str:
return hashlib.sha1(text.encode("utf-8")).hexdigest()
def get_sha1(json_str) -> str:
return hashlib.sha1(json_str.encode('utf-8')).hexdigest()

0 comments on commit 39e6299

Please sign in to comment.