How to Add Multiple Magnet Links to Transmission Torrent Client – Python Automation

0
35
How to Add Multiple Magnet Links to Transmission Torrent Client - Python Automation

Hello, friends, I hope that you are all doing great and learning new things on time. It’s been quite some time that I was not able to write an article so today I will be talking about how you can automate basic tasks using Python for that we will take a look at “How to Add Multiple Magnet Links to Transmission Torrent Client”.

Now, if you have previously used Transmission you know that it is impossible to add multiple magnet links at a time. There is a way around for that in Linux using Transmission CLI but unfortunately, it can’t be done on macOS so for that I found a way around this issue.

The transmission also has a web interface that can be enabled from the settings and that also allows you to add torrents and download them so the first thing that came to my mind was I can use the request that is being sent to the Transmission local server and automate that using Python.

So I started to create a Python script that will do that but first I needed to know what kind of request is being sent to add a torrent magnet link so that what I did is to use a proxy Burp to view the request.

This is the request that is sent to the local Transmission server for the addition of a torrent magnet link as you can see the paraments that are being sent in the request are in JSON format which contains all the information about the torrent and the directory it will be stored in by know that now we can work on a python script right?

But the thing is to protect against CSRF attacks there is a header validation check in place on the Transmission server to bypass that we need to send the X-Transmission-Session-Id header too in order to add that torrent.

So that can be done by first sending a get request to the server and then adding the header in our POST request to add the torrent. So the code will look something like this.

import requests
s = requests.get('http://localhost:9091/transmission/rpc')
payload = {
'method':'torrent-add',
'arguments':{
'paused':'false',
'download-dir':'/Users/osamamahmood/Movies',
'filename': 'magnetlink'
   }
}
r = requests.post('http://localhost:9091/transmission/rpc',
headers=s.headers,json=payload)
print("Done all links added.")

Now that we have written down the basic structure of the request and how to handle the session issue we can see that using this request we can only add on the magnet link that is given in the code so we need to find a way using which we can add multiple links.

Also Read: Web Scraping from API Calls Using Python

For that what I did was I stored all the magnet links in a text file and then imported that file in the code and used file read to get all the links that are in the file and then get the link and add all the link one by one in the request and sent to the server so the final code looks like this:

import requests
links = open('dl.txt', 'r')
s = requests.get('http://localhost:9091/transmission/rpc')
for x in links:
    payload = {
        'method': 'torrent-add',
        'arguments': {
            'paused': 'false',
            'download-dir': '/Users/osamamahmood/Movies',
            'filename': x
        }
    }
    r = requests.post('http://localhost:9091/transmission/rpc',
                      headers=s.headers, json=payload)
print("Done all links added.")

Now this will do all the work.

How to Add Multiple Magnet Links to Transmission Torrent Client Video:

GitHub: https://github.com/OsamaMahmood/transmission-add-magnet

I hope you enjoyed the article and if you have any questions do comment down below and so I can try to solve those issues. The best way to learn programming is to solve problems like these that give a better understanding of how things work.