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()