logo

searx

My custom branche(s) on searx, a meta-search engine git clone https://hacktivis.me/git/searx.git

google_images.py (2321B)


  1. """
  2. Google (Images)
  3. @website https://www.google.com
  4. @provide-api yes (https://developers.google.com/custom-search/)
  5. @using-api no
  6. @results HTML chunks with JSON inside
  7. @stable no
  8. @parse url, title, img_src
  9. """
  10. from datetime import date, timedelta
  11. from json import loads
  12. from lxml import html
  13. from searx.url_utils import urlencode, urlparse, parse_qs
  14. # engine dependent config
  15. categories = ['images']
  16. paging = True
  17. safesearch = True
  18. time_range_support = True
  19. number_of_results = 100
  20. search_url = 'https://www.google.com/search'\
  21. '?{query}'\
  22. '&tbm=isch'\
  23. '&gbv=1'\
  24. '&sa=G'\
  25. '&{search_options}'
  26. time_range_attr = "qdr:{range}"
  27. time_range_custom_attr = "cdr:1,cd_min:{start},cd_max{end}"
  28. time_range_dict = {'day': 'd',
  29. 'week': 'w',
  30. 'month': 'm'}
  31. # do search-request
  32. def request(query, params):
  33. search_options = {
  34. 'ijn': params['pageno'] - 1,
  35. 'start': (params['pageno'] - 1) * number_of_results
  36. }
  37. if params['time_range'] in time_range_dict:
  38. search_options['tbs'] = time_range_attr.format(range=time_range_dict[params['time_range']])
  39. elif params['time_range'] == 'year':
  40. now = date.today()
  41. then = now - timedelta(days=365)
  42. start = then.strftime('%m/%d/%Y')
  43. end = now.strftime('%m/%d/%Y')
  44. search_options['tbs'] = time_range_custom_attr.format(start=start, end=end)
  45. if safesearch and params['safesearch']:
  46. search_options['safe'] = 'on'
  47. params['url'] = search_url.format(query=urlencode({'q': query}),
  48. search_options=urlencode(search_options))
  49. return params
  50. # get response from search-request
  51. def response(resp):
  52. results = []
  53. dom = html.fromstring(resp.text)
  54. # parse results
  55. for img in dom.xpath('//a'):
  56. r = {
  57. 'title': u' '.join(img.xpath('.//div[class="rg_ilmbg"]//text()')),
  58. 'content': '',
  59. 'template': 'images.html',
  60. }
  61. url = urlparse(img.xpath('.//@href')[0])
  62. query = parse_qs(url.query)
  63. r['url'] = query['imgrefurl'][0]
  64. r['img_src'] = query['imgurl'][0]
  65. r['thumbnail_src'] = r['img_src']
  66. # append result
  67. results.append(r)
  68. # return results
  69. return results