Skip to content

Commit 268574b

Browse files
committedJan 18, 2020
added torrent downloader tutorial
1 parent 7639602 commit 268574b

File tree

5 files changed

+59
-1
lines changed

5 files changed

+59
-1
lines changed
 

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
5252
- [How to Make a Screen Recorder in Python](https://www.thepythoncode.com/article/make-screen-recorder-python). ([code](general/screen-recorder))
5353
- [How to Generate and Read QR Code in Python](https://www.thepythoncode.com/article/generate-read-qr-code-python). ([code](general/generating-reading-qrcode))
5454
- [How to Download Files in Python](https://www.thepythoncode.com/article/download-files-python). ([code](general/file-downloader))
55-
5655
- [How to Extract PDF Tables in Python](https://www.thepythoncode.com/article/extract-pdf-tables-in-python-camelot). ([code](general/pdf-table-extractor))
5756
- [How to Automate your VPS or Dedicated Server Management in Python](https://www.thepythoncode.com/article/automate-veesp-server-management-in-python). ([code](general/automating-server-management))
5857
- [How to Execute BASH Commands in a Remote Machine in Python](https://www.thepythoncode.com/article/executing-bash-commands-remotely-in-python). ([code](general/execute-ssh-commands))
5958
- [How to Convert Python Files into Executables](https://www.thepythoncode.com/article/building-python-files-into-stand-alone-executables-using-pyinstaller)
6059
- [How to Get the Size of Directories in Python](https://www.thepythoncode.com/article/get-directory-size-in-bytes-using-python). ([code](general/calculate-directory-size))
60+
- [How to Download Torrent Files in Python](https://www.thepythoncode.com/article/download-torrent-files-in-python). ([code](general/torrent-downloader))
6161

6262
- ### [Web Scraping](https://www.thepythoncode.com/topic/web-scraping)
6363
- [How to Access Wikipedia in Python](https://www.thepythoncode.com/article/access-wikipedia-python). ([code](web-scraping/wikipedia-extractor))

‎general/torrent-downloader/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# [How to Download Torrent Files in Python](https://www.thepythoncode.com/article/download-torrent-files-in-python)
2+
To run this:
3+
- Install [qBittorrent client](https://www.qbittorrent.org/download.php) for your operating system.
4+
- `pip3 install -r requirements.txt`
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python-qbittorrent
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from qbittorrent import Client
2+
3+
# connect to the qbittorent Web UI
4+
qb = Client("http://127.0.0.1:8080/")
5+
6+
# put the credentials (as you configured)
7+
qb.login("admin", "adminadmin")
8+
9+
# open the torrent file of the file you wanna download
10+
torrent_file = open("debian-10.2.0-amd64-netinst.iso.torrent", "rb")
11+
# start downloading
12+
qb.download_from_file(torrent_file)
13+
# this magnet is not valid, replace with yours
14+
# magnet_link = "magnet:?xt=urn:btih:e334ab9ddd91c10938a7....."
15+
# qb.download_from_link(magnet_link)
16+
# you can specify the save path for downloads
17+
# qb.download_from_file(torrent_file, savepath="/the/path/you/want/to/save")
18+
19+
# pause all downloads
20+
qb.pause_all()
21+
22+
# resume them
23+
qb.resume_all()
24+
25+
26+
def get_size_format(b, factor=1024, suffix="B"):
27+
"""
28+
Scale bytes to its proper byte format
29+
e.g:
30+
1253656 => '1.20MB'
31+
1253656678 => '1.17GB'
32+
"""
33+
for unit in ["", "K", "M", "G", "T", "P", "E", "Z"]:
34+
if b < factor:
35+
return f"{b:.2f}{unit}{suffix}"
36+
b /= factor
37+
return f"{b:.2f}Y{suffix}"
38+
39+
# return list of torrents
40+
torrents = qb.torrents()
41+
42+
for torrent in torrents:
43+
print("Torrent name:", torrent["name"])
44+
print("hash:", torrent["hash"])
45+
print("Seeds:", torrent["num_seeds"])
46+
print("File size:", get_size_format(torrent["total_size"]))
47+
print("Download speed:", get_size_format(torrent["dlspeed"]) + "/s")
48+
49+
# Torrent name: debian-10.2.0-amd64-netinst.iso
50+
# hash: 86d4c80024a469be4c50bc5a102cf71780310074
51+
# Seeds: 70
52+
# File size: 335.00MB
53+
# Download speed: 606.15KB/s

0 commit comments

Comments
 (0)
Please sign in to comment.