Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,35 +1,53 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import
|
| 3 |
import time
|
| 4 |
import threading
|
| 5 |
import os
|
| 6 |
from pathlib import Path
|
| 7 |
|
| 8 |
-
# Aria2
|
| 9 |
-
|
| 10 |
-
|
|
|
|
| 11 |
|
| 12 |
# 初始化 aria2 连接
|
| 13 |
def init_aria2():
|
| 14 |
try:
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
# Session 状态初始化
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
if 'download_progress' not in st.session_state:
|
| 34 |
st.session_state.download_progress = 0
|
| 35 |
if 'downloading' not in st.session_state:
|
|
@@ -40,65 +58,41 @@ if 'download_filename' not in st.session_state:
|
|
| 40 |
st.session_state.download_filename = None
|
| 41 |
if 'error_message' not in st.session_state:
|
| 42 |
st.session_state.error_message = None
|
| 43 |
-
if 'aria2' not in st.session_state:
|
| 44 |
-
st.session_state.aria2 = init_aria2()
|
| 45 |
|
| 46 |
def download_torrent(magnet_link):
|
| 47 |
try:
|
| 48 |
# 添加下载任务
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
options=options
|
| 57 |
)
|
| 58 |
|
| 59 |
st.session_state.error_message = None
|
| 60 |
st.session_state.downloading = True
|
| 61 |
|
| 62 |
-
#
|
| 63 |
-
while
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
downloaded = int(status['completedLength'])
|
| 67 |
-
|
| 68 |
-
if total > 0:
|
| 69 |
-
progress = (downloaded / total) * 100
|
| 70 |
-
else:
|
| 71 |
-
progress = 0
|
| 72 |
-
|
| 73 |
-
st.session_state.download_progress = progress
|
| 74 |
|
| 75 |
-
#
|
| 76 |
-
if status['status'] == 'complete':
|
| 77 |
-
st.session_state.download_complete = True
|
| 78 |
-
st.session_state.download_filename = status['files'][0]['path']
|
| 79 |
-
break
|
| 80 |
-
|
| 81 |
-
# 超时处理(10分钟无进度)
|
| 82 |
-
if time.time() - int(status['updateTime']) > 600:
|
| 83 |
-
raise TimeoutError("下载超时")
|
| 84 |
-
|
| 85 |
-
# 用户取消
|
| 86 |
-
if not st.session_state.downloading:
|
| 87 |
-
st.session_state.aria2.remove(gid)
|
| 88 |
-
break
|
| 89 |
-
|
| 90 |
time.sleep(1)
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
except Exception as e:
|
| 95 |
st.session_state.error_message = f"下载失败: {str(e)}"
|
|
|
|
| 96 |
st.session_state.downloading = False
|
| 97 |
-
st.session_state.download_complete = False
|
| 98 |
|
| 99 |
-
def start_download_thread(magnet_link):
|
| 100 |
-
thread = threading.Thread(target=download_torrent, args=(magnet_link,))
|
| 101 |
-
thread.start()
|
| 102 |
|
| 103 |
# 界面布局
|
| 104 |
st.title("磁力链接下载器 🧲 (Aria2版)")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import aria2p # 改用更稳定的 aria2p 库
|
| 3 |
import time
|
| 4 |
import threading
|
| 5 |
import os
|
| 6 |
from pathlib import Path
|
| 7 |
|
| 8 |
+
# Aria2 配置
|
| 9 |
+
ARIA2_RPC_HOST = "http://localhost"
|
| 10 |
+
ARIA2_RPC_PORT = 6800
|
| 11 |
+
ARIA2_SECRET = "SECRET_KEY"
|
| 12 |
|
| 13 |
# 初始化 aria2 连接
|
| 14 |
def init_aria2():
|
| 15 |
try:
|
| 16 |
+
# 自动启动 aria2 后台进程
|
| 17 |
+
if not os.popen("pgrep aria2c").read():
|
| 18 |
+
download_dir = os.path.abspath("./downloads")
|
| 19 |
+
os.makedirs(download_dir, exist_ok=True)
|
| 20 |
+
|
| 21 |
+
cmd = [
|
| 22 |
+
"aria2c",
|
| 23 |
+
"--enable-rpc",
|
| 24 |
+
f"--rpc-listen-port={ARIA2_RPC_PORT}",
|
| 25 |
+
f"--rpc-secret={ARIA2_SECRET}",
|
| 26 |
+
f"--dir={download_dir}",
|
| 27 |
+
"--daemon=true",
|
| 28 |
+
"--max-concurrent-downloads=5",
|
| 29 |
+
"--split=16",
|
| 30 |
+
"--max-connection-per-server=16",
|
| 31 |
+
"--file-allocation=prealloc"
|
| 32 |
+
]
|
| 33 |
+
threading.Thread(target=lambda: os.system(" ".join(cmd))).start()
|
| 34 |
+
time.sleep(2)
|
| 35 |
|
| 36 |
+
# 初始化 API 客户端
|
| 37 |
+
return aria2p.Client(
|
| 38 |
+
host=ARIA2_RPC_HOST,
|
| 39 |
+
port=ARIA2_RPC_PORT,
|
| 40 |
+
secret=ARIA2_SECRET
|
| 41 |
+
)
|
| 42 |
+
except Exception as e:
|
| 43 |
+
st.error(f"初始化失败: {str(e)}")
|
| 44 |
+
raise
|
| 45 |
|
| 46 |
# Session 状态初始化
|
| 47 |
+
if 'aria_client' not in st.session_state:
|
| 48 |
+
st.session_state.aria_client = init_aria2()
|
| 49 |
+
if 'aria_api' not in st.session_state:
|
| 50 |
+
st.session_state.aria_api = aria2p.API(st.session_state.aria_client)
|
| 51 |
if 'download_progress' not in st.session_state:
|
| 52 |
st.session_state.download_progress = 0
|
| 53 |
if 'downloading' not in st.session_state:
|
|
|
|
| 58 |
st.session_state.download_filename = None
|
| 59 |
if 'error_message' not in st.session_state:
|
| 60 |
st.session_state.error_message = None
|
|
|
|
|
|
|
| 61 |
|
| 62 |
def download_torrent(magnet_link):
|
| 63 |
try:
|
| 64 |
# 添加下载任务
|
| 65 |
+
download = st.session_state.aria_api.add_magnet(
|
| 66 |
+
magnet_link,
|
| 67 |
+
options={
|
| 68 |
+
"max-download-limit": "0",
|
| 69 |
+
"seed-time": "0",
|
| 70 |
+
"bt-detach-seed-only": "true"
|
| 71 |
+
}
|
|
|
|
| 72 |
)
|
| 73 |
|
| 74 |
st.session_state.error_message = None
|
| 75 |
st.session_state.downloading = True
|
| 76 |
|
| 77 |
+
# 进度监控
|
| 78 |
+
while download.is_active:
|
| 79 |
+
download.update()
|
| 80 |
+
st.session_state.download_progress = download.progress * 100
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
+
# 自动刷新间隔
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
time.sleep(1)
|
| 84 |
+
|
| 85 |
+
if download.is_complete:
|
| 86 |
+
st.session_state.download_complete = True
|
| 87 |
+
st.session_state.download_filename = download.files[0].path
|
| 88 |
+
else:
|
| 89 |
+
raise Exception(download.error_message)
|
| 90 |
|
| 91 |
except Exception as e:
|
| 92 |
st.session_state.error_message = f"下载失败: {str(e)}"
|
| 93 |
+
finally:
|
| 94 |
st.session_state.downloading = False
|
|
|
|
| 95 |
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
# 界面布局
|
| 98 |
st.title("磁力链接下载器 🧲 (Aria2版)")
|