|
@@ -0,0 +1,115 @@
|
|
|
+"""
|
|
|
+ Nyaa.se (Anime Bittorrent tracker)
|
|
|
+
|
|
|
+ @website http://www.nyaa.se/
|
|
|
+ @provide-api no
|
|
|
+ @using-api no
|
|
|
+ @results HTML
|
|
|
+ @stable no (HTML can change)
|
|
|
+ @parse url, title, content, seed, leech, torrentfile
|
|
|
+"""
|
|
|
+
|
|
|
+from cgi import escape
|
|
|
+from urllib import urlencode
|
|
|
+from lxml import html
|
|
|
+from searx.engines.xpath import extract_text
|
|
|
+
|
|
|
+
|
|
|
+categories = ['files', 'images', 'videos', 'music']
|
|
|
+paging = True
|
|
|
+
|
|
|
+
|
|
|
+base_url = 'http://www.nyaa.se/'
|
|
|
+search_url = base_url + '?page=search&{query}&offset={offset}'
|
|
|
+
|
|
|
+
|
|
|
+xpath_results = '//table[@class="tlist"]//tr[contains(@class, "tlistrow")]'
|
|
|
+xpath_category = './/td[@class="tlisticon"]/a'
|
|
|
+xpath_title = './/td[@class="tlistname"]/a'
|
|
|
+xpath_torrent_file = './/td[@class="tlistdownload"]/a'
|
|
|
+xpath_filesize = './/td[@class="tlistsize"]/text()'
|
|
|
+xpath_seeds = './/td[@class="tlistsn"]/text()'
|
|
|
+xpath_leeches = './/td[@class="tlistln"]/text()'
|
|
|
+xpath_downloads = './/td[@class="tlistdn"]/text()'
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+def int_or_zero(num):
|
|
|
+ if isinstance(num, list):
|
|
|
+ if len(num) < 1:
|
|
|
+ return 0
|
|
|
+ num = num[0]
|
|
|
+ if num.isdigit():
|
|
|
+ return int(num)
|
|
|
+ return 0
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+def request(query, params):
|
|
|
+ query = urlencode({'term': query})
|
|
|
+ params['url'] = search_url.format(query=query, offset=params['pageno'])
|
|
|
+ return params
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+def response(resp):
|
|
|
+ results = []
|
|
|
+
|
|
|
+ dom = html.fromstring(resp.text)
|
|
|
+
|
|
|
+ for result in dom.xpath(xpath_results):
|
|
|
+
|
|
|
+ category = result.xpath(xpath_category)[0].attrib.get('title')
|
|
|
+
|
|
|
+
|
|
|
+ page_a = result.xpath(xpath_title)[0]
|
|
|
+ title = escape(extract_text(page_a))
|
|
|
+
|
|
|
+
|
|
|
+ href = page_a.attrib.get('href')
|
|
|
+
|
|
|
+
|
|
|
+ torrent_link = result.xpath(xpath_torrent_file)[0].attrib.get('href')
|
|
|
+
|
|
|
+
|
|
|
+ try:
|
|
|
+ file_size, suffix = result.xpath(xpath_filesize)[0].split(' ')
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ multiplier = {
|
|
|
+ 'KIB': 1024,
|
|
|
+ 'MIB': 1024 ** 2,
|
|
|
+ 'GIB': 1024 ** 3,
|
|
|
+ 'TIB': 1024 ** 4
|
|
|
+ }[suffix.upper()]
|
|
|
+
|
|
|
+ file_size = int(float(file_size) * multiplier)
|
|
|
+ except Exception as e:
|
|
|
+ file_size = None
|
|
|
+
|
|
|
+
|
|
|
+ seed = int_or_zero(result.xpath(xpath_seeds))
|
|
|
+
|
|
|
+
|
|
|
+ leech = int_or_zero(result.xpath(xpath_leeches))
|
|
|
+
|
|
|
+
|
|
|
+ downloads = int_or_zero(result.xpath(xpath_downloads))
|
|
|
+
|
|
|
+
|
|
|
+ content = 'Category: "{category}". Downloaded {downloads} times.'
|
|
|
+ content = content.format(category=category, downloads=downloads)
|
|
|
+ content = escape(content)
|
|
|
+
|
|
|
+ results.append({'url': href,
|
|
|
+ 'title': title,
|
|
|
+ 'content': content,
|
|
|
+ 'seed': seed,
|
|
|
+ 'leech': leech,
|
|
|
+ 'filesize': file_size,
|
|
|
+ 'torrentfile': torrent_link,
|
|
|
+ 'template': 'torrent.html'})
|
|
|
+
|
|
|
+ return results
|