2

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

Nanoni
  • 451
  • 2
  • 7
  • 20
  • The url you are using seems not a .torrent file. Try with this for example and tell us the result please. https://nyaa.si/download/958473.torrent – maiky_forrester Sep 12 '17 at 11:10
  • File "C:\Python36\lib\urllib\request.py", line 258, in urlretrieve tfp = open(filename, 'wb') OSError: [Errno 22] Invalid argument: 'https://nyaa.si/download/958359.torrent' Process finished with exit code 1 – Nanoni Sep 12 '17 at 11:28
  • the links for the actual .torrents are random so i cant reall tell the app what to download, instead the idea i had here was to click the "name_of show - ep_num" link and from that site find .torrent link (there is only one on each page) and download that. but i dont know how to do that either :/ – Nanoni Sep 12 '17 at 11:34
  • do you really need python script for this. ? – Anurag Misra Sep 12 '17 at 11:40
  • Well yes and no, this script helps me so i dont have to manually check what series and what episodes i have downloaded and i wouldnt have to manually check if a new ep is out everyday. i currently have 64 series to follow so yeah it takes a bit of work to do this manually, also series go on random breaks and come back after couple of months so checking manually for all this is alot of work + im trying to learn python so what better way to do it than to do a script that saves me alot of time and im learning coding at the same time :) – Nanoni Sep 12 '17 at 11:50

3 Answers3

1

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.

Nanoni
  • 451
  • 2
  • 7
  • 20
  • 1
    1. Better to parse XML RSS feed (https://nyaa.si/?page=rss) 2. To parsr html I prefer to use scrapy library. It use xpath to select data. May be You prefer different xpath framework https://stackoverflow.com/questions/8692/how-to-use-xpath-in-python – mmv-ru Apr 10 '18 at 23:00
0

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'.

Natesh bhat
  • 12,274
  • 10
  • 84
  • 125
0

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)

user2632863
  • 7
  • 1
  • 3