Procházet zdrojové kódy

Merge pull request #371 from framasoft/add-useragent-plugin

Add a self user agent plugin
Adam Tauber před 10 roky
rodič
revize
a9d92c3874

+ 2
- 2
searx/plugins/__init__.py Zobrazit soubor

@@ -20,7 +20,7 @@ from searx import logger
20 20
 logger = logger.getChild('plugins')
21 21
 
22 22
 from searx.plugins import (https_rewrite,
23
-                           self_ip,
23
+                           self_info,
24 24
                            search_on_category_select)
25 25
 
26 26
 required_attrs = (('name', str),
@@ -71,5 +71,5 @@ class PluginStore():
71 71
 
72 72
 plugins = PluginStore()
73 73
 plugins.register(https_rewrite)
74
-plugins.register(self_ip)
74
+plugins.register(self_info)
75 75
 plugins.register(search_on_category_select)

searx/plugins/self_ip.py → searx/plugins/self_info.py Zobrazit soubor

@@ -15,11 +15,16 @@ along with searx. If not, see < http://www.gnu.org/licenses/ >.
15 15
 (C) 2015 by Adam Tauber, <asciimoo@gmail.com>
16 16
 '''
17 17
 from 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('Displays your IP if the query is "ip" and your user agent if the query contains "user agent".')
20 21
 default_on = True
21 22
 
22 23
 
24
+# Self User Agent regex
25
+p = re.compile('.*user[ -]agent.*', re.IGNORECASE)
26
+
27
+
23 28
 # attach callback to the post search hook
24 29
 #  request: flask request object
25 30
 #  ctx: the whole local context of the pre search hook
@@ -32,4 +37,8 @@ def post_search(request, ctx):
32 37
             ip = request.remote_addr
33 38
         ctx['search'].answers.clear()
34 39
         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)
35 44
     return True

+ 18
- 1
searx/tests/test_plugins.py Zobrazit soubor

@@ -38,10 +38,11 @@ class SelfIPTest(SearxTestCase):
38 38
 
39 39
     def test_PluginStore_init(self):
40 40
         store = plugins.PluginStore()
41
-        store.register(plugins.self_ip)
41
+        store.register(plugins.self_info)
42 42
 
43 43
         self.assertTrue(len(store.plugins) == 1)
44 44
 
45
+        # IP test
45 46
         request = Mock(user_plugins=store.plugins,
46 47
                        remote_addr='127.0.0.1')
47 48
         request.headers.getlist.return_value = []
@@ -49,3 +50,19 @@ class SelfIPTest(SearxTestCase):
49 50
                               query='ip')}
50 51
         store.call('post_search', request, ctx)
51 52
         self.assertTrue('127.0.0.1' in ctx['search'].answers)
53
+
54
+        # User agent test
55
+        request = Mock(user_plugins=store.plugins,
56
+                       user_agent='Mock')
57
+        request.headers.getlist.return_value = []
58
+        ctx = {'search': Mock(answers=set(),
59
+                              query='user-agent')}
60
+        store.call('post_search', request, ctx)
61
+        self.assertTrue('Mock' in ctx['search'].answers)
62
+        ctx = {'search': Mock(answers=set(),
63
+                              query='user agent')}
64
+        store.call('post_search', request, ctx)
65
+        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)