Browse Source

Merge self_ip and self_useragent into one plugin

Luc Didry 10 years ago
parent
commit
41fd748cdf
4 changed files with 18 additions and 45 deletions
  1. 2
    4
      searx/plugins/__init__.py
  2. 11
    2
      searx/plugins/self.py
  3. 0
    36
      searx/plugins/self_useragent.py
  4. 5
    3
      searx/tests/test_plugins.py

+ 2
- 4
searx/plugins/__init__.py View File

20
 logger = logger.getChild('plugins')
20
 logger = logger.getChild('plugins')
21
 
21
 
22
 from searx.plugins import (https_rewrite,
22
 from searx.plugins import (https_rewrite,
23
-                           self_ip,
24
-                           self_useragent,
23
+                           self,
25
                            search_on_category_select)
24
                            search_on_category_select)
26
 
25
 
27
 required_attrs = (('name', str),
26
 required_attrs = (('name', str),
72
 
71
 
73
 plugins = PluginStore()
72
 plugins = PluginStore()
74
 plugins.register(https_rewrite)
73
 plugins.register(https_rewrite)
75
-plugins.register(self_ip)
76
-plugins.register(self_useragent)
74
+plugins.register(self)
77
 plugins.register(search_on_category_select)
75
 plugins.register(search_on_category_select)

searx/plugins/self_ip.py → searx/plugins/self.py View File

15
 (C) 2015 by Adam Tauber, <asciimoo@gmail.com>
15
 (C) 2015 by Adam Tauber, <asciimoo@gmail.com>
16
 '''
16
 '''
17
 from flask.ext.babel import gettext
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('Correct Duckduckgo instant answers with your own informations (IP and User Agent)')
20
 default_on = True
21
 default_on = True
21
 
22
 
22
 
23
 
24
+# Self User Agent regex
25
+p = re.compile('.*user[ -]agent.*', re.IGNORECASE)
26
+
27
+
23
 # attach callback to the post search hook
28
 # attach callback to the post search hook
24
 #  request: flask request object
29
 #  request: flask request object
25
 #  ctx: the whole local context of the pre search hook
30
 #  ctx: the whole local context of the pre search hook
32
             ip = request.remote_addr
37
             ip = request.remote_addr
33
         ctx['search'].answers.clear()
38
         ctx['search'].answers.clear()
34
         ctx['search'].answers.add(ip)
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
     return True
44
     return True

+ 0
- 36
searx/plugins/self_useragent.py View File

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

+ 5
- 3
searx/tests/test_plugins.py View File

38
 
38
 
39
     def test_PluginStore_init(self):
39
     def test_PluginStore_init(self):
40
         store = plugins.PluginStore()
40
         store = plugins.PluginStore()
41
-        store.register(plugins.self_ip)
42
-        store.register(plugins.self_useragent)
41
+        store.register(plugins.self)
43
 
42
 
44
-        self.assertTrue(len(store.plugins) == 2)
43
+        self.assertTrue(len(store.plugins) == 1)
45
 
44
 
46
         # IP test
45
         # IP test
47
         request = Mock(user_plugins=store.plugins,
46
         request = Mock(user_plugins=store.plugins,
64
                               query='user agent')}
63
                               query='user agent')}
65
         store.call('post_search', request, ctx)
64
         store.call('post_search', request, ctx)
66
         self.assertTrue('Mock' in ctx['search'].answers)
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)