浏览代码

Add a self user agent plugin

Just like with the "ip" query, duckduckgo gives the server's
information with the "user agent" query.
This corrects this behavior by adding a plugin based on self_ip.py plugin.
Luc Didry 9 年前
父节点
当前提交
538029dc14
共有 3 个文件被更改,包括 54 次插入1 次删除
  1. 2
    0
      searx/plugins/__init__.py
  2. 36
    0
      searx/plugins/self_useragent.py
  3. 16
    1
      searx/tests/test_plugins.py

+ 2
- 0
searx/plugins/__init__.py 查看文件

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

+ 36
- 0
searx/plugins/self_useragent.py 查看文件

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

+ 16
- 1
searx/tests/test_plugins.py 查看文件

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