on master help sign in
Merge self_ip and self_useragent into one plugin
commit41fd748cdf7a6bcb14b664ded16f339eb864900c authorLuc Didry <luc@didry.org> dateMon 15 Jun 2015, 10:22 (11 years ago) parent538029d
4 files changed+18−45
searx/plugins/__init__.py+2−4 searx/plugins/self_ip.py → searx/plugins/self.py+11−2 searx/plugins/self_useragent.py−36 searx/tests/test_plugins.py+5−3
searx/plugins/__init__.py+2−4
@@ -20,8 +20,7 @@ from searx import logger
2020logger = logger.getChild('plugins')
2121
2222from searx.plugins import (https_rewrite,
23− self_ip,
24− self_useragent,
23+ self,
2524 search_on_category_select)
2625
2726required_attrs = (('name', str),
@@ -72,6 +71,5 @@ class PluginStore():
7271
7372plugins = PluginStore()
7473plugins.register(https_rewrite)
75−plugins.register(self_ip)
76−plugins.register(self_useragent)
74+plugins.register(self)
7775plugins.register(search_on_category_select)
searx/plugins/self_ip.py → searx/plugins/self.pyrenamed+11−2
@@ -15,11 +15,16 @@ along with searx. If not, see < http://www.gnu.org/licenses/ >.
1515(C) 2015 by Adam Tauber, <asciimoo@gmail.com>
1616'''
1717from flask.ext.babel import gettext
18−name = "Self IP"
19−description = gettext('Display your source IP address if the query expression is "ip"')
18+import re
19+name = "Self Informations"
20+description = gettext('Correct Duckduckgo instant answers with your own informations (IP and User Agent)')
2021default_on = True
2122
2223
24+# Self User Agent regex
25+p = re.compile('.*user[ -]agent.*', re.IGNORECASE)
26+
27+
2328# attach callback to the post search hook
2429# request: flask request object
2530# ctx: the whole local context of the pre search hook
@@ -32,4 +37,8 @@ def post_search(request, ctx):
3237 ip = request.remote_addr
3338 ctx['search'].answers.clear()
3439 ctx['search'].answers.add(ip)
40+ elif p.match(ctx['search'].query):
41+ ua = request.user_agent
42+ ctx['search'].answers.clear()
43+ ctx['search'].answers.add(ua)
3544 return True
searx/plugins/self_useragent.pydeleted−36
@@ -1,36 +0,0 @@
1−'''
2−searx is free software: you can redistribute it and/or modify
3−it under the terms of the GNU Affero General Public License as published by
4−the Free Software Foundation, either version 3 of the License, or
5−(at your option) any later version.
6−
7−searx is distributed in the hope that it will be useful,
8−but WITHOUT ANY WARRANTY; without even the implied warranty of
9−MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10−GNU Affero General Public License for more details.
11−
12−You should have received a copy of the GNU Affero General Public License
13−along with searx. If not, see < http://www.gnu.org/licenses/ >.
14−
15−(C) 2015 by Adam Tauber, <asciimoo@gmail.com>
16−'''
17−from flask.ext.babel import gettext
18−import re
19−name = "Self User Agent"
20−description = gettext('Display your own User Agent if the query expression contains "user agent" or "user-agent"')
21−default_on = True
22−
23−
24−# User Agent query regex
25−p = re.compile('user[ -]agent', re.IGNORECASE)
26−
27−
28−# attach callback to the post search hook
29−# request: flask request object
30−# ctx: the whole local context of the pre search hook
31−def post_search(request, ctx):
32− if p.match(ctx['search'].query):
33− ua = request.user_agent
34− ctx['search'].answers.clear()
35− ctx['search'].answers.add(ua)
36− return True
searx/tests/test_plugins.py+5−3
@@ -38,10 +38,9 @@ class SelfIPTest(SearxTestCase):
3838
3939 def test_PluginStore_init(self):
4040 store = plugins.PluginStore()
41− store.register(plugins.self_ip)
42− store.register(plugins.self_useragent)
41+ store.register(plugins.self)
4342
44− self.assertTrue(len(store.plugins) == 2)
43+ self.assertTrue(len(store.plugins) == 1)
4544
4645 # IP test
4746 request = Mock(user_plugins=store.plugins,
@@ -64,3 +63,6 @@ class SelfIPTest(SearxTestCase):
6463 query='user agent')}
6564 store.call('post_search', request, ctx)
6665 self.assertTrue('Mock' in ctx['search'].answers)
66+ ctx = {'search': Mock(answers=set(),
67+ query='What is my User-Agent?')}
68+ store.call('post_search', request, ctx)