erfyhersr commited on
Commit
cdb9913
·
verified ·
1 Parent(s): ea06b30

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -31
app.py CHANGED
@@ -6,7 +6,7 @@ from torrentp import TorrentDownloader
6
 
7
  def ftp_upload_file(ftp, local_path, remote_path):
8
  """
9
- Upload a file to the FTP server, showing a progress bar.
10
  """
11
  file_size = os.path.getsize(local_path)
12
  uploaded_bytes = 0
@@ -21,13 +21,16 @@ def ftp_upload_file(ftp, local_path, remote_path):
21
  with open(local_path, 'rb') as f:
22
  ftp.storbinary(f"STOR {remote_path}", f, blocksize=1024, callback=handle_block)
23
 
24
- def start_torrent_download(link, download_folder, download_speed=0, upload_speed=0, port=6881):
25
  """
26
- Create a TorrentDownloader from torrentp and run its asynchronous download method.
 
 
 
 
27
  """
28
- torrent_file = TorrentDownloader(link, download_folder, port=port)
29
- # This will block until the asynchronous download completes.
30
- asyncio.run(torrent_file.start_download(download_speed=download_speed, upload_speed=upload_speed))
31
  return download_folder
32
 
33
  def main():
@@ -42,44 +45,34 @@ def main():
42
  st.header("Torrent Details")
43
  torrent_link = st.text_area("Torrent Link (magnet or .torrent file)", placeholder="magnet:?xt=urn:btih:...")
44
  download_folder = st.text_input("Download Folder", "./downloads")
45
- download_speed = st.number_input("Download Speed (kB/s, 0 for unlimited)", value=0, step=1)
46
- upload_speed = st.number_input("Upload Speed (kB/s, 0 for unlimited)", value=0, step=1)
47
- port_value = st.number_input("Torrent Port", value=6881, step=1)
48
 
49
  if st.button("Start Download & Upload"):
50
- # --- Verify FTP connection ---
51
- st.write("Verifying FTP connection...")
52
- try:
53
- ftp = ftplib.FTP()
54
- ftp.connect(ftp_host, ftp_port, timeout=10)
55
- ftp.login(ftp_username, ftp_password)
56
- st.success("FTP connection successful!")
57
- except Exception as e:
58
- st.error(f"FTP connection failed: {e}")
59
- return
60
-
61
- # Ensure the download folder exists.
62
- if not os.path.exists(download_folder):
63
- os.makedirs(download_folder)
64
-
65
  # --- Start torrent download ---
66
  st.write("Starting torrent download...")
 
 
67
  with st.spinner("Downloading torrent..."):
68
  try:
69
- start_torrent_download(torrent_link, download_folder, download_speed, upload_speed, port=port_value)
70
  except Exception as e:
71
  st.error(f"Torrent download failed: {e}")
72
- ftp.quit()
73
  return
74
  st.success("Torrent download complete!")
75
 
76
- # --- Upload downloaded files to FTP ---
77
- st.write("Uploading downloaded files to FTP...")
 
 
 
 
 
 
 
 
78
  for root, dirs, files in os.walk(download_folder):
79
  for file in files:
80
  local_file = os.path.join(root, file)
81
- # Adjust remote_file if you want to preserve directory structure.
82
- remote_file = file
83
  st.write(f"Uploading {file}...")
84
  try:
85
  ftp_upload_file(ftp, local_file, remote_file)
@@ -91,4 +84,4 @@ def main():
91
  st.success("All files uploaded. FTP connection closed.")
92
 
93
  if __name__ == '__main__':
94
- main()
 
6
 
7
  def ftp_upload_file(ftp, local_path, remote_path):
8
  """
9
+ Upload a file to the FTP server with a progress bar.
10
  """
11
  file_size = os.path.getsize(local_path)
12
  uploaded_bytes = 0
 
21
  with open(local_path, 'rb') as f:
22
  ftp.storbinary(f"STOR {remote_path}", f, blocksize=1024, callback=handle_block)
23
 
24
+ def start_torrent_download(link, download_folder):
25
  """
26
+ Start the torrent download using torrentp with default settings.
27
+ Defaults:
28
+ - Port: 6881
29
+ - Download speed: 0 (unlimited)
30
+ - Upload speed: 0 (unlimited)
31
  """
32
+ torrent_file = TorrentDownloader(link, download_folder, port=6881)
33
+ asyncio.run(torrent_file.start_download(download_speed=0, upload_speed=0))
 
34
  return download_folder
35
 
36
  def main():
 
45
  st.header("Torrent Details")
46
  torrent_link = st.text_area("Torrent Link (magnet or .torrent file)", placeholder="magnet:?xt=urn:btih:...")
47
  download_folder = st.text_input("Download Folder", "./downloads")
 
 
 
48
 
49
  if st.button("Start Download & Upload"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  # --- Start torrent download ---
51
  st.write("Starting torrent download...")
52
+ if not os.path.exists(download_folder):
53
+ os.makedirs(download_folder)
54
  with st.spinner("Downloading torrent..."):
55
  try:
56
+ start_torrent_download(torrent_link, download_folder)
57
  except Exception as e:
58
  st.error(f"Torrent download failed: {e}")
 
59
  return
60
  st.success("Torrent download complete!")
61
 
62
+ # --- Connect to FTP and upload files ---
63
+ st.write("Connecting to FTP and uploading files...")
64
+ try:
65
+ ftp = ftplib.FTP()
66
+ ftp.connect(ftp_host, ftp_port, timeout=10)
67
+ ftp.login(ftp_username, ftp_password)
68
+ except Exception as e:
69
+ st.error(f"FTP connection failed: {e}")
70
+ return
71
+
72
  for root, dirs, files in os.walk(download_folder):
73
  for file in files:
74
  local_file = os.path.join(root, file)
75
+ remote_file = file # Modify as needed to set the remote path
 
76
  st.write(f"Uploading {file}...")
77
  try:
78
  ftp_upload_file(ftp, local_file, remote_file)
 
84
  st.success("All files uploaded. FTP connection closed.")
85
 
86
  if __name__ == '__main__':
87
+ main()