File size: 4,011 Bytes
aba142b aafa08b aba142b aafa08b |
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
import streamlit as st
import ftplib
import libtorrent as lt
import time
import os
def ftp_upload_file(ftp, local_path, remote_path):
"""
Upload a file to the FTP server showing a progress bar.
"""
file_size = os.path.getsize(local_path)
uploaded_bytes = 0
progress_bar = st.progress(0)
# Callback for storbinary that is called after each block is sent.
def handle_block(block):
nonlocal uploaded_bytes
uploaded_bytes += len(block)
progress = int((uploaded_bytes / file_size) * 100)
progress_bar.progress(progress)
with open(local_path, 'rb') as f:
ftp.storbinary(f"STOR {remote_path}", f, blocksize=1024, callback=handle_block)
def download_torrent(magnet_link, download_folder):
"""
Download a torrent from a magnet link using libtorrent.
Shows a progress bar and status updates.
"""
st.write("Starting torrent session...")
ses = lt.session()
ses.listen_on(6881, 6891)
params = {
'save_path': download_folder,
'storage_mode': lt.storage_mode_t(2),
}
# Add the magnet link
handle = lt.add_magnet_uri(ses, magnet_link, params)
st.write("Fetching metadata...")
# Wait until metadata is received.
while not handle.has_metadata():
st.write("Waiting for metadata...")
time.sleep(1)
st.write("Metadata received. Starting download...")
progress_bar = st.progress(0)
status_text = st.empty()
# Loop until the torrent is complete (i.e. seeding)
while True:
s = handle.status()
progress = s.progress * 100
progress_bar.progress(int(progress))
status_text.text(
f"State: {s.state}, Progress: {progress:.2f}% | "
f"Down: {s.download_rate/1000:.2f} kB/s | "
f"Up: {s.upload_rate/1000:.2f} kB/s | "
f"Peers: {s.num_peers}"
)
if s.is_seeding:
break
time.sleep(1)
st.success("Torrent download complete!")
return download_folder
def main():
st.title("Magnet Link to FTP Downloader")
st.header("FTP Connection Details")
ftp_host = st.text_input("FTP Host", "ftp.example.com")
ftp_port = st.number_input("FTP Port", value=21, step=1)
ftp_username = st.text_input("FTP Username")
ftp_password = st.text_input("FTP Password", type="password")
st.header("Torrent Details")
magnet_link = st.text_area("Magnet Link", placeholder="magnet:?xt=urn:btih:...")
download_folder = st.text_input("Download Folder", "./downloads")
if st.button("Start Download & Upload"):
# --- Verify FTP connection ---
st.write("Verifying FTP connection...")
try:
ftp = ftplib.FTP()
ftp.connect(ftp_host, ftp_port, timeout=10)
ftp.login(ftp_username, ftp_password)
st.success("FTP connection successful!")
except Exception as e:
st.error(f"FTP connection failed: {e}")
return
# Create download folder if it doesn't exist.
if not os.path.exists(download_folder):
os.makedirs(download_folder)
# --- Start torrent download ---
downloaded_folder = download_torrent(magnet_link, download_folder)
# --- Upload downloaded files to FTP ---
st.write("Uploading downloaded files to FTP...")
for root, dirs, files in os.walk(downloaded_folder):
for file in files:
local_file = os.path.join(root, file)
remote_file = file # You can adjust the remote path as needed
st.write(f"Uploading {file}...")
try:
ftp_upload_file(ftp, local_file, remote_file)
st.success(f"Uploaded {file} successfully!")
except Exception as e:
st.error(f"Failed to upload {file}: {e}")
ftp.quit()
st.success("All files uploaded. FTP connection closed.")
if __name__ == '__main__':
main() |