Browse Source

[fix] google images paging - closes #571

Adam Tauber 8 years ago
parent
commit
eb57481450
2 changed files with 24 additions and 33 deletions
  1. 23 11
      searx/engines/google_images.py
  2. 1 22
      tests/unit/engines/test_google_images.py

+ 23 - 11
searx/engines/google_images.py

@@ -19,13 +19,16 @@ categories = ['images']
 paging = True
 safesearch = True
 time_range_support = True
+number_of_results = 100
 
 search_url = 'https://www.google.com/search'\
     '?{query}'\
+    '&asearch=ichunk'\
+    '&async=_id:rg_s,_pms:s'\
     '&tbm=isch'\
-    '&ijn=1'\
-    '&start={offset}'
-time_range_search = "&tbs=qdr:{range}"
+    '&yv=2'\
+    '&{search_options}'
+time_range_attr = "qdr:{range}"
 time_range_dict = {'day': 'd',
                    'week': 'w',
                    'month': 'm'}
@@ -33,16 +36,20 @@ time_range_dict = {'day': 'd',
 
 # do search-request
 def request(query, params):
-    offset = (params['pageno'] - 1) * 100
 
-    params['url'] = search_url.format(query=urlencode({'q': query}),
-                                      offset=offset,
-                                      safesearch=safesearch)
+    search_options = {
+        'ijn': params['pageno'] - 1,
+        'start': (params['pageno'] - 1) * number_of_results
+    }
+
     if params['time_range'] in time_range_dict:
-        params['url'] += time_range_search.format(range=time_range_dict[params['time_range']])
+        search_options['tbs'] = time_range_attr.format(range=time_range_dict[params['time_range']])
 
     if safesearch and params['safesearch']:
-        params['url'] += '&' + urlencode({'safe': 'active'})
+        search_options['safe'] = 'on'
+
+    params['url'] = search_url.format(query=urlencode({'q': query}),
+                                      search_options=urlencode(search_options))
 
     return params
 
@@ -51,12 +58,17 @@ def request(query, params):
 def response(resp):
     results = []
 
-    dom = html.fromstring(resp.text)
+    g_result = loads(resp.text)
+
+    dom = html.fromstring(g_result[1][1])
 
     # parse results
     for result in dom.xpath('//div[@data-ved]'):
 
-        metadata = loads(result.xpath('./div[@class="rg_meta"]/text()')[0])
+        try:
+            metadata = loads(''.join(result.xpath('./div[@class="rg_meta"]/text()')))
+        except:
+            continue
 
         thumbnail_src = metadata['tu']
 

File diff suppressed because it is too large
+ 1 - 22
tests/unit/engines/test_google_images.py


Some files were not shown because too many files changed in this diff