Skip to content Skip to sidebar Skip to footer

How To Download A File With .torrent Extension From Link With Python

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 A4

Solution 1:

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:
        withopen(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.

Solution 2:

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 :

importwebbrowserurl="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

Post a Comment for "How To Download A File With .torrent Extension From Link With Python"