Home > Article > Backend Development > 10 lines of Python code to batch download torrents using magnet links, your vacation will never be boring again!
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
My local registered name is the one above. Of course, if the above does not work, you can try the following:
ThunderAgent.Agent64.1
There should be no problem with the current version of Thunder 9 or Thunder 10.
Then let’s start our automation journey!
First we need to download the pypiwin32 library, and we use pip to install it directly.
pip install pypiwin32
If the download speed is too slow, you can specify the Tsinghua source to download:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pypiwin32
Now we can use this module directly.
We mainly use the Dispatch function and put the Thunder registration name into this function. to complete Thunder loading.
<br/>
from win32com.client import Dispatch thunder = Dispatch('ThunderAgent.Agent.1')
After successfully starting Thunder, we can add tasks to Thunder.
At this time we use the AddTask method.
Three parameters need to be passed in to the AddTask method:
thunder.AddTask(磁力连接, 下载保存的文件名, 保存路径 )
After that, we only need to use the CommitTasks function to submit the task.
The complete code is as follows:
from win32com.client import Dispatch thunder = Dispatch('ThunderAgent.Agent.1') 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))
That’s all the code. However, for Thunder, we still need to make some settings:
#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:
Next we create multiple tasks for downloading, we will take Let's use the resources of "Love Apartment 5" as a demonstration:
#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:
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(']')[1] thunder.AddTask(url, filename, r"C:\迅雷下载") print("下载任务建立:{}....".format(filename)) thunder.CommitTasks()
At this point, our batch download task has been established, and automatic batch downloading can be performed. The effect video is as follows:
如果朋友给你的磁力连接是一个txt文件,那我们还可以读取文件每一行的磁力连接进行下载:
import csv with open("爱情公寓资源.txt", 'r', encoding='utf-8') as f: reader = csv.reader(f) films = [i[0] for i in reader] thunder = Dispatch('ThunderAgent.Agent.1') for film in films: thunder.AddTask(film, film[-12:], r"C:\迅雷下载") thunder.CommitTasks()
怎么样是不是很棒?你也来试试吧!
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!