10 lines of Python code to batch download torrents using magnet links, your vacation will never be boring again!

Release: 2023-08-10 16:19:29
forward
2661 people have browsed it


This article mainly introduces the use of Python to implement magnet links to batch download movies.

A few days ago, a friend sent me a bunch of magnet links, saying they were some relatively good movies that I could watch at my leisure in the next two days. But there are too many links. It would be exhausting to add and download one by one! So I decided to try some automated download methods.

At first I tried to find the API of some existing download tools, but unfortunately, I couldn't find it, but I found a more interesting library pypiwin32. This library is used to execute some windows commands. It's also a great library. I have used it to process excel before. At this time, I plan to use win32 to automatically drive Thunder to realize batch automatic downloading of seed connections.

Regarding the pypiwin32 library, I noticed the Dispatch function. Using this function should be able to directly drive Thunder. This function is used to connect to the fixed software. To use this program, you only need to obtain the name of the installation software registered on this computer. In order to simplify the process of finding the registered name, I wrote the registered name of Xunlei directly here:

ThunderAgent.Agent.1
Copy after login

My local registered name is the one above. Of course, if the above does not work, you can try the following: <br/>

ThunderAgent.Agent64.1
Copy after login

There should be no problem with the current version of Thunder 9 or Thunder 10. <br/>

Then let’s start our automation journey! <br/>

First we need to download the pypiwin32 library, and we use pip to install it directly. <br/>

pip install pypiwin32

If the download speed is too slow, you can specify the Tsinghua source to download: <br/>

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pypiwin32

10 lines of Python code to batch download torrents using magnet links, your vacation will never be boring again!

Now we can use this module directly. <br/>

We mainly use the Dispatch function and put the Thunder registration name into this function. to complete Thunder loading. <br/>

<br/>
Copy after login
from win32com.client import Dispatch
thunder = Dispatch(&#39;ThunderAgent.Agent.1&#39;)
Copy after login

After successfully starting Thunder, we can add tasks to Thunder.

At this time we use the AddTask method. <br/>

Three parameters need to be passed in to the AddTask method: <br/>

thunder.AddTask(磁力连接,
                下载保存的文件名,
                保存路径
                )
Copy after login

After that, we only need to use the CommitTasks function to submit the task.<br/>

The complete code is as follows: <br/>

from win32com.client import Dispatch

thunder = Dispatch(&#39;ThunderAgent.Agent.1&#39;)
url = "ftp://ygdy8:ygdy8@yg39.dydytt.net:3010" \
"/阳光电影www.ygdy8.com.追龙番外之十亿探长" \
".HD.1080p.国语中字.mkv"
filename = "追龙番外之十亿探长.mkv"
thunder.AddTask(url, filename, r"C:\迅雷下载")
thunder.CommitTasks()
print("任务已建立,开始下载:{}....".format(filename))
Copy after login

That’s all the code. However, for Thunder, we still need to make some settings:

10 lines of Python code to batch download torrents using magnet links, your vacation will never be boring again!

#We need to check the one-click download and select immediate download in the default download method.

That’s it, the following is the effect video: <br/>

Next we create multiple tasks for downloading, we will take Let's use the resources of "Love Apartment 5" as a demonstration: <br/>

10 lines of Python code to batch download torrents using magnet links, your vacation will never be boring again!

#There are 36 episodes in total. We can see some rules of the URL and can get all the magnetism of the 36 episodes. Connect, and then add it to the task in batches: <br/>

for i in range(1, 37):
if i < 10:
        i = "0{}".format(i)
    url = "ftp://ygdy8:ygdy8@yg76.dydytt.net:5919/" \
"[阳光电影-www.ygdy8.com]爱情公寓5-{}.mp4"\
        .format(i)
    filename = url.split(&#39;]&#39;)[1]
    thunder.AddTask(url, filename, r"C:\迅雷下载")
print("下载任务建立:{}....".format(filename))
    thunder.CommitTasks()
Copy after login

At this point, our batch download task has been established, and automatic batch downloading can be performed. The effect video is as follows:

如果朋友给你的磁力连接是一个txt文件,那我们还可以读取文件每一行的磁力连接进行下载:<br/>

10 lines of Python code to batch download torrents using magnet links, your vacation will never be boring again!

import csv
with open("爱情公寓资源.txt", &#39;r&#39;, encoding=&#39;utf-8&#39;) as f:
    reader = csv.reader(f)
    films = [i[0] for i in reader]
thunder = Dispatch(&#39;ThunderAgent.Agent.1&#39;)
for film in films:
    thunder.AddTask(film, film[-12:], r"C:\迅雷下载")
    thunder.CommitTasks()
Copy after login

怎么样是不是很棒?你也来试试吧!

The above is the detailed content of 10 lines of Python code to batch download torrents using magnet links, your vacation will never be boring again!. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:Python当打之年
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!