mx / app.py
Ethscriptions's picture
Update app.py
22ba115
raw
history blame
1.24 kB
import libtorrent as lt
import time
import streamlit as st
import os
st.title('磁力链接 BT 下载器')
magnet_link = st.text_input('请输入磁力链接:')
if magnet_link:
ses = lt.session()
h = lt.add_magnet_uri(ses, magnet_link, {'save_path': './'})
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('下载完成')
# 获取下载的文件名
torrent_info = h.get_torrent_info()
files = torrent_info.files()
filepath = f"./{files.file_path(0)}"
# 提供一个下载按钮给用户下载文件
with open(filepath, "rb") as file:
btn = st.download_button(
label="下载文件",
data=file,
file_name=os.path.basename(filepath),
mime="application/octet-stream",
)