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.

51 lines
1.5 KiB

  1. from __future__ import unicode_literals
  2. import itertools
  3. import re
  4. from .common import SearchInfoExtractor
  5. from ..utils 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. def _get_n_results(self, query, n):
  14. """Get a specified number of results for a query"""
  15. entries = []
  16. res = {
  17. '_type': 'playlist',
  18. 'id': query,
  19. 'title': query,
  20. }
  21. for pagenum in itertools.count():
  22. result_url = (
  23. 'http://www.google.com/search?tbm=vid&q=%s&start=%s&hl=en'
  24. % (compat_urllib_parse.quote_plus(query), pagenum * 10))
  25. webpage = self._download_webpage(
  26. result_url, 'gvsearch:' + query,
  27. note='Downloading result page ' + str(pagenum + 1))
  28. for hit_idx, mobj in enumerate(re.finditer(
  29. r'<h3 class="r"><a href="([^"]+)"', webpage)):
  30. # Skip playlists
  31. if not re.search(r'id="vidthumb%d"' % (hit_idx + 1), webpage):
  32. continue
  33. entries.append({
  34. '_type': 'url',
  35. 'url': mobj.group(1)
  36. })
  37. if (len(entries) >= n) or not re.search(r'id="pnnext"', webpage):
  38. res['entries'] = entries[:n]
  39. return res