Spaces:
Build error
Build error
| import libtorrent as lt | |
| import time | |
| import streamlit as st | |
| import os | |
| import shutil | |
| st.title('磁力链接 BT 下载器') | |
| magnet_link = st.text_input('请输入磁力链接:') | |
| if magnet_link: | |
| # 创建一个会话并配置更多的设置以优化下载速度 | |
| ses = lt.session() | |
| # 更具体的优化设置 | |
| settings = { | |
| 'connections_limit': 500, # 最大连接数 | |
| 'download_rate_limit': 0, # 下载速率限制 (0 表示无限制) | |
| 'upload_rate_limit': 0, # 上传速率限制 (0 表示无限制) | |
| 'active_downloads': 10, # 同时进行的活跃下载数 | |
| 'active_limit': 20, # 允许的活跃 torrent 数 | |
| 'active_seeds': 10, # 同时进行的活跃做种数 | |
| 'alert_mask': 0, # 控制 libtorrent 产生哪些警告信息 | |
| 'file_pool_size': 512, # 文件池大小限制 | |
| # ... 其他设置 | |
| } | |
| ses.apply_settings(settings) | |
| # 添加磁力链接以开始下载 | |
| h = lt.add_magnet_uri(ses, magnet_link, {'save_path': './downloads'}) | |
| st.write('开始下载...') | |
| while not h.is_seed(): | |
| s = h.status() | |
| state_str = [ | |
| "queued", | |
| "checking", | |
| "downloading metadata", | |
| "downloading", | |
| "finished", | |
| "seeding", | |
| "allocating", | |
| "checking fastresume", | |
| ] | |
| st.write( | |
| f"进度: {s.progress * 100:.2f}%,速度: {s.download_rate / 1000:.2f} KB/s,状态:{state_str[s.state]}" | |
| ) | |
| time.sleep(1) | |
| st.write('下载完成') | |
| # 获取下载的文件路径 | |
| save_path = './downloads' | |
| # 压缩文件夹为 zip 文件 | |
| shutil.make_archive('downloads', 'zip', save_path) | |
| # 提供一个下载按钮给用户下载 zip 文件 | |
| with open('downloads.zip', 'rb') as file: | |
| st.download_button( | |
| label='下载文件', | |
| data=file, | |
| file_name='downloads.zip', | |
| mime='application/zip' | |
| ) | |