如何在Python中下载Torrent文件?代码实现示例

2021年11月16日21:26:07 发表评论 2,490 次浏览

本文教你使用 Python 包装器为 qBittorrent Web API 自动下载、暂停和处理 Python 中的 Torrent 文件,包括Python下载Torrent文件示例

你是否曾经想以编程方式下载 torrent 中的文件?好吧,在本教程中,你将学习如何使用 Python 下载 Torrent 文件。

我们将在这里使用qBittorrent,这是因为它有一个很酷的 Python 包装器,可以为我们简化一切。

Python如何下载Torrent文件?首先,你需要为你的操作系统下载并安装 qBittorent 官方客户端,然后使用以下命令安装 Python 包装器模块:

pip3 install python-qbittorrent

如何在Python中下载Torrent文件?现在在我们深入代码之前,我们需要进行一些配置,在安装 qBittorent 客户端后,你需要使用以下步骤启用qBittorrent Web UI

  • 设置好一切后,启动 qBittorrent。在菜单栏上,转到工具 > 选项qBittorrent WEB UI。
  • 当新窗口出现时,选择Web UI选项。
  • 选中“Web 用户界面(远程控制)”复选框。
  • 你可以选择一个端口(默认为8080)。
  • 设置用户名和密码(默认为admin:adminadmin)。

下图应该让一切都清楚:

如何在Python中下载Torrent文件?代码实现示例现在我们已经启用了 Web UI,你可以转到浏览器并使用地址“127.0.0.1:8080”查看 qBittorrent 网络版本。你将看到一个小的登录页面,如下所示:

如何在Python中下载Torrent文件?代码实现示例

将你设置的凭据放入配置中,然后登录,现在你应该可以看到 qBittorrent Web UI:

如何在Python中下载Torrent文件?代码实现示例

Python下载Torrent文件示例解释:如果你在这里,那么恭喜你!你现在可以使用 Python 下载 torrent 文件,打开一个新的 Python 文件(或交互式 Python shell),并导入 qBittorrent 模块:

from qbittorrent import Client

现在让我们连接并登录到 Web UI:

# connect to the qbittorent Web UI
qb = Client("http://127.0.0.1:8080/")

# put the credentials (as you configured)
qb.login("admin", "adminadmin")

Python如何下载Torrent文件?我为本教程选择了这个 torrent 文件,请随意使用你想要的任何 torrent 文件(只需将其放在你当前的工作目录中并更改名称):

# open the torrent file of the file you wanna download
torrent_file = open("debian-10.2.0-amd64-netinst.iso.torrent", "rb")

注意:如果你不确定open()函数在做什么,请查看本教程。

让我们开始下载:

# start downloading
qb.download_from_file(torrent_file)

如果你在交互式窗口中逐个单元执行此操作,你将立即看到一个新的 torrent 文件出现在 Web UI 和 qBittorrent 桌面客户端中,如下图所示:

如何在Python中下载Torrent文件?代码实现示例

太棒了,你可以使用savepath 参数将结果文件保存到你实际想要的路径:

# you can specify the save path for downloads
qb.download_from_file(torrent_file, savepath="/the/path/you/want/to/save")

如何在Python中下载Torrent文件?你还可以使用download_from_link()方法,该方法采用你要下载的磁铁 URL:

# this magnet is not valid, replace with yours
magnet_link = "magnet:?xt=urn:btih:e334ab9ddd91c10938a7....."
qb.download_from_link(magnet_link)

你还可以做各种事情,例如,让我们暂停客户端中的所有种子:

# pause all downloads
qb.pause_all()

或者你可以恢复它们:

# resume them
qb.resume_all()

Python下载Torrent文件示例 - 甚至列出它们并显示一些有用的信息:

def get_size_format(b, factor=1024, suffix="B"):
    """
    Scale bytes to its proper byte format
    e.g:
        1253656 => '1.20MB'
        1253656678 => '1.17GB'
    """
    for unit in ["", "K", "M", "G", "T", "P", "E", "Z"]:
        if b < factor:
            return f"{b:.2f}{unit}{suffix}"
        b /= factor
    return f"{b:.2f}Y{suffix}"

# return list of torrents
torrents = qb.torrents()

for torrent in torrents:
    print("Torrent name:", torrent["name"])
    print("hash:", torrent["hash"])
    print("Seeds:", torrent["num_seeds"])
    print("File size:", get_size_format(torrent["total_size"]))
    print("Download speed:", get_size_format(torrent["dlspeed"]) + "/s")

这是我的输出:

Torrent name: debian-10.2.0-amd64-netinst.iso
hash: 86d4c80024a469be4c50bc5a102cf71780310074
Seeds: 70
File size: 335.00MB
Download speed: 606.15KB/s

Python如何下载Torrent文件?你还可以使用它们的哈希值暂停和恢复特定的 torrent 文件,这个包装器提供了丰富的有用方法,请查看他们的完整 API 方法文档GitHub 存储库

好的,这就是本教程的内容,这将使你接受许多很酷的挑战,这是一个示例挑战:

  • 获取所有网站链接并仅提取 torrent 文件,然后仅下载带有.torrent扩展名的文件。之后,启动它们以在 qBittorrent 中下载,非常整洁对吗?去吧。

顺便说一句,如果你希望为 uTorrent 客户端使用 Python 包装器,这个存储库可能会有所帮助。

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: