Google Colab /Python Torrent Downloader Needs Help Me to add progress bar

826 Views Asked by At

The downloader given by the code below works. It lets me select the files I want by inputting the file index numbers. But when it is downloading it just keeps printing the percentage and name. Instead of that I would like to have a percentage bar with the file name.

from google.colab import drive
drive.mount('/content/drive')

!python -m pip install lbry-libtorrent

!apt install python3-libtorrent

!pip install tqdm


import libtorrent as lt

# Mount Google Drive
from google.colab import drive
drive.mount('/content/drive')

# Set save path
save_path = "/content/drive/My Drive/TorrDownloads"

# Set magnet link
magnet_link = "<magnetlink>"



# Create session and add magnet link
ses = lt.session()
params = {
    'save_path': save_path,
  
}
handle = lt.add_magnet_uri(ses, magnet_link, params)

# Wait for metadata to download
print("Downloading metadata...")
while not handle.has_metadata():
    time.sleep(1)

# Get torrent info
torrent_info = handle.get_torrent_info()

# Display list of files and their sizes
print("Select the files you want to download:")
for i, f in enumerate(torrent_info.files()):
    print(f"{i+1}. {f.path} ({f.size/1024/1024:.2f} MB)")

# Create list of selected files
selected_files = []
while True:
    selection = input("Enter file numbers to download (comma-separated), or 'done' to start downloading: ")
    if selection == "done":
        break
    try:
        file_nums = [int(x)-1 for x in selection.split(",")]
        for file_num in file_nums:
            selected_files.append(file_num)
    except ValueError:
        print("Invalid input, try again")

# Download selected files
print("Downloading selected files...")
for i, f in enumerate(torrent_info.files()):
    if i in selected_files:
        print(f"Downloading {f.path} ({f.size/1024/1024:.2f} MB)...")
        handle.file_priority(i, 1)
    else:
        handle.file_priority(i, 0)

# Start download
handle.resume()
print("Downloading...")
while handle.status().state != lt.torrent_status.seeding:
    s = handle.status()
    state = ['queued', 'checking', 'downloading metadata', \
             'downloading', 'finished', 'seeding', 'allocating', 'checking fastresume']
    print('%.2f%% complete (down: %.1f kb/s up: %.1f kB/s peers: %d) %s' % \
          (s.progress * 100, s.download_rate / 1000, s.upload_rate / 1000, s.num_peers, state[s.state]))
    time.sleep(1)
print("Download complete!")
0

There are 0 best solutions below