diff options
author | Kirill Isakov <ukwt@ya.ru> | 2016-03-25 19:30:32 +0600 |
---|---|---|
committer | Kirill Isakov <ukwt@ya.ru> | 2016-03-25 19:30:32 +0600 |
commit | d026a97e42dce14bb187ea79682b9a303cd91e9e (patch) | |
tree | 63b85aedbb50d9515a47acfaa9f01cccbf789ace /searx | |
parent | e5677ae6b6b23c943e64d8e2abcb64c13c0e8bbf (diff) | |
download | searxng-d026a97e42dce14bb187ea79682b9a303cd91e9e.tar.gz searxng-d026a97e42dce14bb187ea79682b9a303cd91e9e.zip |
Add Reddit search engine
Diffstat (limited to 'searx')
-rw-r--r-- | searx/engines/reddit.py | 74 | ||||
-rw-r--r-- | searx/settings.yml | 7 |
2 files changed, 81 insertions, 0 deletions
diff --git a/searx/engines/reddit.py b/searx/engines/reddit.py new file mode 100644 index 000000000..d2b185b40 --- /dev/null +++ b/searx/engines/reddit.py @@ -0,0 +1,74 @@ +""" + Reddit + + @website https://www.reddit.com/ + @provide-api yes (https://www.reddit.com/dev/api) + + @using-api yes + @results JSON + @stable yes + @parse url, title, content, thumbnail, publishedDate +""" + +import json +from cgi import escape +from urllib import urlencode +from urlparse import urlparse +from datetime import datetime + +# engine dependent config +categories = ['general', 'images', 'news', 'social media'] +page_size = 25 + +# search-url +search_url = 'https://www.reddit.com/search.json?{query}' + + +# do search-request +def request(query, params): + query = urlencode({'q': query, + 'limit': page_size}) + params['url'] = search_url.format(query=query) + + return params + + +# get response from search-request +def response(resp): + img_results = [] + text_results = [] + + search_results = json.loads(resp.text) + + # return empty array if there are no results + if 'data' not in search_results: + return [] + + posts = search_results.get('data', {}).get('children', []) + + # process results + for post in posts: + data = post['data'] + + # extract post information + params = { + 'url': data['url'], + 'title': data['title'] + } + + # if thumbnail field contains a valid URL, we need to change template + thumbnail = data['thumbnail'] + url_info = urlparse(thumbnail) + # netloc & path + if url_info[1] != '' and url_info[2] != '': + params['thumbnail_src'] = thumbnail + params['template'] = 'images.html' + img_results.append(params) + else: + created = datetime.fromtimestamp(data['created_utc']) + params['content'] = escape(data['selftext']) + params['publishedDate'] = created + text_results.append(params) + + # show images first and text results second + return img_results + text_results diff --git a/searx/settings.yml b/searx/settings.yml index bd075b86c..4942ae56f 100644 --- a/searx/settings.yml +++ b/searx/settings.yml @@ -213,6 +213,13 @@ engines: shortcut : qws categories : social media + - name : reddit + engine : reddit + shortcut : re + page_size : 25 + timeout : 10.0 + disabled : True + - name : kickass engine : kickass shortcut : ka |