google_images.py 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 urllib import urlencode
  11. from urlparse import parse_qs
  12. from json import loads
  13. from lxml import html
  14. # engine dependent config
  15. categories = ['images']
  16. paging = True
  17. safesearch = True
  18. time_range_support = True
  19. search_url = 'https://www.google.com/search'\
  20. '?{query}'\
  21. '&tbm=isch'\
  22. '&ijn=1'\
  23. '&start={offset}'
  24. time_range_search = "&tbs=qdr:{range}"
  25. time_range_dict = {'day': 'd',
  26. 'week': 'w',
  27. 'month': 'm'}
  28. # do search-request
  29. def request(query, params):
  30. offset = (params['pageno'] - 1) * 100
  31. params['url'] = search_url.format(query=urlencode({'q': query}),
  32. offset=offset,
  33. safesearch=safesearch)
  34. if params['time_range']:
  35. params['url'] += time_range_search.format(range=time_range_dict[params['time_range']])
  36. if safesearch and params['safesearch']:
  37. params['url'] += '&' + urlencode({'safe': 'active'})
  38. return params
  39. # get response from search-request
  40. def response(resp):
  41. results = []
  42. dom = html.fromstring(resp.text)
  43. # parse results
  44. for result in dom.xpath('//div[@data-ved]'):
  45. metadata = loads(result.xpath('./div[@class="rg_meta"]/text()')[0])
  46. thumbnail_src = metadata['tu']
  47. # http to https
  48. thumbnail_src = thumbnail_src.replace("http://", "https://")
  49. # append result
  50. results.append({'url': metadata['ru'],
  51. 'title': metadata['pt'],
  52. 'content': metadata['s'],
  53. 'thumbnail_src': thumbnail_src,
  54. 'img_src': metadata['ou'],
  55. 'template': 'images.html'})
  56. # return results
  57. return results