1
0
mirror of https://github.com/searx/searx synced 2024-12-10 11:10:07 +01:00

Merge pull request #1009 from misnyo/torrentz

[fix]torrentz search engine fixed for new version
This commit is contained in:
Adam Tauber 2017-08-30 21:08:31 +02:00 committed by GitHub
commit 489bf0f6fd
3 changed files with 42 additions and 34 deletions

View File

@ -1,7 +1,7 @@
""" """
Torrentz.eu (BitTorrent meta-search engine) Torrentz2.eu (BitTorrent meta-search engine)
@website https://torrentz.eu/ @website https://torrentz2.eu/
@provide-api no @provide-api no
@using-api no @using-api no
@ -14,24 +14,24 @@
import re import re
from lxml import html from lxml import html
from datetime import datetime from datetime import datetime
from searx.engines.nyaa import int_or_zero, get_filesize_mul
from searx.engines.xpath import extract_text from searx.engines.xpath import extract_text
from searx.url_utils import urlencode from searx.url_utils import urlencode
from searx.utils import get_torrent_size
# engine dependent config # engine dependent config
categories = ['files', 'videos', 'music'] categories = ['files', 'videos', 'music']
paging = True paging = True
# search-url # search-url
# https://torrentz.eu/search?f=EXAMPLE&p=6 # https://torrentz2.eu/search?f=EXAMPLE&p=6
base_url = 'https://torrentz.eu/' base_url = 'https://torrentz2.eu/'
search_url = base_url + 'search?{query}' search_url = base_url + 'search?{query}'
# do search-request # do search-request
def request(query, params): def request(query, params):
page = params['pageno'] - 1 page = params['pageno'] - 1
query = urlencode({'q': query, 'p': page}) query = urlencode({'f': query, 'p': page})
params['url'] = search_url.format(query=query) params['url'] = search_url.format(query=query)
return params return params
@ -54,22 +54,29 @@ def response(resp):
# extract url and remove a slash in the beginning # extract url and remove a slash in the beginning
link = links[0].attrib.get('href').lstrip('/') link = links[0].attrib.get('href').lstrip('/')
seed = result.xpath('./dd/span[@class="u"]/text()')[0].replace(',', '') seed = 0
leech = result.xpath('./dd/span[@class="d"]/text()')[0].replace(',', '') leech = 0
try:
seed = int(result.xpath('./dd/span[4]/text()')[0].replace(',', ''))
leech = int(result.xpath('./dd/span[5]/text()')[0].replace(',', ''))
except:
pass
params = { params = {
'url': base_url + link, 'url': base_url + link,
'title': title, 'title': title,
'seed': int_or_zero(seed), 'seed': seed,
'leech': int_or_zero(leech), 'leech': leech,
'template': 'torrent.html' 'template': 'torrent.html'
} }
# let's try to calculate the torrent size # let's try to calculate the torrent size
try: try:
size_str = result.xpath('./dd/span[@class="s"]/text()')[0] filesize_info = result.xpath('./dd/span[3]/text()')[0]
size, suffix = size_str.split() filesize, filesize_multiplier = filesize_info.split()
params['filesize'] = int(size) * get_filesize_mul(suffix) filesize = get_torrent_size(filesize, filesize_multiplier)
params['filesize'] = filesize
except: except:
pass pass
@ -80,9 +87,8 @@ def response(resp):
# extract and convert creation date # extract and convert creation date
try: try:
date_str = result.xpath('./dd/span[@class="a"]/span')[0].attrib.get('title') date_ts = result.xpath('./dd/span[2]')[0].attrib.get('title')
# Fri, 25 Mar 2016 16:29:01 date = datetime.fromtimestamp(float(date_ts))
date = datetime.strptime(date_str, '%a, %d %b %Y %H:%M:%S')
params['publishedDate'] = date params['publishedDate'] = date
except: except:
pass pass

View File

@ -552,6 +552,12 @@ engines:
timeout : 6.0 timeout : 6.0
disabled : True disabled : True
- name : torrentz
engine : torrentz
shortcut : tor
url: https://torrentz2.eu/
timeout : 3.0
- name : twitter - name : twitter
engine : twitter engine : twitter
shortcut : tw shortcut : tw

View File

@ -14,7 +14,7 @@ class TestTorrentzEngine(SearxTestCase):
params = torrentz.request(query, dic) params = torrentz.request(query, dic)
self.assertTrue('url' in params) self.assertTrue('url' in params)
self.assertTrue(query in params['url']) self.assertTrue(query in params['url'])
self.assertTrue('torrentz.eu' in params['url']) self.assertTrue('torrentz2.eu' in params['url'])
def test_response(self): def test_response(self):
resp = mock.Mock(text='<html></html>') resp = mock.Mock(text='<html></html>')
@ -30,13 +30,11 @@ class TestTorrentzEngine(SearxTestCase):
books ebooks books ebooks
</dt> </dt>
<dd> <dd>
<span class="v">1</span> <span>1</span>
<span class="a"> <span title="1503595924">5 hours</span>
<span title="Sun, 22 Nov 2015 03:01:42">4 months</span> <span>30 MB</span>
</span> <span>14</span>
<span class="s">30 MB</span> <span>1</span>
<span class="u">14</span>
<span class="d">1</span>
</dd> </dd>
</dl> </dl>
@ -48,13 +46,11 @@ class TestTorrentzEngine(SearxTestCase):
books ebooks books ebooks
</dt> </dt>
<dd> <dd>
<span class="v">1</span> <span>1</span>
<span class="a"> <span title="1503595924 aaa">5 hours</span>
<span title="Sun, 2124091j0j190gm42">4 months</span> <span>30MB</span>
</span> <span>5,555</span>
<span class="s">30MB</span> <span>1,234,567</span>
<span class="u">5,555</span>
<span class="d">1,234,567</span>
</dd> </dd>
</dl> </dl>
</div> </div>
@ -68,10 +64,10 @@ class TestTorrentzEngine(SearxTestCase):
# testing against the first result # testing against the first result
r = results[0] r = results[0]
self.assertEqual(r['url'], 'https://torrentz.eu/4362e08b1d80e1820fb2550b752f9f3126fe76d6') self.assertEqual(r['url'], 'https://torrentz2.eu/4362e08b1d80e1820fb2550b752f9f3126fe76d6')
self.assertEqual(r['title'], 'Completely valid info books ebooks') self.assertEqual(r['title'], 'Completely valid info books ebooks')
# 22 Nov 2015 03:01:42 # 22 Nov 2015 03:01:42
self.assertEqual(r['publishedDate'], datetime(2015, 11, 22, 3, 1, 42)) self.assertEqual(r['publishedDate'], datetime.fromtimestamp(1503595924))
self.assertEqual(r['seed'], 14) self.assertEqual(r['seed'], 14)
self.assertEqual(r['leech'], 1) self.assertEqual(r['leech'], 1)
self.assertEqual(r['filesize'], 30 * 1024 * 1024) self.assertEqual(r['filesize'], 30 * 1024 * 1024)
@ -79,7 +75,7 @@ class TestTorrentzEngine(SearxTestCase):
# testing against the second result # testing against the second result
r = results[1] r = results[1]
self.assertEqual(r['url'], 'https://torrentz.eu/poaskdpokaspod') self.assertEqual(r['url'], 'https://torrentz2.eu/poaskdpokaspod')
self.assertEqual(r['title'], 'Invalid hash and date and filesize books ebooks') self.assertEqual(r['title'], 'Invalid hash and date and filesize books ebooks')
self.assertEqual(r['seed'], 5555) self.assertEqual(r['seed'], 5555)
self.assertEqual(r['leech'], 1234567) self.assertEqual(r['leech'], 1234567)