Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,26 +26,34 @@ def human_readable_size(size):
|
|
| 26 |
return f"{size:.2f} {units[index]}"
|
| 27 |
|
| 28 |
def configure_session():
|
| 29 |
-
"""
|
| 30 |
-
|
| 31 |
-
#
|
| 32 |
settings = lt.settings_pack()
|
| 33 |
settings.set_str(lt.settings_pack.listen_interfaces, '0.0.0.0:6881')
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
#
|
| 37 |
ses = lt.session()
|
| 38 |
-
ses
|
| 39 |
-
|
|
|
|
| 40 |
|
| 41 |
-
def
|
| 42 |
-
"""
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
def download_task(magnet_uri, download_path, status):
|
| 51 |
"""后台下载任务"""
|
|
@@ -53,29 +61,19 @@ def download_task(magnet_uri, download_path, status):
|
|
| 53 |
ses = configure_session()
|
| 54 |
status.status = "解析磁力链接..."
|
| 55 |
|
| 56 |
-
#
|
| 57 |
-
|
| 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 |
# 等待元数据,带超时机制
|
| 69 |
-
timeout = 30
|
| 70 |
start = time.time()
|
| 71 |
while not handle.has_metadata():
|
| 72 |
if time.time() - start > timeout:
|
| 73 |
raise TimeoutError("获取元数据超时")
|
| 74 |
time.sleep(0.5)
|
| 75 |
-
|
| 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,10 +83,8 @@ def download_task(magnet_uri, download_path, status):
|
|
| 85 |
start_time = time.time()
|
| 86 |
last_downloaded = 0
|
| 87 |
while not handle.is_seed():
|
| 88 |
-
|
| 89 |
-
ses.post_torrent_updates()
|
| 90 |
-
except:
|
| 91 |
-
pass
|
| 92 |
|
| 93 |
s = handle.status()
|
| 94 |
|
|
@@ -118,16 +114,13 @@ def download_task(magnet_uri, download_path, status):
|
|
| 118 |
def main():
|
| 119 |
st.title("🕹️ 磁力链接下载器")
|
| 120 |
|
| 121 |
-
# 初始化session状态
|
| 122 |
if 'download_status' not in st.session_state:
|
| 123 |
st.session_state.download_status = DownloadStatus()
|
| 124 |
|
| 125 |
-
# 输入区域
|
| 126 |
with st.form("magnet_form"):
|
| 127 |
magnet_uri = st.text_input("磁力链接:", placeholder="magnet:?xt=urn:...")
|
| 128 |
submitted = st.form_submit_button("开始下载")
|
| 129 |
|
| 130 |
-
# 创建下载目录
|
| 131 |
download_path = Path("downloads")
|
| 132 |
download_path.mkdir(exist_ok=True)
|
| 133 |
|
|
@@ -136,7 +129,6 @@ def main():
|
|
| 136 |
st.error("无效的磁力链接格式")
|
| 137 |
return
|
| 138 |
|
| 139 |
-
# 启动下载线程
|
| 140 |
st.session_state.download_status = DownloadStatus()
|
| 141 |
thread = threading.Thread(
|
| 142 |
target=download_task,
|
|
@@ -144,16 +136,13 @@ def main():
|
|
| 144 |
)
|
| 145 |
thread.start()
|
| 146 |
|
| 147 |
-
# 实时状态显示
|
| 148 |
status = st.session_state.download_status
|
| 149 |
status_container = st.empty()
|
| 150 |
|
| 151 |
while status.status not in ["下载完成", "错误"] and status.status != "等待中":
|
| 152 |
with status_container.container():
|
| 153 |
-
# 进度条
|
| 154 |
st.progress(status.progress, text=f"进度: {status.progress*100:.1f}%")
|
| 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))
|
|
@@ -167,7 +156,6 @@ def main():
|
|
| 167 |
|
| 168 |
time.sleep(0.5)
|
| 169 |
|
| 170 |
-
# 下载完成后的处理
|
| 171 |
if status.status == "下载完成":
|
| 172 |
st.success("🎉 下载完成!")
|
| 173 |
with open(status.file_path, "rb") as f:
|
|
|
|
| 26 |
return f"{size:.2f} {units[index]}"
|
| 27 |
|
| 28 |
def configure_session():
|
| 29 |
+
"""精确版本检测的会话配置"""
|
| 30 |
+
if hasattr(lt, 'settings_pack'):
|
| 31 |
+
# 新版本配置方式 (libtorrent 1.2.x+)
|
| 32 |
settings = lt.settings_pack()
|
| 33 |
settings.set_str(lt.settings_pack.listen_interfaces, '0.0.0.0:6881')
|
| 34 |
+
return lt.session(settings)
|
| 35 |
+
else:
|
| 36 |
+
# 旧版本配置方式 (libtorrent <1.2.x)
|
| 37 |
ses = lt.session()
|
| 38 |
+
if hasattr(ses, 'listen_on'):
|
| 39 |
+
ses.listen_on(6881, 6891)
|
| 40 |
+
return ses
|
| 41 |
|
| 42 |
+
def add_torrent_compat(ses, magnet_uri, download_path):
|
| 43 |
+
"""精确版本检测的种子添加方法"""
|
| 44 |
+
if hasattr(lt, 'parse_magnet_uri'):
|
| 45 |
+
# 新版本添加方式
|
| 46 |
+
params = lt.parse_magnet_uri(magnet_uri)
|
| 47 |
+
params.save_path = download_path
|
| 48 |
+
if hasattr(lt.storage_mode_t, 'storage_mode_sparse'):
|
| 49 |
+
params.storage_mode = lt.storage_mode_t.storage_mode_sparse
|
| 50 |
+
return ses.add_torrent(params)
|
| 51 |
+
else:
|
| 52 |
+
# 旧版本添加方式
|
| 53 |
+
return lt.add_magnet_uri(ses, magnet_uri, {
|
| 54 |
+
'save_path': download_path,
|
| 55 |
+
'storage_mode': lt.storage_mode_t(2)
|
| 56 |
+
})
|
| 57 |
|
| 58 |
def download_task(magnet_uri, download_path, status):
|
| 59 |
"""后台下载任务"""
|
|
|
|
| 61 |
ses = configure_session()
|
| 62 |
status.status = "解析磁力链接..."
|
| 63 |
|
| 64 |
+
# 添加种子
|
| 65 |
+
handle = add_torrent_compat(ses, magnet_uri, download_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
status.status = "获取元数据..."
|
| 67 |
|
| 68 |
# 等待元数据,带超时机制
|
| 69 |
+
timeout = 30
|
| 70 |
start = time.time()
|
| 71 |
while not handle.has_metadata():
|
| 72 |
if time.time() - start > timeout:
|
| 73 |
raise TimeoutError("获取元数据超时")
|
| 74 |
time.sleep(0.5)
|
| 75 |
+
if hasattr(ses, 'post_torrent_updates'):
|
| 76 |
+
ses.post_torrent_updates()
|
|
|
|
|
|
|
| 77 |
|
| 78 |
torrent_info = handle.get_torrent_info()
|
| 79 |
status.total_size = torrent_info.total_size()
|
|
|
|
| 83 |
start_time = time.time()
|
| 84 |
last_downloaded = 0
|
| 85 |
while not handle.is_seed():
|
| 86 |
+
if hasattr(ses, 'post_torrent_updates'):
|
| 87 |
+
ses.post_torrent_updates()
|
|
|
|
|
|
|
| 88 |
|
| 89 |
s = handle.status()
|
| 90 |
|
|
|
|
| 114 |
def main():
|
| 115 |
st.title("🕹️ 磁力链接下载器")
|
| 116 |
|
|
|
|
| 117 |
if 'download_status' not in st.session_state:
|
| 118 |
st.session_state.download_status = DownloadStatus()
|
| 119 |
|
|
|
|
| 120 |
with st.form("magnet_form"):
|
| 121 |
magnet_uri = st.text_input("磁力链接:", placeholder="magnet:?xt=urn:...")
|
| 122 |
submitted = st.form_submit_button("开始下载")
|
| 123 |
|
|
|
|
| 124 |
download_path = Path("downloads")
|
| 125 |
download_path.mkdir(exist_ok=True)
|
| 126 |
|
|
|
|
| 129 |
st.error("无效的磁力链接格式")
|
| 130 |
return
|
| 131 |
|
|
|
|
| 132 |
st.session_state.download_status = DownloadStatus()
|
| 133 |
thread = threading.Thread(
|
| 134 |
target=download_task,
|
|
|
|
| 136 |
)
|
| 137 |
thread.start()
|
| 138 |
|
|
|
|
| 139 |
status = st.session_state.download_status
|
| 140 |
status_container = st.empty()
|
| 141 |
|
| 142 |
while status.status not in ["下载完成", "错误"] and status.status != "等待中":
|
| 143 |
with status_container.container():
|
|
|
|
| 144 |
st.progress(status.progress, text=f"进度: {status.progress*100:.1f}%")
|
| 145 |
|
|
|
|
| 146 |
cols = st.columns(4)
|
| 147 |
cols[0].metric("总大小", human_readable_size(status.total_size) if status.total_size > 0 else "--")
|
| 148 |
cols[1].metric("已下载", human_readable_size(status.downloaded_size))
|
|
|
|
| 156 |
|
| 157 |
time.sleep(0.5)
|
| 158 |
|
|
|
|
| 159 |
if status.status == "下载完成":
|
| 160 |
st.success("🎉 下载完成!")
|
| 161 |
with open(status.file_path, "rb") as f:
|