瀏覽代碼

[fix] pep8

Adam Tauber 10 年之前
父節點
當前提交
611f4e2a86
共有 1 個檔案被更改,包括 22 行新增13 行删除
  1. 22
    13
      searx/engines/google.py

+ 22
- 13
searx/engines/google.py 查看文件

1
-## Google (Web)
2
-# 
1
+#  Google (Web)
2
+#
3
 # @website     https://www.google.com
3
 # @website     https://www.google.com
4
 # @provide-api yes (https://developers.google.com/custom-search/)
4
 # @provide-api yes (https://developers.google.com/custom-search/)
5
-# 
5
+#
6
 # @using-api   no
6
 # @using-api   no
7
 # @results     HTML
7
 # @results     HTML
8
 # @stable      no (HTML can change)
8
 # @stable      no (HTML can change)
9
 # @parse       url, title, content, suggestion
9
 # @parse       url, title, content, suggestion
10
 
10
 
11
 from urllib import urlencode
11
 from urllib import urlencode
12
-from urlparse import unquote,urlparse,parse_qsl
12
+from urlparse import urlparse, parse_qsl
13
 from lxml import html
13
 from lxml import html
14
 from searx.engines.xpath import extract_text, extract_url
14
 from searx.engines.xpath import extract_text, extract_url
15
 
15
 
23
 search_path = '/search'
23
 search_path = '/search'
24
 redirect_path = '/url'
24
 redirect_path = '/url'
25
 images_path = '/images'
25
 images_path = '/images'
26
-search_url = 'https://' + google_hostname + search_path + '?{query}&start={offset}&gbv=1'
26
+search_url = ('https://' +
27
+              google_hostname +
28
+              search_path +
29
+              '?{query}&start={offset}&gbv=1')
27
 
30
 
28
 # specific xpath variables
31
 # specific xpath variables
29
-results_xpath= '//li[@class="g"]'
32
+results_xpath = '//li[@class="g"]'
30
 url_xpath = './/h3/a/@href'
33
 url_xpath = './/h3/a/@href'
31
 title_xpath = './/h3'
34
 title_xpath = './/h3'
32
 content_xpath = './/span[@class="st"]'
35
 content_xpath = './/span[@class="st"]'
36
 image_url_xpath = './@href'
39
 image_url_xpath = './@href'
37
 image_img_src_xpath = './img/@src'
40
 image_img_src_xpath = './img/@src'
38
 
41
 
42
+
39
 # remove google-specific tracking-url
43
 # remove google-specific tracking-url
40
 def parse_url(url_string):
44
 def parse_url(url_string):
41
     parsed_url = urlparse(url_string)
45
     parsed_url = urlparse(url_string)
42
-    if parsed_url.netloc in [google_hostname, ''] and parsed_url.path==redirect_path:
46
+    if (parsed_url.netloc in [google_hostname, '']
47
+            and parsed_url.path == redirect_path):
43
         query = dict(parse_qsl(parsed_url.query))
48
         query = dict(parse_qsl(parsed_url.query))
44
         return query['q']
49
         return query['q']
45
     else:
50
     else:
46
         return url_string
51
         return url_string
47
 
52
 
53
+
48
 # do search-request
54
 # do search-request
49
 def request(query, params):
55
 def request(query, params):
50
     offset = (params['pageno'] - 1) * 10
56
     offset = (params['pageno'] - 1) * 10
52
     if params['language'] == 'all':
58
     if params['language'] == 'all':
53
         language = 'en'
59
         language = 'en'
54
     else:
60
     else:
55
-        language = params['language'].replace('_','-').lower()
61
+        language = params['language'].replace('_', '-').lower()
56
 
62
 
57
     params['url'] = search_url.format(offset=offset,
63
     params['url'] = search_url.format(offset=offset,
58
                                       query=urlencode({'q': query}))
64
                                       query=urlencode({'q': query}))
74
         try:
80
         try:
75
             url = parse_url(extract_url(result.xpath(url_xpath), search_url))
81
             url = parse_url(extract_url(result.xpath(url_xpath), search_url))
76
             parsed_url = urlparse(url)
82
             parsed_url = urlparse(url)
77
-            if parsed_url.netloc==google_hostname and parsed_url.path==search_path:
83
+            if (parsed_url.netloc == google_hostname
84
+                    and parsed_url.path == search_path):
78
                 # remove the link to google news
85
                 # remove the link to google news
79
                 continue
86
                 continue
80
 
87
 
81
-            if parsed_url.netloc==google_hostname and parsed_url.path==images_path:
88
+            if (parsed_url.netloc == google_hostname
89
+                    and parsed_url.path == images_path):
82
                 # images result
90
                 # images result
83
                 results = results + parse_images(result)
91
                 results = results + parse_images(result)
84
             else:
92
             else:
85
                 # normal result
93
                 # normal result
86
                 content = extract_text(result.xpath(content_xpath)[0])
94
                 content = extract_text(result.xpath(content_xpath)[0])
87
                 # append result
95
                 # append result
88
-                results.append({'url': url, 
89
-                                'title': title, 
96
+                results.append({'url': url,
97
+                                'title': title,
90
                                 'content': content})
98
                                 'content': content})
91
         except:
99
         except:
92
             continue
100
             continue
99
     # return results
107
     # return results
100
     return results
108
     return results
101
 
109
 
110
+
102
 def parse_images(result):
111
 def parse_images(result):
103
     results = []
112
     results = []
104
     for image in result.xpath(images_xpath):
113
     for image in result.xpath(images_xpath):
105
         url = parse_url(extract_text(image.xpath(image_url_xpath)[0]))
114
         url = parse_url(extract_text(image.xpath(image_url_xpath)[0]))
106
         img_src = extract_text(image.xpath(image_img_src_xpath)[0])
115
         img_src = extract_text(image.xpath(image_img_src_xpath)[0])
107
-        
116
+
108
         # append result
117
         # append result
109
         results.append({'url': url,
118
         results.append({'url': url,
110
                         'title': '',
119
                         'title': '',