How to download torrent file

12.2k views Asked by At

Im looking for a way to download a torrent file but i can get it to work.

I found couple of similar question here and tried the solutions but i cant get it to work. Heres what i have:

def get_torrent(site):
results = site
url = "https://nyaa.si/user/HorribleSubs?f=0&c=0_0&q=HorribleSubs+%5B720p%5D&p={}"
opener = urllib.request.build_opener()
opener.addheaders = [('User-Agent', 'CERN-LineMode/2.15 libwww/2.17b3')]
urllib.request.install_opener(opener)  # NOTE: global for the process
for line in results:
    name = "[HorribleSubs] " + line + " [720p].mkv"
    urllib.request.urlretrieve(url, name)

The function gets a list of episodes that are new and need to be downloaded, here is an example: Isekai Shokudou - 11, Koi to Uso - 11.

I then add the rest of the link styling and try to download the links but all the above code does is download the whole sites html code inside the below files:

[HorribleSubs] Isekai Shokudou - 11 [720p].mkv 

and

[HorribleSubs] Koi to Uso - 11 [720p].mkv

So i need a way to download the actual .torrent files from the site mentioned in the code

3

There are 3 answers

1
Nanoni On

Ok, i got the downloading of .torrent to work now, code below:

def get_torrent(site):
    results = site
    url = "https://nyaa.si/download/958359.torrent"
    opener = urllib.request.build_opener()
    opener.addheaders = [('User-Agent', 'CERN-LineMode/2.15 libwww/2.17b3')]
    urllib.request.install_opener(opener)  # NOTE: global for the process
    for line in results:
        name = "[HorribleSubs] " + line + " [720p].torrent"
        urllib.request.urlretrieve(url, name)

This downloads the .torrent file from link that is hardcoded to url, and the filename that it is saved as is in the for loop. Need to figure out next how to get the .torrent links automaticly inside the for loop for all animes in the list. Problem here is the way the .torrent links are on the website im downloading them from.

0
user2632863 On

You can follow these procedure to download file.

  1. Get file content using requests.get(url)
  2. Assign the destination file path. Eg : destination = NameofFile.torrent'
  3. Open and write the file open(destination,'wb').write(file.content)
url = "https://nyaa.si/download/958359.torrent"
file = requests.get(url)
destination = '958359.torrent'

open(destination,'wb').write(file.content)

0
Natesh bhat On

For movie downloading purposes u can use a nice library called

pyYify

which does most of the work of handling the magnet link and starting the downloads for you and stuff . Here is the github link for the library . https://github.com/nateshmbhat/pyYify

It also has torrent search feature for movies along with quality controls and rating selections u can use in your code .

The search_string for the movie can be 'Movie Title,IMDb Code, Actor Name, Director Name'. quality = 'All' , '720p' , '1080p' , '3D'.