googledrive.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import re
  2. from .common import InfoExtractor
  3. from ..utils import (
  4. RegexNotFoundError,
  5. ExtractorError,
  6. )
  7. class GoogleDriveEmbedIE(InfoExtractor):
  8. _VALID_URL = r'https?://(?:video\.google\.com/get_player\?.*?docid=|(?:docs|drive)\.google\.com/file/d/)(?P<id>[a-zA-Z0-9_-]{28})'
  9. _TEST = {
  10. 'url': 'https://docs.google.com/file/d/0B8KB9DRosYGKMXNoeWxqa3JYclE/preview',
  11. 'info_dict': {
  12. 'id': '0B8KB9DRosYGKMXNoeWxqa3JYclE',
  13. 'ext': 'mp4',
  14. 'title': 'Jimmy Fallon Sings Since You\'ve Been Gone.wmv',
  15. }
  16. }
  17. @staticmethod
  18. def _extract_url(webpage):
  19. mobj = re.search(
  20. r'<iframe src="https?://(?:video\.google\.com/get_player\?.*?docid=|(?:docs|drive)\.google\.com/file/d/)(?P<id>[a-zA-Z0-9_-]{28})',
  21. webpage)
  22. if mobj:
  23. return 'https://drive.google.com/file/d/%s' % mobj.group('id')
  24. def _real_extract(self, url):
  25. video_id = self._match_id(url)
  26. return {
  27. '_type': 'url',
  28. 'ie_key': 'GoogleDrive',
  29. 'url': 'https://drive.google.com/file/d/%s' % video_id
  30. }
  31. class GoogleDriveIE(InfoExtractor):
  32. _VALID_URL = r'https?://(?:docs|drive)\.google\.com/(?:uc\?.*?id=|file/d/)(?P<id>[a-zA-Z0-9_-]{28})'
  33. _TEST = {
  34. 'url': 'https://drive.google.com/file/d/0ByeS4oOUV-49Zzh4R1J6R09zazQ/edit?pli=1',
  35. 'info_dict': {
  36. 'id': '0ByeS4oOUV-49Zzh4R1J6R09zazQ',
  37. 'ext': 'mp4',
  38. 'title': 'Big Buck Bunny.mp4',
  39. }
  40. }
  41. _formats = {
  42. '5': {'ext': 'flv'},
  43. '6': {'ext': 'flv'},
  44. '13': {'ext': '3gp'},
  45. '17': {'ext': '3gp'},
  46. '18': {'ext': 'mp4'},
  47. '22': {'ext': 'mp4'},
  48. '34': {'ext': 'flv'},
  49. '35': {'ext': 'flv'},
  50. '36': {'ext': '3gp'},
  51. '37': {'ext': 'mp4'},
  52. '38': {'ext': 'mp4'},
  53. '43': {'ext': 'webm'},
  54. '44': {'ext': 'webm'},
  55. '45': {'ext': 'webm'},
  56. '46': {'ext': 'webm'},
  57. '59': {'ext': 'mp4'}
  58. }
  59. def _real_extract(self, url):
  60. video_id = self._match_id(url)
  61. webpage = self._download_webpage(
  62. 'http://docs.google.com/file/d/' + video_id, video_id, encoding='unicode_escape'
  63. )
  64. try:
  65. title = self._html_search_regex(
  66. r'"title"\s*,\s*"([^"]+)',
  67. webpage,
  68. 'title'
  69. )
  70. fmt_stream_map = self._html_search_regex(
  71. r'"fmt_stream_map"\s*,\s*"([^"]+)',
  72. webpage,
  73. 'fmt_stream_map'
  74. )
  75. fmt_list = self._html_search_regex(
  76. r'"fmt_list"\s*,\s*"([^"]+)',
  77. webpage,
  78. 'fmt_list'
  79. )
  80. # timestamp = self._html_search_regex(
  81. # r'"timestamp"\s*,\s*"([^"]+)',
  82. # webpage,
  83. # 'timestamp'
  84. # )
  85. length_seconds = self._html_search_regex(
  86. r'"length_seconds"\s*,\s*"([^"]+)',
  87. webpage,
  88. 'length_seconds'
  89. )
  90. except RegexNotFoundError:
  91. try:
  92. reason = self._html_search_regex(
  93. r'"reason","([^"]+)',
  94. webpage,
  95. 'reason'
  96. )
  97. raise ExtractorError(reason)
  98. return
  99. except RegexNotFoundError:
  100. raise ExtractorError('not a video')
  101. return
  102. fmt_stream_map = fmt_stream_map.split(',')
  103. fmt_list = fmt_list.split(',')
  104. formats = []
  105. for i in range(len(fmt_stream_map)):
  106. fmt_id, fmt_url = fmt_stream_map[i].split('|')
  107. resolution = fmt_list[i].split('/')[1]
  108. width, height = resolution.split('x')
  109. formats.append({
  110. 'url': fmt_url,
  111. 'format_id': fmt_id,
  112. 'resolution': resolution,
  113. 'width': int(width),
  114. 'height': int(height),
  115. 'ext': self._formats[fmt_id]['ext']
  116. })
  117. self._sort_formats(formats)
  118. return {
  119. 'id': video_id,
  120. 'title': title,
  121. # 'timestamp': int(timestamp),
  122. 'duration': int(length_seconds),
  123. 'formats': formats
  124. }