google_videos.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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
  9. """
  10. from datetime import date, timedelta
  11. from json import loads
  12. from lxml import html
  13. from searx.engines.xpath import extract_text
  14. from searx.url_utils import urlencode
  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. '?{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/a'))
  56. url = result.xpath('.//h3/a/@href')[0]
  57. content = extract_text(result.xpath('.//span[@class="st"]'))
  58. # append result
  59. results.append({'url': url,
  60. 'title': title,
  61. 'content': content,
  62. 'thumbnail': '',
  63. 'template': 'videos.html'})
  64. return results