on master help sign in
Merge pull request #542 from ukwt/fix538

[fix] incorrect URLs in Reddit image search results - closes #538

commit817c74e52317126128c6f8740df01b8bdc51c3cf authorAdam Tauber <asciimoo@gmail.com> dateSat 16 Apr 2016, 13:29 (10 years ago) parent2661c3ad98024c
2 files changed+11−5
searx/engines/reddit.py+5−3 tests/unit/engines/test_reddit.py+6−2
searx/engines/reddit.py+5−3
@@ -13,7 +13,7 @@
1313import json
1414from cgi import escape
1515from urllib import urlencode
16−from urlparse import urlparse
16+from urlparse import urlparse, urljoin
1717from datetime import datetime
1818
1919# engine dependent config
@@ -21,7 +21,8 @@ categories = ['general', 'images', 'news', 'social media']
2121page_size = 25
2222
2323# search-url
24−search_url = 'https://www.reddit.com/search.json?{query}'
24+base_url = 'https://www.reddit.com/'
25+search_url = base_url + 'search.json?{query}'
2526
2627
2728# do search-request
@@ -52,7 +53,7 @@ def response(resp):
5253
5354 # extract post information
5455 params = {
55− 'url': data['url'],
56+ 'url': urljoin(base_url, data['permalink']),
5657 'title': data['title']
5758 }
5859
@@ -61,6 +62,7 @@ def response(resp):
6162 url_info = urlparse(thumbnail)
6263 # netloc & path
6364 if url_info[1] != '' and url_info[2] != '':
65+ params['img_src'] = data['url']
6466 params['thumbnail_src'] = thumbnail
6567 params['template'] = 'images.html'
6668 img_results.append(params)
tests/unit/engines/test_reddit.py+6−2
@@ -25,7 +25,8 @@ class TestRedditEngine(SearxTestCase):
2525 "data": {
2626 "children": [{
2727 "data": {
28− "url": "http://google.com/",
28+ "url": "http://google2.com/",
29+ "permalink": "http://google.com/",
2930 "title": "Title number one",
3031 "selftext": "Sample",
3132 "created_utc": 1401219957.0,
@@ -33,7 +34,8 @@ class TestRedditEngine(SearxTestCase):
3334 }
3435 }, {
3536 "data": {
36− "url": "https://reddit.com/",
37+ "url": "https://reddit2.com/",
38+ "permalink": "https://reddit.com/",
3739 "title": "Title number two",
3840 "selftext": "Dominus vobiscum",
3941 "created_utc": 1438792533.0,
@@ -55,6 +57,7 @@ class TestRedditEngine(SearxTestCase):
5557 self.assertEqual(r['url'], 'http://google.com/')
5658 self.assertEqual(r['title'], 'Title number one')
5759 self.assertEqual(r['template'], 'images.html')
60+ self.assertEqual(r['img_src'], 'http://google2.com/')
5861 self.assertEqual(r['thumbnail_src'], 'http://image.com/picture.jpg')
5962
6063 # testing second result (self-post)
@@ -65,3 +68,4 @@ class TestRedditEngine(SearxTestCase):
6568 created = datetime.fromtimestamp(1438792533.0)
6669 self.assertEqual(r['publishedDate'], created)
6770 self.assertTrue('thumbnail_src' not in r)
71+ self.assertTrue('img_src' not in r)