Ethscriptions commited on
Commit
5c118c1
·
verified ·
1 Parent(s): 0fe959b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -62
app.py CHANGED
@@ -1,35 +1,53 @@
1
  import streamlit as st
2
- import pyaria2
3
  import time
4
  import threading
5
  import os
6
  from pathlib import Path
7
 
8
- # Aria2 RPC 配置
9
- ARIA2_RPC_URL = "http://localhost:6800/rpc"
10
- ARIA2_SECRET = "SECRET_KEY" # 建议修改为随机字符串
 
11
 
12
  # 初始化 aria2 连接
13
  def init_aria2():
14
  try:
15
- aria2 = pyaria2.Aria2(ARIA2_RPC_URL, secret=ARIA2_SECRET)
16
- aria2.getVersion() # 测试连接
17
- return aria2
18
- except:
19
- # 自动启动 aria2 进程(后台模式)
20
- aria2_dir = os.path.expanduser("~/.aria2")
21
- os.makedirs(aria2_dir, exist_ok=True)
22
-
23
- cmd = f"aria2c --enable-rpc --rpc-listen-all=true --rpc-secret={ARIA2_SECRET} "
24
- cmd += f"--dir={os.path.abspath('./downloads')} --daemon=true "
25
- cmd += "--max-concurrent-downloads=3 --max-connection-per-server=16 "
26
- cmd += "--split=16 --min-split-size=1M --file-allocation=none"
 
 
 
 
 
 
 
27
 
28
- threading.Thread(target=lambda: os.system(cmd)).start()
29
- time.sleep(2) # 等待服务启动
30
- return pyaria2.Aria2(ARIA2_RPC_URL, secret=ARIA2_SECRET)
 
 
 
 
 
 
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
- options = {
50
- "max-download-limit": "0", # 不限速
51
- "seed-time": "0", # 下载完成后不做种
52
- "bt-detach-seed-only": "true"
53
- }
54
- gid = st.session_state.aria2.addUri(
55
- [magnet_link],
56
- options=options
57
  )
58
 
59
  st.session_state.error_message = None
60
  st.session_state.downloading = True
61
 
62
- # 进度监控循环
63
- while True:
64
- status = st.session_state.aria2.tellStatus(gid)
65
- total = int(status['totalLength'])
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
- st.session_state.downloading = False
 
 
 
 
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版)")