Python Forum

Full Version: Send a requests to a magnet links shortener which doesn't have APIs
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, there is this website where you enter a torrent magnet link and you get a short url redirecting to the magnet link.
I'd like to do the procedure with python but the website doesn't have APIs, so after a quick research it seems that I have to use requests.post, but I don't understand how to find the data and headers to put in the requests.
This is my first try based on a snippet code that I found on internet
import requests, json
url = "https://tormag.ezpz.work/"
data = {'magnet_link': 'magnet:?xt=urn:btih:44EA47CF8DF204C8E5DF79A255809D47AC94C21B'}
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
r = requests.post(url, data=json.dumps(data), headers=headers)
r.status_code
200
then I run the command r.json() to look at the available headers but it raised this error
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\gianni\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\models.py", line 897, in json
    return complexjson.loads(self.text, **kwargs)
  File "C:\Users\gianni\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "C:\Users\gianni\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\gianni\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Any help?
It dos not use main url when connect to the API.
url = "https://tormag.ezpz.work/"
# Use this
url = "https://tormag.ezpz.work/api/api.php?action=insertMagnets"
Have to look what send over network then try to recreate and test what's needed.
So most have this in header.
"x-requested-with": "XMLHttpRequest"  
Payload need to send magnet link inside a list,and Requests don't need json import it has build in json decoder.
Then it look like this,woking for me.
import requests

url = "https://tormag.ezpz.work/api/api.php?action=insertMagnets"
magnet = 'magnet:?xt=urn:btih:44EA47CF8DF204C8E5DF79A255809D47AC94C21B'
headers = {
    "Content-type": "application/x-www-form-urlencoded; charset=UTF-8",
    "path": "/api/api.php?action=insertMagnets",
    "scheme": "https",
    "accept": "*/*",
    "x-requested-with": "XMLHttpRequest",
}

payload = {
    "magnets": [
        magnet
    ]
}
r = requests.post(url, headers=headers, json=payload)
>>> r.status_code
200
>>> r.json()
{'magnetEntries': ['https://tormag.ezpz.work/7njcoahpbc'], 'success': True}
>>> r.json()['magnetEntries'][0]
'https://tormag.ezpz.work/7njcoahpbc'
Wow this is incredible many many thanks!
Thanks to your suggestion "look what send over network" I tried to play with the browser DevTools and inside the Network tab I found the request url, the payload and the request headers. But I found many more request headers than the ones you put insede the python dictionary (see image below), so may I ask you how to choose what are the ones needed to be put in the dictionary?
Again thank you so much

[Image: vCeuAat.png]
(Feb-20-2022, 01:31 PM)Ascalon Wrote: [ -> ]But I found many more request headers than the ones you put insede the python dictionary (see image below), so may I ask you how to choose what are the ones needed to be put in the dictionary?
Can put all dictionary,but if test can get away with less.
So my guess was that XMLHttpRequest was needed,some need cookie(can make trouble as more step may be needed) but not here.