You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.7 KiB

  1. from __future__ import unicode_literals
  2. import itertools
  3. import re
  4. from .common import SearchInfoExtractor
  5. from ..compat import (
  6. compat_urllib_parse,
  7. )
  8. class GoogleSearchIE(SearchInfoExtractor):
  9. IE_DESC = 'Google Video search'
  10. _MAX_RESULTS = 1000
  11. IE_NAME = 'video.google:search'
  12. _SEARCH_KEY = 'gvsearch'
  13. _TEST = {
  14. 'url': 'gvsearch15:python language',
  15. 'info_dict': {
  16. 'id': 'python language',
  17. 'title': 'python language',
  18. },
  19. 'playlist_count': 15,
  20. }
  21. def _get_n_results(self, query, n):
  22. """Get a specified number of results for a query"""
  23. entries = []
  24. res = {
  25. '_type': 'playlist',
  26. 'id': query,
  27. 'title': query,
  28. }
  29. for pagenum in itertools.count():
  30. result_url = (
  31. 'http://www.google.com/search?tbm=vid&q=%s&start=%s&hl=en'
  32. % (compat_urllib_parse.quote_plus(query), pagenum * 10))
  33. webpage = self._download_webpage(
  34. result_url, 'gvsearch:' + query,
  35. note='Downloading result page ' + str(pagenum + 1))
  36. for hit_idx, mobj in enumerate(re.finditer(
  37. r'<h3 class="r"><a href="([^"]+)"', webpage)):
  38. # Skip playlists
  39. if not re.search(r'id="vidthumb%d"' % (hit_idx + 1), webpage):
  40. continue
  41. entries.append({
  42. '_type': 'url',
  43. 'url': mobj.group(1)
  44. })
  45. if (len(entries) >= n) or not re.search(r'id="pnnext"', webpage):
  46. res['entries'] = entries[:n]
  47. return res