Born_Loard
Junior Member
- Sep 5, 2022
- 175
- 78
Downloading torrent using Utorrent or Bittorrent is quite easy. But for any reason if you want to download torrent using codding then this thread is for you.
In this post I am using google colab to download torrent file directly to my google drive so downloading speed will be faster.
So first we will connect my colab notebook to google drive.
Code 01 --------------------------------------------------------------------------------------------------
from google.colab import drive
drive.mount('/content/drive')
--------------------------------------------------------------------------------------------------------------
After this we will install “libtorrent” library which will help us in downloading torrent.
Code 02 --------------------------------------------------------------------------------------------------
!python -m pip install lbry-libtorrent
!apt install python3-libtorrent
import libtorrent as lt
ses = lt.session()
ses.listen_on(6881, 6891)
downloads = []
--------------------------------------------------------------------------------------------------------------
Now, If you want to download using torrent file then you can use below code. It will take a torrent file as input. You can add multiple files.
Code 03 --------------------------------------------------------------------------------------------------
from google.colab import files
source = files.upload()
params = {
"save_path": "/content/drive/My Drive/Torrent",
"ti": lt.torrent_info(list(source.keys())[0]),
}
downloads.append(ses.add_torrent(params))
--------------------------------------------------------------------------------------------------------------
And If you want to download using magnet URL then use below code. This will take magnet URL as input. You can enter multiple magnet URLs. After adding URLs use “Exit” to exit the cell.
Code 04 --------------------------------------------------------------------------------------------------
params = {"save_path": "/content/drive/My Drive/Torrent"}
while True:
magnet_link = input("Enter Magnet Link Or Type Exit: ")
if magnet_link.lower() == "exit":
break
downloads.append(
lt.add_magnet_uri(ses, magnet_link, params)
)
--------------------------------------------------------------------------------------------------------------
Now, finally its time to download the file. So just use the below code and It will start downloading the torrent file. Don’t forget to change the destination path to wherever you want to download the file.
Code 05 --------------------------------------------------------------------------------------------------
import time
from IPython.display import display
import ipywidgets as widgets
state_str = [
"queued",
"checking",
"downloading metadata",
"downloading",
"finished",
"seeding",
"allocating",
"checking fastresume",
]
layout = widgets.Layout(width="auto")
style = {"description_width": "initial"}
download_bars = [
widgets.FloatSlider(
step=0.01, disabled=True, layout=layout, style=style
)
for _ in downloads
]
display(*download_bars)
while downloads:
next_shift = 0
for index, download in enumerate(downloads[:]):
bar = download_bars[index + next_shift]
if not download.is_seed():
s = download.status()
bar.description = " ".join(
[
download.name(),
str(s.download_rate / 1000),
"kB/s",
state_str[s.state],
]
)
bar.value = s.progress * 100
else:
next_shift -= 1
ses.remove_torrent(download)
downloads.remove(download)
bar.close()
download_bars.remove(bar)
print(download.name(), "complete")
time.sleep(1)
--------------------------------------------------------------------------------------------------------------
So, this is it. You can also download file to your local device but it will consume more data and speed will be limited to your internet connection speed.
In this post I am using google colab to download torrent file directly to my google drive so downloading speed will be faster.
So first we will connect my colab notebook to google drive.
Code 01 --------------------------------------------------------------------------------------------------
from google.colab import drive
drive.mount('/content/drive')
--------------------------------------------------------------------------------------------------------------
After this we will install “libtorrent” library which will help us in downloading torrent.
Code 02 --------------------------------------------------------------------------------------------------
!python -m pip install lbry-libtorrent
!apt install python3-libtorrent
import libtorrent as lt
ses = lt.session()
ses.listen_on(6881, 6891)
downloads = []
--------------------------------------------------------------------------------------------------------------
Now, If you want to download using torrent file then you can use below code. It will take a torrent file as input. You can add multiple files.
Code 03 --------------------------------------------------------------------------------------------------
from google.colab import files
source = files.upload()
params = {
"save_path": "/content/drive/My Drive/Torrent",
"ti": lt.torrent_info(list(source.keys())[0]),
}
downloads.append(ses.add_torrent(params))
--------------------------------------------------------------------------------------------------------------
And If you want to download using magnet URL then use below code. This will take magnet URL as input. You can enter multiple magnet URLs. After adding URLs use “Exit” to exit the cell.
Code 04 --------------------------------------------------------------------------------------------------
params = {"save_path": "/content/drive/My Drive/Torrent"}
while True:
magnet_link = input("Enter Magnet Link Or Type Exit: ")
if magnet_link.lower() == "exit":
break
downloads.append(
lt.add_magnet_uri(ses, magnet_link, params)
)
--------------------------------------------------------------------------------------------------------------
Now, finally its time to download the file. So just use the below code and It will start downloading the torrent file. Don’t forget to change the destination path to wherever you want to download the file.
Code 05 --------------------------------------------------------------------------------------------------
import time
from IPython.display import display
import ipywidgets as widgets
state_str = [
"queued",
"checking",
"downloading metadata",
"downloading",
"finished",
"seeding",
"allocating",
"checking fastresume",
]
layout = widgets.Layout(width="auto")
style = {"description_width": "initial"}
download_bars = [
widgets.FloatSlider(
step=0.01, disabled=True, layout=layout, style=style
)
for _ in downloads
]
display(*download_bars)
while downloads:
next_shift = 0
for index, download in enumerate(downloads[:]):
bar = download_bars[index + next_shift]
if not download.is_seed():
s = download.status()
bar.description = " ".join(
[
download.name(),
str(s.download_rate / 1000),
"kB/s",
state_str[s.state],
]
)
bar.value = s.progress * 100
else:
next_shift -= 1
ses.remove_torrent(download)
downloads.remove(download)
bar.close()
download_bars.remove(bar)
print(download.name(), "complete")
time.sleep(1)
--------------------------------------------------------------------------------------------------------------
So, this is it. You can also download file to your local device but it will consume more data and speed will be limited to your internet connection speed.