on master help sign in
Merge pull request #1754 from MarcAbonce/seedpeer
20da8f2Adam Tauber
searx/engines/seedpeer.pynew file+78
@@ -0,0 +1,78 @@
1+# Seedpeer (Videos, Music, Files)
2+#
3+# @website https://seedpeer.me
4+# @provide-api no (nothing found)
5+#
6+# @using-api no
7+# @results HTML (using search portal)
8+# @stable yes (HTML can change)
9+# @parse url, title, content, seed, leech, magnetlink
10+
11+from lxml import html
12+from json import loads
13+from operator import itemgetter
14+from searx.url_utils import quote, urljoin
15+from searx.engines.xpath import extract_text
16+
17+
18+url = 'https://seedpeer.me/'
19+search_url = url + 'search/{search_term}?page={page_no}'
20+torrent_file_url = url + 'torrent/{torrent_hash}'
21+
22+# specific xpath variables
23+script_xpath = '//script[@type="text/javascript"][not(@src)]'
24+torrent_xpath = '(//table)[2]/tbody/tr'
25+link_xpath = '(./td)[1]/a/@href'
26+age_xpath = '(./td)[2]'
27+size_xpath = '(./td)[3]'
28+
29+
30+# do search-request
31+def request(query, params):
32+ params['url'] = search_url.format(search_term=quote(query),
33+ page_no=params['pageno'])
34+ return params
35+
36+
37+# get response from search-request
38+def response(resp):
39+ results = []
40+ dom = html.fromstring(resp.text)
41+ result_rows = dom.xpath(torrent_xpath)
42+
43+ try:
44+ script_element = dom.xpath(script_xpath)[0]
45+ json_string = script_element.text[script_element.text.find('{'):]
46+ torrents_json = loads(json_string)
47+ except:
48+ return []
49+
50+ # parse results
51+ for torrent_row, torrent_json in zip(result_rows, torrents_json['data']['list']):
52+ title = torrent_json['name']
53+ seed = int(torrent_json['seeds'])
54+ leech = int(torrent_json['peers'])
55+ size = int(torrent_json['size'])
56+ torrent_hash = torrent_json['hash']
57+
58+ torrentfile = torrent_file_url.format(torrent_hash=torrent_hash)
59+ magnetlink = 'magnet:?xt=urn:btih:{}'.format(torrent_hash)
60+
61+ age = extract_text(torrent_row.xpath(age_xpath))
62+ link = torrent_row.xpath(link_xpath)[0]
63+
64+ href = urljoin(url, link)
65+
66+ # append result
67+ results.append({'url': href,
68+ 'title': title,
69+ 'content': age,
70+ 'seed': seed,
71+ 'leech': leech,
72+ 'filesize': size,
73+ 'torrentfile': torrentfile,
74+ 'magnetlink': magnetlink,
75+ 'template': 'torrent.html'})
76+
77+ # return results sorted by seeder
78+ return sorted(results, key=itemgetter('seed'), reverse=True)
searx/settings.yml+5
@@ -748,6 +748,11 @@ engines:
748 page_size : 10
748 page_size : 10
749 disabled : True
749 disabled : True
750
750
751+ - name : seedpeer
752+ shortcut : speu
753+ engine : seedpeer
754+ categories: files, music, videos
755+
751# - name : yacy
756# - name : yacy
752# engine : yacy
757# engine : yacy
753# shortcut : ya
758# shortcut : ya
searx/templates/courgette/result_templates/torrent.html+1−1
@@ -4,7 +4,7 @@
4 {% endif %}
4 {% endif %}
5 <h3 class="result_title"><a href="{{ result.url }}" {% if results_on_new_tab %}target="_blank" rel="noopener noreferrer"{% else %}rel="noreferrer"{% endif %}>{{ result.title|safe }}</a></h3>
5 <h3 class="result_title"><a href="{{ result.url }}" {% if results_on_new_tab %}target="_blank" rel="noopener noreferrer"{% else %}rel="noreferrer"{% endif %}>{{ result.title|safe }}</a></h3>
6 {% if result.content %}<span class="content">{{ result.content|safe }}</span><br />{% endif %}
6 {% if result.content %}<span class="content">{{ result.content|safe }}</span><br />{% endif %}
7− {% if result.seed %}<span class="stats">{{ _('Seeder') }} : {{ result.seed }}, {{ _('Leecher') }} : {{ result.leech }}</span><br />{% endif %}
7+ {% if result.seed is defined %}<span class="stats">{{ _('Seeder') }} : {{ result.seed }}, {{ _('Leecher') }} : {{ result.leech }}</span><br />{% endif %}
8 <span>
8 <span>
9 {% if result.magnetlink %}<a href="{{ result.magnetlink }}" class="magnetlink">{{ _('magnet link') }}</a>{% endif %}
9 {% if result.magnetlink %}<a href="{{ result.magnetlink }}" class="magnetlink">{{ _('magnet link') }}</a>{% endif %}
10 {% if result.torrentfile %}<a href="{{ result.torrentfile }}" class="torrentfile" {% if results_on_new_tab %}target="_blank" rel="noopener noreferrer"{% else %}rel="noreferrer"{% endif %}>{{ _('torrent file') }}</a>{% endif %}
10 {% if result.torrentfile %}<a href="{{ result.torrentfile }}" class="torrentfile" {% if results_on_new_tab %}target="_blank" rel="noopener noreferrer"{% else %}rel="noreferrer"{% endif %}>{{ _('torrent file') }}</a>{% endif %}
searx/templates/legacy/result_templates/torrent.html+1−1
@@ -8,6 +8,6 @@
8 <p>
8 <p>
9 {% if result.magnetlink %}<a href="{{ result.magnetlink }}" class="magnetlink">{{ _('magnet link') }}</a>{% endif %}
9 {% if result.magnetlink %}<a href="{{ result.magnetlink }}" class="magnetlink">{{ _('magnet link') }}</a>{% endif %}
10 {% if result.torrentfile %}<a href="{{ result.torrentfile }}" {% if results_on_new_tab %}target="_blank" rel="noopener noreferrer"{% else %}rel="noreferrer"{% endif %} class="torrentfile">{{ _('torrent file') }}</a>{% endif %} -
10 {% if result.torrentfile %}<a href="{{ result.torrentfile }}" {% if results_on_new_tab %}target="_blank" rel="noopener noreferrer"{% else %}rel="noreferrer"{% endif %} class="torrentfile">{{ _('torrent file') }}</a>{% endif %} -
11− {% if result.seed %}<span class="stats">{{ _('Seeder') }} : {{ result.seed }}, {{ _('Leecher') }} : {{ result.leech }}</span>{% endif %}
11+ {% if result.seed is defined %}<span class="stats">{{ _('Seeder') }} : {{ result.seed }}, {{ _('Leecher') }} : {{ result.leech }}</span>{% endif %}
12 </p>
12 </p>
13</div>
13</div>
searx/templates/oscar/result_templates/torrent.html+1−1
@@ -3,7 +3,7 @@
3{{ result_header(result, favicons) }}
3{{ result_header(result, favicons) }}
4{{ result_sub_header(result) }}
4{{ result_sub_header(result) }}
5
5
6−{% if result.seed %}<p class="result-content">{{ icon('transfer') }} {{ _('Seeder') }} <span class="badge">{{ result.seed }}</span> &bull; {{ _('Leecher') }} <span class="badge">{{ result.leech }}</span>{% endif %}
6+{% if result.seed is defined %}<p class="result-content">{{ icon('transfer') }} {{ _('Seeder') }} <span class="badge">{{ result.seed }}</span> &bull; {{ _('Leecher') }} <span class="badge">{{ result.leech }}</span>{% endif %}
7{% if result.filesize %}<br />{{ icon('floppy-disk') }} {{ _('Filesize') }}
7{% if result.filesize %}<br />{{ icon('floppy-disk') }} {{ _('Filesize') }}
8 <span class="badge">
8 <span class="badge">
9 {% if result.filesize < 1024 %}{{ result.filesize }} {{ _('Bytes') }}
9 {% if result.filesize < 1024 %}{{ result.filesize }} {{ _('Bytes') }}
searx/templates/simple/result_templates/torrent.html+1−1
@@ -6,7 +6,7 @@
6{% if result.magnetlink %}<p class="altlink"> &bull; {{ result_link(result.magnetlink, icon('magnet') + _('magnet link'), "magnetlink") }}</p>{% endif %}
6{% if result.magnetlink %}<p class="altlink"> &bull; {{ result_link(result.magnetlink, icon('magnet') + _('magnet link'), "magnetlink") }}</p>{% endif %}
7{% if result.torrentfile %}<p class="altlink"> &bull; {{ result_link(result.torrentfile, icon('download-alt') + _('torrent file'), "torrentfile") }}</p>{% endif %}
7{% if result.torrentfile %}<p class="altlink"> &bull; {{ result_link(result.torrentfile, icon('download-alt') + _('torrent file'), "torrentfile") }}</p>{% endif %}
8
8
9−{% if result.seed %}<p class="stat"> &bull; {{ icon('arrow-swap') }} {{ _('Seeder') }} <span class="badge">{{ result.seed }}</span> &bull; {{ _('Leecher') }} <span class="badge">{{ result.leech }}</span></p>{% endif %}
9+{% if result.seed is defined %}<p class="stat"> &bull; {{ icon('arrow-swap') }} {{ _('Seeder') }} <span class="badge">{{ result.seed }}</span> &bull; {{ _('Leecher') }} <span class="badge">{{ result.leech }}</span></p>{% endif %}
10
10
11{%- if result.filesize %}<p class="stat">{{ icon('floppy-disk') }} {{ _('Filesize') }}<span class="badge">
11{%- if result.filesize %}<p class="stat">{{ icon('floppy-disk') }} {{ _('Filesize') }}<span class="badge">
12 {%- if result.filesize < 1024 %}{{ result.filesize }} {{ _('Bytes') }}
12 {%- if result.filesize < 1024 %}{{ result.filesize }} {{ _('Bytes') }}
tests/unit/engines/test_seedpeer.pynew file+66
@@ -0,0 +1,66 @@
1+# -*- coding: utf-8 -*-
2+from collections import defaultdict
3+import mock
4+from searx.engines import seedpeer
5+from searx.testing import SearxTestCase
6+
7+
8+class TestBtdiggEngine(SearxTestCase):
9+
10+ def test_request(self):
11+ query = 'test_query'
12+ dicto = defaultdict(dict)
13+ dicto['pageno'] = 1
14+ params = seedpeer.request(query, dicto)
15+ self.assertIn('url', params)
16+ self.assertIn(query, params['url'])
17+ self.assertIn('seedpeer', params['url'])
18+
19+ def test_response(self):
20+ self.assertRaises(AttributeError, seedpeer.response, None)
21+ self.assertRaises(AttributeError, seedpeer.response, [])
22+ self.assertRaises(AttributeError, seedpeer.response, '')
23+ self.assertRaises(AttributeError, seedpeer.response, '[]')
24+
25+ response = mock.Mock(text='<html></html>')
26+ self.assertEqual(seedpeer.response(response), [])
27+
28+ html = u"""
29+ <html>
30+ <head>
31+ <script></script>
32+ <script type="text/javascript" src="not_here.js"></script>
33+ <script type="text/javascript">
34+ window.initialData=
35+ {"data": {"list": [{"name": "Title", "seeds": "10", "peers": "20", "size": "1024", "hash": "abc123"}]}}
36+ </script>
37+ </head>
38+ <body>
39+ <table></table>
40+ <table>
41+ <thead><tr></tr></thead>
42+ <tbody>
43+ <tr>
44+ <td><a href="link">Title</a></td>
45+ <td>1 year</td>
46+ <td>1 KB</td>
47+ <td>10</td>
48+ <td>20</td>
49+ <td></td>
50+ </tr>
51+ </tbody>
52+ </table>
53+ </body>
54+ </html>
55+ """
56+ response = mock.Mock(text=html)
57+ results = seedpeer.response(response)
58+ self.assertEqual(type(results), list)
59+ self.assertEqual(len(results), 1)
60+ self.assertEqual(results[0]['title'], 'Title')
61+ self.assertEqual(results[0]['url'], 'https://seedpeer.me/link')
62+ self.assertEqual(results[0]['seed'], 10)
63+ self.assertEqual(results[0]['leech'], 20)
64+ self.assertEqual(results[0]['filesize'], 1024)
65+ self.assertEqual(results[0]['torrentfile'], 'https://seedpeer.me/torrent/abc123')
66+ self.assertEqual(results[0]['magnetlink'], 'magnet:?xt=urn:btih:abc123')