4

I tried using wget:

url = https://yts.lt/torrent/download/A4A68F25347C709B55ED2DF946507C413D636DCA
wget.download(url, 'c:/path/')

The result was that I got a file with the name A4A68F25347C709B55ED2DF946507C413D636DCA and without any extension.

Whereas when I put the link in the navigator bar and click enter, a torrent file gets downloaded.

EDIT: Answer must be generic not case dependent. It must be a way to download .torrent files with their original name.

Hamza
  • 157
  • 2
  • 12

2 Answers2

2

You can get the filename inside the content-disposition header, i.e.:

import re, requests, traceback
try:
    url = "https://yts.lt/torrent/download/A4A68F25347C709B55ED2DF946507C413D636DCA"
    r = requests.get(url)
    d = r.headers['content-disposition']
    fname = re.findall('filename="(.+)"', d)
    if fname:
        with open(fname[0], 'wb') as f:
            f.write(r.content)
except:
    print(traceback.format_exc())

Py3 Demo


The code above is for python3. I don't have python2 installed and I normally don't post code without testing it.
Have a look at https://stackoverflow.com/a/11783325/797495, the method is the same.

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • Thank you, but please, how do you download the .torrent file with this way ? – Hamza Aug 23 '19 at 12:51
  • You're welcome, you can take a look at: https://www.cyberciti.biz/tips/linux-command-line-bittorrent-client.html . If my answer helped you, please consider accepting it as the correct answer. Thank you! – Pedro Lobito Aug 23 '19 at 17:13
0

I found an a way that gets the torrent files downloaded with their original name like as they were actually downloaded by putting the link in the browser's nav bar.

The solution consists of opening the user's browser from Python :

import webbrowser
url = "https://yts.lt/torrent/download/A4A68F25347C709B55ED2DF946507C413D636DCA"
webbrowser.open(url, new=0, autoraise=True)

Read more: Call to operating system to open url?

However the downside is :

  1. I don't get the option to choose the folder where I want to save the file (unless I changed it in the browser but still, in case I want to save torrents that matches some criteria in an other path, it won't be possible).
  2. And of course, your browser goes insane opening all those links XD
Hamza
  • 157
  • 2
  • 12
  • To download the actual content of the torrent using a similar method, take a look at: https://stackoverflow.com/a/33912912 – Pedro Lobito Aug 23 '19 at 14:58