summaryrefslogtreecommitdiff
path: root/searx
diff options
context:
space:
mode:
authorKirill Isakov <ukwt@ya.ru>2016-03-26 05:28:58 +0600
committerKirill Isakov <ukwt@ya.ru>2016-03-26 05:28:58 +0600
commit7fbc12ee4e6aea8a8ad0098deb03054976056371 (patch)
tree213517c3d4eb85dc08726de9fd28401720372516 /searx
parentd026a97e42dce14bb187ea79682b9a303cd91e9e (diff)
Add Torrentz.eu search engine
Diffstat (limited to 'searx')
-rw-r--r--searx/engines/nyaa.py26
-rw-r--r--searx/engines/torrentz.py93
-rw-r--r--searx/settings.yml5
3 files changed, 112 insertions, 12 deletions
diff --git a/searx/engines/nyaa.py b/searx/engines/nyaa.py
index 81caed76f..b0f884b77 100644
--- a/searx/engines/nyaa.py
+++ b/searx/engines/nyaa.py
@@ -43,6 +43,19 @@ def int_or_zero(num):
return int(num)
return 0
+# get multiplier to convert torrent size to bytes
+def get_filesize_mul(suffix):
+ return {
+ 'KB': 1024,
+ 'MB': 1024 ** 2,
+ 'GB': 1024 ** 3,
+ 'TB': 1024 ** 4,
+
+ 'KIB': 1024,
+ 'MIB': 1024 ** 2,
+ 'GIB': 1024 ** 3,
+ 'TIB': 1024 ** 4
+ }[str(suffix).upper()]
# do search-request
def request(query, params):
@@ -74,18 +87,7 @@ def response(resp):
# torrent size
try:
file_size, suffix = result.xpath(xpath_filesize)[0].split(' ')
-
- # convert torrent size to bytes.
- # if there is no correct index in this dictionary,
- # the try block fails as it should
- multiplier = {
- 'KIB': 1024,
- 'MIB': 1024 ** 2,
- 'GIB': 1024 ** 3,
- 'TIB': 1024 ** 4
- }[suffix.upper()]
-
- file_size = int(float(file_size) * multiplier)
+ file_size = int(float(file_size) * get_filesize_mul(suffix))
except Exception as e:
file_size = None
diff --git a/searx/engines/torrentz.py b/searx/engines/torrentz.py
new file mode 100644
index 000000000..92fbe7013
--- /dev/null
+++ b/searx/engines/torrentz.py
@@ -0,0 +1,93 @@
+"""
+ Torrentz.eu (BitTorrent meta-search engine)
+
+ @website https://torrentz.eu/
+ @provide-api no
+
+ @using-api no
+ @results HTML
+ @stable no (HTML can change, although unlikely,
+ see https://torrentz.eu/torrentz.btsearch)
+ @parse url, title, publishedDate, seed, leech, filesize, magnetlink
+"""
+
+import re
+from cgi import escape
+from urllib import urlencode
+from lxml import html
+from searx.engines.xpath import extract_text
+from datetime import datetime
+from searx.engines.nyaa import int_or_zero, get_filesize_mul
+
+# engine dependent config
+categories = ['files', 'videos', 'music']
+paging = True
+
+# search-url
+# https://torrentz.eu/search?f=EXAMPLE&p=6
+base_url = 'https://torrentz.eu/'
+search_url = base_url + 'search?{query}'
+
+
+# do search-request
+def request(query, params):
+ page = params['pageno'] - 1
+ query = urlencode({'q': query, 'p': page})
+ params['url'] = search_url.format(query=query)
+ return params
+
+
+# get response from search-request
+def response(resp):
+ results = []
+
+ dom = html.fromstring(resp.text)
+
+ for result in dom.xpath('//div[@class="results"]/dl'):
+ name_cell = result.xpath('./dt')[0]
+ title = extract_text(name_cell)
+
+ # skip rows that do not contain a link to a torrent
+ links = name_cell.xpath('./a')
+ if len(links) != 1:
+ continue
+
+ # extract url and remove a slash in the beginning
+ link = links[0].attrib.get('href').lstrip('/')
+
+ seed = result.xpath('./dd/span[@class="u"]/text()')[0].replace(',', '')
+ leech = result.xpath('./dd/span[@class="d"]/text()')[0].replace(',', '')
+
+ params = {
+ 'url': base_url + link,
+ 'title': title,
+ 'seed': int_or_zero(seed),
+ 'leech': int_or_zero(leech),
+ 'template': 'torrent.html'
+ }
+
+ # let's try to calculate the torrent size
+ try:
+ size_str = result.xpath('./dd/span[@class="s"]/text()')[0]
+ size, suffix = size_str.split()
+ params['filesize'] = int(size) * get_filesize_mul(suffix)
+ except Exception as e:
+ pass
+
+ # does our link contain a valid SHA1 sum?
+ if re.compile('[0-9a-fA-F]{40}').match(link):
+ # add a magnet link to the result
+ params['magnetlink'] = 'magnet:?xt=urn:btih:' + link
+
+ # extract and convert creation date
+ try:
+ date_str = result.xpath('./dd/span[@class="a"]/span')[0].attrib.get('title')
+ # Fri, 25 Mar 2016 16:29:01
+ date = datetime.strptime(date_str, '%a, %d %b %Y %H:%M:%S')
+ params['publishedDate'] = date
+ except Exception as e:
+ pass
+
+ results.append(params)
+
+ return results
diff --git a/searx/settings.yml b/searx/settings.yml
index 4942ae56f..2dce06fd0 100644
--- a/searx/settings.yml
+++ b/searx/settings.yml
@@ -271,6 +271,11 @@ engines:
shortcut : sw
disabled : True
+ - name : torrentz
+ engine : torrentz
+ timeout : 5.0
+ shortcut : to
+
- name : twitter
engine : twitter
shortcut : tw