Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -26,11 +26,26 @@ def human_readable_size(size):
|
|
26 |
return f"{size:.2f} {units[index]}"
|
27 |
|
28 |
def configure_session():
|
29 |
-
"""
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
def download_task(magnet_uri, download_path, status):
|
36 |
"""后台下载任务"""
|
@@ -38,13 +53,16 @@ def download_task(magnet_uri, download_path, status):
|
|
38 |
ses = configure_session()
|
39 |
status.status = "解析磁力链接..."
|
40 |
|
41 |
-
#
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
-
# 异步添加种子
|
47 |
-
handle = ses.add_torrent(params)
|
48 |
status.status = "获取元数据..."
|
49 |
|
50 |
# 等待元数据,带超时机制
|
@@ -54,7 +72,10 @@ def download_task(magnet_uri, download_path, status):
|
|
54 |
if time.time() - start > timeout:
|
55 |
raise TimeoutError("获取元数据超时")
|
56 |
time.sleep(0.5)
|
57 |
-
|
|
|
|
|
|
|
58 |
|
59 |
torrent_info = handle.get_torrent_info()
|
60 |
status.total_size = torrent_info.total_size()
|
@@ -64,7 +85,11 @@ def download_task(magnet_uri, download_path, status):
|
|
64 |
start_time = time.time()
|
65 |
last_downloaded = 0
|
66 |
while not handle.is_seed():
|
67 |
-
|
|
|
|
|
|
|
|
|
68 |
s = handle.status()
|
69 |
|
70 |
# 计算下载速度
|
@@ -130,13 +155,13 @@ def main():
|
|
130 |
|
131 |
# 下载信息
|
132 |
cols = st.columns(4)
|
133 |
-
cols[0].metric("总大小", human_readable_size(status.total_size))
|
134 |
cols[1].metric("已下载", human_readable_size(status.downloaded_size))
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
|
141 |
st.caption(f"状态: {status.status}")
|
142 |
|
|
|
26 |
return f"{size:.2f} {units[index]}"
|
27 |
|
28 |
def configure_session():
|
29 |
+
"""兼容不同版本的会话配置"""
|
30 |
+
try:
|
31 |
+
# 新版配置方式
|
32 |
+
settings = lt.settings_pack()
|
33 |
+
settings.set_str(lt.settings_pack.listen_interfaces, '0.0.0.0:6881')
|
34 |
+
ses = lt.session(settings)
|
35 |
+
except AttributeError:
|
36 |
+
# 旧版配置方式
|
37 |
+
ses = lt.session()
|
38 |
+
ses.listen_on(6881, 6891)
|
39 |
+
return ses
|
40 |
+
|
41 |
+
def parse_magnet_compat(magnet_uri):
|
42 |
+
"""兼容不同版本的磁力解析方法"""
|
43 |
+
try:
|
44 |
+
return lt.parse_magnet_uri(magnet_uri)
|
45 |
+
except AttributeError:
|
46 |
+
# 旧版处理方式
|
47 |
+
params = {'save_path': '.', 'storage_mode': lt.storage_mode_t(2)}
|
48 |
+
return (magnet_uri, params)
|
49 |
|
50 |
def download_task(magnet_uri, download_path, status):
|
51 |
"""后台下载任务"""
|
|
|
53 |
ses = configure_session()
|
54 |
status.status = "解析磁力链接..."
|
55 |
|
56 |
+
# 兼容解析磁力链接
|
57 |
+
try:
|
58 |
+
params = lt.parse_magnet_uri(magnet_uri)
|
59 |
+
params.save_path = download_path
|
60 |
+
params.storage_mode = lt.storage_mode_t.storage_mode_sparse
|
61 |
+
handle = ses.add_torrent(params)
|
62 |
+
except:
|
63 |
+
# 旧版处理方式
|
64 |
+
handle = lt.add_magnet_uri(ses, magnet_uri, {'save_path': download_path})
|
65 |
|
|
|
|
|
66 |
status.status = "获取元数据..."
|
67 |
|
68 |
# 等待元数据,带超时机制
|
|
|
72 |
if time.time() - start > timeout:
|
73 |
raise TimeoutError("获取元数据超时")
|
74 |
time.sleep(0.5)
|
75 |
+
try:
|
76 |
+
ses.post_torrent_updates() # 新版状态更新
|
77 |
+
except:
|
78 |
+
pass
|
79 |
|
80 |
torrent_info = handle.get_torrent_info()
|
81 |
status.total_size = torrent_info.total_size()
|
|
|
85 |
start_time = time.time()
|
86 |
last_downloaded = 0
|
87 |
while not handle.is_seed():
|
88 |
+
try:
|
89 |
+
ses.post_torrent_updates() # 新版需要状态更新
|
90 |
+
except:
|
91 |
+
pass
|
92 |
+
|
93 |
s = handle.status()
|
94 |
|
95 |
# 计算下载速度
|
|
|
155 |
|
156 |
# 下载信息
|
157 |
cols = st.columns(4)
|
158 |
+
cols[0].metric("总大小", human_readable_size(status.total_size) if status.total_size > 0 else "--")
|
159 |
cols[1].metric("已下载", human_readable_size(status.downloaded_size))
|
160 |
+
speed_display = (f"{status.download_rate/1024:.2f} MB/s" if status.download_rate > 1024
|
161 |
+
else f"{status.download_rate:.2f} KB/s") if status.download_rate > 0 else "--"
|
162 |
+
cols[2].metric("速度", speed_display)
|
163 |
+
time_display = f"{status.remaining_time:.1f}秒" if status.remaining_time > 0 else "--"
|
164 |
+
cols[3].metric("剩余时间", time_display)
|
165 |
|
166 |
st.caption(f"状态: {status.status}")
|
167 |
|