Skip to content

Instantly share code, notes, and snippets.

@baverman

baverman/nya.py

Created Oct 29, 2012
Embed
What would you like to do?
Next episodes downloader from http://www.nyaa.eu/
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import re
import sys
import os.path
from urllib2 import build_opener
from urllib import urlencode
from lxml import html
DLDIR = '/home/bobrov/downloads'
TRDIR = u'/home/bobrov/torrents'
SEARCH_URL = 'http://www.nyaa.eu/?{}'
TEAMS = ('HorribleSubs', 'Commie', 'Zero-Raws', 'Leopard-Raws')
SD_TEAMS = ('Commie', )
opener = build_opener()
def dump(resp, fname='/tmp/boo.html'):
with open(fname, 'w') as f:
f.write(resp.read())
def get_team_idx(title, teams):
for i, t in enumerate(teams):
if t in title:
return i
return -1
def get_torrent_url_for_next_episode(fname):
title, _, episode = fname.lstrip('! ').rpartition(' - ')
if title.startswith('['):
teams, _, title = title.partition(']')
title = title.strip()
teams = map(str.strip, teams.lstrip('[').split('|'))
else:
teams = TEAMS
episode = '{:0{}}'.format(int(episode) + 1, len(episode))
query = '{} {}'.format(title, episode)
params = {
'page': 'search',
'cats': '0_0',
'filter': '2',
'term': query
}
resp = opener.open(SEARCH_URL.format(urlencode(params)))
root = html.parse(resp)
candidates = []
for tr in root.xpath('//tr[contains(@class, "tlistrow")]'):
title = tr.xpath('./td[@class="tlistname"]/a')[0].text.replace('_', ' ')
if ('720' in title or any(r in title for r in SD_TEAMS)) and ('- '+episode) in title:
tidx = get_team_idx(title, teams)
if tidx >= 0:
seeders = int(tr.xpath('./td[@class="tlistsn"]')[0].text)
if seeders < 10:
continue
turl = tr.xpath('./td[@class="tlistdownload"]/a')[0].attrib['href']
candidates.append((tidx, title, turl))
if candidates:
return min(candidates)[2]
else:
return None
def fetch_torrent(url):
resp = opener.open(url)
fname = re.search('filename="(.+?)"', resp.headers['Content-Disposition']).group(1).decode('utf-8')
if any(os.path.exists(os.path.join(TRDIR, fname + r)) for r in (u'', u'.added')):
print fname, 'already exists'
else:
print fname, 'added'
dump(resp, os.path.join(TRDIR, fname))
def main():
if len(sys.argv) > 1:
fnames = sys.argv[1:]
else:
fnames = [fname for fname in os.listdir(DLDIR) if fname.startswith('!!!')]
for fname in fnames:
url = get_torrent_url_for_next_episode(fname)
if url:
fetch_torrent(url)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment