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.

88 lines
2.9 KiB

  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. ExtractorError,
  6. int_or_none,
  7. )
  8. class GoogleDriveIE(InfoExtractor):
  9. _VALID_URL = r'https?://(?:(?:docs|drive)\.google\.com/(?:uc\?.*?id=|file/d/)|video\.google\.com/get_player\?.*?docid=)(?P<id>[a-zA-Z0-9_-]{28})'
  10. _TEST = {
  11. 'url': 'https://drive.google.com/file/d/0ByeS4oOUV-49Zzh4R1J6R09zazQ/edit?pli=1',
  12. 'md5': '881f7700aec4f538571fa1e0eed4a7b6',
  13. 'info_dict': {
  14. 'id': '0ByeS4oOUV-49Zzh4R1J6R09zazQ',
  15. 'ext': 'mp4',
  16. 'title': 'Big Buck Bunny.mp4',
  17. 'duration': 46,
  18. }
  19. }
  20. _FORMATS_EXT = {
  21. '5': 'flv',
  22. '6': 'flv',
  23. '13': '3gp',
  24. '17': '3gp',
  25. '18': 'mp4',
  26. '22': 'mp4',
  27. '34': 'flv',
  28. '35': 'flv',
  29. '36': '3gp',
  30. '37': 'mp4',
  31. '38': 'mp4',
  32. '43': 'webm',
  33. '44': 'webm',
  34. '45': 'webm',
  35. '46': 'webm',
  36. '59': 'mp4',
  37. }
  38. @staticmethod
  39. def _extract_url(webpage):
  40. mobj = re.search(
  41. r'<iframe[^>]+src="https?://(?:video\.google\.com/get_player\?.*?docid=|(?:docs|drive)\.google\.com/file/d/)(?P<id>[a-zA-Z0-9_-]{28})',
  42. webpage)
  43. if mobj:
  44. return 'https://drive.google.com/file/d/%s' % mobj.group('id')
  45. def _real_extract(self, url):
  46. video_id = self._match_id(url)
  47. webpage = self._download_webpage(
  48. 'http://docs.google.com/file/d/%s' % video_id, video_id, encoding='unicode_escape')
  49. reason = self._search_regex(r'"reason"\s*,\s*"([^"]+)', webpage, 'reason', default=None)
  50. if reason:
  51. raise ExtractorError(reason)
  52. title = self._search_regex(r'"title"\s*,\s*"([^"]+)', webpage, 'title')
  53. duration = int_or_none(self._search_regex(
  54. r'"length_seconds"\s*,\s*"([^"]+)', webpage, 'length seconds', default=None))
  55. fmt_stream_map = self._search_regex(
  56. r'"fmt_stream_map"\s*,\s*"([^"]+)', webpage, 'fmt stream map').split(',')
  57. fmt_list = self._search_regex(r'"fmt_list"\s*,\s*"([^"]+)', webpage, 'fmt_list').split(',')
  58. formats = []
  59. for fmt, fmt_stream in zip(fmt_list, fmt_stream_map):
  60. fmt_id, fmt_url = fmt_stream.split('|')
  61. resolution = fmt.split('/')[1]
  62. width, height = resolution.split('x')
  63. formats.append({
  64. 'url': fmt_url,
  65. 'format_id': fmt_id,
  66. 'resolution': resolution,
  67. 'width': int_or_none(width),
  68. 'height': int_or_none(height),
  69. 'ext': self._FORMATS_EXT[fmt_id],
  70. })
  71. self._sort_formats(formats)
  72. return {
  73. 'id': video_id,
  74. 'title': title,
  75. 'thumbnail': self._og_search_thumbnail(webpage, default=None),
  76. 'duration': duration,
  77. 'formats': formats,
  78. }