File size: 1,239 Bytes
4f989ff
 
 
22ba115
4f989ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22ba115
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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",
        )