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