Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -25,18 +25,26 @@ def human_readable_size(size):
|
|
25 |
index += 1
|
26 |
return f"{size:.2f} {units[index]}"
|
27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
def download_task(magnet_uri, download_path, status):
|
29 |
"""后台下载任务"""
|
30 |
try:
|
31 |
-
ses =
|
32 |
-
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
|
39 |
-
|
|
|
40 |
status.status = "获取元数据..."
|
41 |
|
42 |
# 等待元数据,带超时机制
|
@@ -46,6 +54,7 @@ def download_task(magnet_uri, download_path, status):
|
|
46 |
if time.time() - start > timeout:
|
47 |
raise TimeoutError("获取元数据超时")
|
48 |
time.sleep(0.5)
|
|
|
49 |
|
50 |
torrent_info = handle.get_torrent_info()
|
51 |
status.total_size = torrent_info.total_size()
|
@@ -55,6 +64,7 @@ def download_task(magnet_uri, download_path, status):
|
|
55 |
start_time = time.time()
|
56 |
last_downloaded = 0
|
57 |
while not handle.is_seed():
|
|
|
58 |
s = handle.status()
|
59 |
|
60 |
# 计算下载速度
|
@@ -122,8 +132,11 @@ def main():
|
|
122 |
cols = st.columns(4)
|
123 |
cols[0].metric("总大小", human_readable_size(status.total_size))
|
124 |
cols[1].metric("已下载", human_readable_size(status.downloaded_size))
|
125 |
-
cols[2].metric("速度",
|
126 |
-
|
|
|
|
|
|
|
127 |
|
128 |
st.caption(f"状态: {status.status}")
|
129 |
|
@@ -139,9 +152,6 @@ def main():
|
|
139 |
file_name=os.path.basename(status.file_path),
|
140 |
mime="application/octet-stream"
|
141 |
)
|
142 |
-
|
143 |
-
# 可选:清理文件
|
144 |
-
# os.remove(status.file_path)
|
145 |
|
146 |
if __name__ == "__main__":
|
147 |
main()
|
|
|
25 |
index += 1
|
26 |
return f"{size:.2f} {units[index]}"
|
27 |
|
28 |
+
def configure_session():
|
29 |
+
"""配置libtorrent会话"""
|
30 |
+
settings = lt.settings_pack()
|
31 |
+
settings.set_str(lt.settings_pack.listen_interfaces, '0.0.0.0:6881')
|
32 |
+
settings.set_int(lt.settings_ppack.alert_mask, lt.alert.category_t.all_categories)
|
33 |
+
return lt.session(settings)
|
34 |
+
|
35 |
def download_task(magnet_uri, download_path, status):
|
36 |
"""后台下载任务"""
|
37 |
try:
|
38 |
+
ses = configure_session()
|
39 |
+
status.status = "解析磁力链接..."
|
40 |
|
41 |
+
# 创建添加参数
|
42 |
+
params = lt.parse_magnet_uri(magnet_uri)
|
43 |
+
params.save_path = download_path
|
44 |
+
params.storage_mode = lt.storage_mode_t.storage_mode_sparse
|
45 |
|
46 |
+
# 异步添加种子
|
47 |
+
handle = ses.add_torrent(params)
|
48 |
status.status = "获取元数据..."
|
49 |
|
50 |
# 等待元数据,带超时机制
|
|
|
54 |
if time.time() - start > timeout:
|
55 |
raise TimeoutError("获取元数据超时")
|
56 |
time.sleep(0.5)
|
57 |
+
ses.post_torrent_updates()
|
58 |
|
59 |
torrent_info = handle.get_torrent_info()
|
60 |
status.total_size = torrent_info.total_size()
|
|
|
64 |
start_time = time.time()
|
65 |
last_downloaded = 0
|
66 |
while not handle.is_seed():
|
67 |
+
ses.post_torrent_updates()
|
68 |
s = handle.status()
|
69 |
|
70 |
# 计算下载速度
|
|
|
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 |
+
cols[2].metric("速度",
|
136 |
+
f"{status.download_rate/1024:.2f} MB/s" if status.download_rate > 1024
|
137 |
+
else f"{status.download_rate:.2f} KB/s")
|
138 |
+
cols[3].metric("剩余时间",
|
139 |
+
f"{status.remaining_time:.1f}秒" if status.remaining_time else "--")
|
140 |
|
141 |
st.caption(f"状态: {status.status}")
|
142 |
|
|
|
152 |
file_name=os.path.basename(status.file_path),
|
153 |
mime="application/octet-stream"
|
154 |
)
|
|
|
|
|
|
|
155 |
|
156 |
if __name__ == "__main__":
|
157 |
main()
|