mirror of
https://github.com/searxng/searxng
synced 2024-01-01 18:24:07 +00:00
Add F-Droid search engine
This commit is contained in:
parent
547b8a8765
commit
c1d456b136
53
searx/engines/fdroid.py
Normal file
53
searx/engines/fdroid.py
Normal file
@ -0,0 +1,53 @@
|
||||
"""
|
||||
F-Droid (a repository of FOSS applications for Android)
|
||||
|
||||
@website https://f-droid.org/
|
||||
@provide-api no
|
||||
@using-api no
|
||||
@results HTML
|
||||
@stable no (HTML can change)
|
||||
@parse url, title, content
|
||||
"""
|
||||
|
||||
from cgi import escape
|
||||
from urllib import urlencode
|
||||
from searx.engines.xpath import extract_text
|
||||
from lxml import html
|
||||
|
||||
# engine dependent config
|
||||
categories = ['files']
|
||||
paging = True
|
||||
|
||||
# search-url
|
||||
base_url = 'https://f-droid.org/'
|
||||
search_url = base_url + 'repository/browse/?{query}'
|
||||
|
||||
|
||||
# do search-request
|
||||
def request(query, params):
|
||||
query = urlencode({'fdfilter': query,
|
||||
'fdpage': params['pageno']})
|
||||
params['url'] = search_url.format(query=query)
|
||||
return params
|
||||
|
||||
|
||||
# get response from search-request
|
||||
def response(resp):
|
||||
results = []
|
||||
|
||||
dom = html.fromstring(resp.text)
|
||||
|
||||
for app in dom.xpath('//div[@id="appheader"]'):
|
||||
url = app.xpath('./ancestor::a/@href')[0]
|
||||
title = app.xpath('./p/span/text()')[0]
|
||||
img_src = app.xpath('.//img/@src')[0]
|
||||
|
||||
content = extract_text(app.xpath('./p')[0])
|
||||
content = escape(content.replace(title, '', 1).strip())
|
||||
|
||||
results.append({'url': url,
|
||||
'title': title,
|
||||
'content': content,
|
||||
'img_src': img_src})
|
||||
|
||||
return results
|
@ -105,6 +105,11 @@ engines:
|
||||
shortcut : 1x
|
||||
disabled : True
|
||||
|
||||
- name : fdroid
|
||||
engine : fdroid
|
||||
shortcut : fd
|
||||
disabled : True
|
||||
|
||||
- name : flickr
|
||||
categories : images
|
||||
shortcut : fl
|
||||
|
49
tests/unit/engines/test_fdroid.py
Normal file
49
tests/unit/engines/test_fdroid.py
Normal file
@ -0,0 +1,49 @@
|
||||
import mock
|
||||
from collections import defaultdict
|
||||
from searx.engines import fdroid
|
||||
from searx.testing import SearxTestCase
|
||||
|
||||
|
||||
class TestFdroidEngine(SearxTestCase):
|
||||
|
||||
def test_request(self):
|
||||
query = 'test_query'
|
||||
dic = defaultdict(dict)
|
||||
dic['pageno'] = 1
|
||||
params = fdroid.request(query, dic)
|
||||
self.assertTrue('url' in params)
|
||||
self.assertTrue(query in params['url'])
|
||||
self.assertTrue('f-droid.org' in params['url'])
|
||||
|
||||
def test_response(self):
|
||||
resp = mock.Mock(text='<html></html>')
|
||||
self.assertEqual(fdroid.response(resp), [])
|
||||
|
||||
html = """
|
||||
<a href="https://google.com/qwerty">
|
||||
<div id="appheader">
|
||||
<div style="float:left;padding-right:10px;">
|
||||
<img src="http://example.com/image.png"
|
||||
style="width:48px;border:none;">
|
||||
</div>
|
||||
<div style="float:right;">
|
||||
<p>Details...</p>
|
||||
</div>
|
||||
<p style="color:#000000;">
|
||||
<span style="font-size:20px;">Sample title</span>
|
||||
<br>
|
||||
Sample content
|
||||
</p>
|
||||
</div>
|
||||
</a>
|
||||
"""
|
||||
|
||||
resp = mock.Mock(text=html)
|
||||
results = fdroid.response(resp)
|
||||
|
||||
self.assertEqual(type(results), list)
|
||||
self.assertEqual(len(results), 1)
|
||||
self.assertEqual(results[0]['url'], 'https://google.com/qwerty')
|
||||
self.assertEqual(results[0]['title'], 'Sample title')
|
||||
self.assertEqual(results[0]['content'], 'Sample content')
|
||||
self.assertEqual(results[0]['img_src'], 'http://example.com/image.png')
|
Loading…
Reference in New Issue
Block a user