From 33fa3da2e8c832cce2462e4b3a9a3664072d2ea0 Mon Sep 17 00:00:00 2001 From: Noemi Vanyi Date: Thu, 4 Feb 2016 20:27:36 +0100 Subject: [PATCH 1/3] add Frinkiac engine & tests --- searx/engines/frinkiac.py | 42 ++++++++++++++++++++++++++ searx/settings.yml | 5 +++ tests/unit/engines/test_frinkiac.py | 47 +++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 searx/engines/frinkiac.py create mode 100644 tests/unit/engines/test_frinkiac.py diff --git a/searx/engines/frinkiac.py b/searx/engines/frinkiac.py new file mode 100644 index 000000000..7edb4a3cb --- /dev/null +++ b/searx/engines/frinkiac.py @@ -0,0 +1,42 @@ +""" +Frinkiac (Images) + +@website https://www.frinkiac.com +@provide-api no +@using-api no +@results JSON +@stable no +@parse url, title, img_src +""" + +from json import loads +from urllib import urlencode + +categories = ['images'] + +BASE = 'https://frinkiac.com/' +SEARCH_URL = '{base}api/search?{query}' +RESULT_URL = '{base}?{query}' +THUMB_URL = '{base}img/{episode}/{timestamp}/medium.jpg' +IMAGE_URL = '{base}img/{episode}/{timestamp}.jpg' + +def request(query, params): + params['url'] = SEARCH_URL.format(base=BASE, query=urlencode({'q': query})) + return params + +def response(resp): + results = [] + response_data = loads(resp.text) + for result in response_data: + episode = result['Episode'] + timestamp = result['Timestamp'] + + results.append({'template': 'images.html', + 'url': RESULT_URL.format(base=BASE, + query=urlencode({'p': 'caption', 'e': episode, 't': timestamp})), + 'title': episode, + 'content': '', + 'thumbnail_src': THUMB_URL.format(base=BASE, episode=episode, timestamp=timestamp), + 'img_src': IMAGE_URL.format(base=BASE, episode=episode, timestamp=timestamp)}) + + return results diff --git a/searx/settings.yml b/searx/settings.yml index 00458c59e..7e8182b0f 100644 --- a/searx/settings.yml +++ b/searx/settings.yml @@ -110,6 +110,11 @@ engines: # Or you can use the html non-stable engine, activated by default engine : flickr_noapi + - name : frinkiac + engine : frinkiac + shortcut : frk + disabled : True + - name : gigablast engine : gigablast shortcut : gb diff --git a/tests/unit/engines/test_frinkiac.py b/tests/unit/engines/test_frinkiac.py new file mode 100644 index 000000000..9d5b70bbd --- /dev/null +++ b/tests/unit/engines/test_frinkiac.py @@ -0,0 +1,47 @@ +# -*- coding: utf-8 -*- +from collections import defaultdict +import mock +from json import dumps +from searx.engines import frinkiac +from searx.testing import SearxTestCase + + +class TestFrinkiacEngine(SearxTestCase): + + def test_request(self): + query = 'test_query' + request_dict = defaultdict(dict) + params = frinkiac.request(query, request_dict) + self.assertTrue('url' in params) + + def test_response(self): + self.assertRaises(AttributeError, frinkiac.response, None) + self.assertRaises(AttributeError, frinkiac.response, []) + self.assertRaises(AttributeError, frinkiac.response, '') + self.assertRaises(AttributeError, frinkiac.response, '[]') + + text = dumps([{'Id': 654234, + 'Episode': 'S05E21', + 'Timestamp': 3453455, + 'Filename': ''}, + {'Id': 435354, + 'Episode': 'S05E22', + 'Timestamp': 3453456, + 'Filename': ''}, + {'Id': 435333, + 'Episode': 'S05E23', + 'Timestamp': 3453457, + 'Filename': ''}, + {'Id': 477234, + 'Episode': 'S05E24', + 'Timestamp': 3453458, + 'Filename': ''}]) + + response = mock.Mock(text=text) + results = frinkiac.response(response) + self.assertEqual(type(results), list) + self.assertEqual(len(results), 4) + self.assertEqual(results[0]['title'], u'S05E21') + self.assertEqual(results[0]['url'], 'https://frinkiac.com/?p=caption&e=S05E21&t=3453455') + self.assertEqual(results[0]['thumbnail_src'], 'https://frinkiac.com/img/S05E21/3453455/medium.jpg') + self.assertEqual(results[0]['img_src'], 'https://frinkiac.com/img/S05E21/3453455.jpg') From 3034c85163264aafb307ddf89add446b88ccb49f Mon Sep 17 00:00:00 2001 From: Noemi Vanyi Date: Thu, 4 Feb 2016 21:01:44 +0100 Subject: [PATCH 2/3] fix pep8 errors --- searx/engines/frinkiac.py | 2 ++ tests/unit/engines/test_frinkiac.py | 6 +++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/searx/engines/frinkiac.py b/searx/engines/frinkiac.py index 7edb4a3cb..a9383f862 100644 --- a/searx/engines/frinkiac.py +++ b/searx/engines/frinkiac.py @@ -20,10 +20,12 @@ RESULT_URL = '{base}?{query}' THUMB_URL = '{base}img/{episode}/{timestamp}/medium.jpg' IMAGE_URL = '{base}img/{episode}/{timestamp}.jpg' + def request(query, params): params['url'] = SEARCH_URL.format(base=BASE, query=urlencode({'q': query})) return params + def response(resp): results = [] response_data = loads(resp.text) diff --git a/tests/unit/engines/test_frinkiac.py b/tests/unit/engines/test_frinkiac.py index 9d5b70bbd..0997288a9 100644 --- a/tests/unit/engines/test_frinkiac.py +++ b/tests/unit/engines/test_frinkiac.py @@ -24,15 +24,15 @@ class TestFrinkiacEngine(SearxTestCase): 'Episode': 'S05E21', 'Timestamp': 3453455, 'Filename': ''}, - {'Id': 435354, + {'Id': 435354, 'Episode': 'S05E22', 'Timestamp': 3453456, 'Filename': ''}, - {'Id': 435333, + {'Id': 435333, 'Episode': 'S05E23', 'Timestamp': 3453457, 'Filename': ''}, - {'Id': 477234, + {'Id': 477234, 'Episode': 'S05E24', 'Timestamp': 3453458, 'Filename': ''}]) From baf16b886966bdd7aad07174a5262a6394ca077b Mon Sep 17 00:00:00 2001 From: Noemi Vanyi Date: Thu, 4 Feb 2016 22:39:47 +0100 Subject: [PATCH 3/3] better test data --- tests/unit/engines/test_frinkiac.py | 42 +++++++++++++++-------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/tests/unit/engines/test_frinkiac.py b/tests/unit/engines/test_frinkiac.py index 0997288a9..f3eb021d2 100644 --- a/tests/unit/engines/test_frinkiac.py +++ b/tests/unit/engines/test_frinkiac.py @@ -20,28 +20,30 @@ class TestFrinkiacEngine(SearxTestCase): self.assertRaises(AttributeError, frinkiac.response, '') self.assertRaises(AttributeError, frinkiac.response, '[]') - text = dumps([{'Id': 654234, - 'Episode': 'S05E21', - 'Timestamp': 3453455, - 'Filename': ''}, - {'Id': 435354, - 'Episode': 'S05E22', - 'Timestamp': 3453456, - 'Filename': ''}, - {'Id': 435333, - 'Episode': 'S05E23', - 'Timestamp': 3453457, - 'Filename': ''}, - {'Id': 477234, - 'Episode': 'S05E24', - 'Timestamp': 3453458, - 'Filename': ''}]) + text = """ +[{"Id":770931, + "Episode":"S06E18", + "Timestamp":534616, + "Filename":""}, + {"Id":1657080, + "Episode":"S12E14", + "Timestamp":910868, + "Filename":""}, + {"Id":1943753, + "Episode":"S14E21", + "Timestamp":773439, + "Filename":""}, + {"Id":107835, + "Episode":"S02E03", + "Timestamp":531709, + "Filename":""}] + """ response = mock.Mock(text=text) results = frinkiac.response(response) self.assertEqual(type(results), list) self.assertEqual(len(results), 4) - self.assertEqual(results[0]['title'], u'S05E21') - self.assertEqual(results[0]['url'], 'https://frinkiac.com/?p=caption&e=S05E21&t=3453455') - self.assertEqual(results[0]['thumbnail_src'], 'https://frinkiac.com/img/S05E21/3453455/medium.jpg') - self.assertEqual(results[0]['img_src'], 'https://frinkiac.com/img/S05E21/3453455.jpg') + self.assertEqual(results[0]['title'], u'S06E18') + self.assertEqual(results[0]['url'], 'https://frinkiac.com/?p=caption&e=S06E18&t=534616') + self.assertEqual(results[0]['thumbnail_src'], 'https://frinkiac.com/img/S06E18/534616/medium.jpg') + self.assertEqual(results[0]['img_src'], 'https://frinkiac.com/img/S06E18/534616.jpg')