google_videos.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. """
  2. Google (Videos)
  3. @website https://www.google.com
  4. @provide-api yes (https://developers.google.com/custom-search/)
  5. @using-api no
  6. @results HTML
  7. @stable no
  8. @parse url, title, content, thumbnail
  9. """
  10. from datetime import date, timedelta
  11. from urllib.parse import urlencode
  12. from lxml import html
  13. from searx.utils import extract_text
  14. import re
  15. # engine dependent config
  16. categories = ['videos']
  17. paging = True
  18. safesearch = True
  19. time_range_support = True
  20. number_of_results = 10
  21. search_url = 'https://www.google.com/search'\
  22. '?q={query}'\
  23. '&tbm=vid'\
  24. '&{search_options}'
  25. time_range_attr = "qdr:{range}"
  26. time_range_custom_attr = "cdr:1,cd_min:{start},cd_max{end}"
  27. time_range_dict = {'day': 'd',
  28. 'week': 'w',
  29. 'month': 'm'}
  30. # do search-request
  31. def request(query, params):
  32. search_options = {
  33. 'ijn': params['pageno'] - 1,
  34. 'start': (params['pageno'] - 1) * number_of_results
  35. }
  36. if params['time_range'] in time_range_dict:
  37. search_options['tbs'] = time_range_attr.format(range=time_range_dict[params['time_range']])
  38. elif params['time_range'] == 'year':
  39. now = date.today()
  40. then = now - timedelta(days=365)
  41. start = then.strftime('%m/%d/%Y')
  42. end = now.strftime('%m/%d/%Y')
  43. search_options['tbs'] = time_range_custom_attr.format(start=start, end=end)
  44. if safesearch and params['safesearch']:
  45. search_options['safe'] = 'on'
  46. params['url'] = search_url.format(query=urlencode({'q': query}),
  47. search_options=urlencode(search_options))
  48. return params
  49. # get response from search-request
  50. def response(resp):
  51. results = []
  52. dom = html.fromstring(resp.text)
  53. # parse results
  54. for result in dom.xpath('//div[@class="g"]'):
  55. title = extract_text(result.xpath('.//h3'))
  56. url = result.xpath('.//div[@class="r"]/a/@href')[0]
  57. content = extract_text(result.xpath('.//span[@class="st"]'))
  58. # get thumbnails
  59. script = str(dom.xpath('//script[contains(., "_setImagesSrc")]')[0].text)
  60. ids = result.xpath('.//div[@class="s"]//img/@id')
  61. if len(ids) > 0:
  62. thumbnails_data = \
  63. re.findall('s=\'(.*?)(?:\\\\[a-z,1-9,\\\\]+\'|\')\;var ii=\[(?:|[\'vidthumb\d+\',]+)\'' + ids[0],
  64. script)
  65. tmp = []
  66. if len(thumbnails_data) != 0:
  67. tmp = re.findall('(data:image/jpeg;base64,[a-z,A-Z,0-9,/,\+]+)', thumbnails_data[0])
  68. thumbnail = ''
  69. if len(tmp) != 0:
  70. thumbnail = tmp[-1]
  71. # append result
  72. results.append({'url': url,
  73. 'title': title,
  74. 'content': content,
  75. 'thumbnail': thumbnail,
  76. 'template': 'videos.html'})
  77. return results