googledrive.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. lowercase_escape,
  8. update_url_query,
  9. )
  10. class GoogleDriveIE(InfoExtractor):
  11. _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,})'
  12. _TESTS = [{
  13. 'url': 'https://drive.google.com/file/d/0ByeS4oOUV-49Zzh4R1J6R09zazQ/edit?pli=1',
  14. 'md5': 'd109872761f7e7ecf353fa108c0dbe1e',
  15. 'info_dict': {
  16. 'id': '0ByeS4oOUV-49Zzh4R1J6R09zazQ',
  17. 'ext': 'mp4',
  18. 'title': 'Big Buck Bunny.mp4',
  19. 'duration': 45,
  20. }
  21. }, {
  22. # video id is longer than 28 characters
  23. 'url': 'https://drive.google.com/file/d/1ENcQ_jeCuj7y19s66_Ou9dRP4GKGsodiDQ/edit',
  24. 'md5': 'c230c67252874fddd8170e3fd1a45886',
  25. 'info_dict': {
  26. 'id': '1ENcQ_jeCuj7y19s66_Ou9dRP4GKGsodiDQ',
  27. 'ext': 'mp4',
  28. 'title': 'Andreea Banica feat Smiley - Hooky Song (Official Video).mp4',
  29. 'duration': 189,
  30. },
  31. 'only_matching': True
  32. }]
  33. _FORMATS_EXT = {
  34. '5': 'flv',
  35. '6': 'flv',
  36. '13': '3gp',
  37. '17': '3gp',
  38. '18': 'mp4',
  39. '22': 'mp4',
  40. '34': 'flv',
  41. '35': 'flv',
  42. '36': '3gp',
  43. '37': 'mp4',
  44. '38': 'mp4',
  45. '43': 'webm',
  46. '44': 'webm',
  47. '45': 'webm',
  48. '46': 'webm',
  49. '59': 'mp4',
  50. }
  51. _BASE_URL_CAPTIONS = 'https://drive.google.com/timedtext'
  52. _CAPTIONS_ENTRY_TAG = {
  53. 'subtitles': 'track',
  54. 'automatic_captions': 'target',
  55. }
  56. _caption_formats_ext = []
  57. _captions_xml = None
  58. @staticmethod
  59. def _extract_url(webpage):
  60. mobj = re.search(
  61. r'<iframe[^>]+src="https?://(?:video\.google\.com/get_player\?.*?docid=|(?:docs|drive)\.google\.com/file/d/)(?P<id>[a-zA-Z0-9_-]{28,})',
  62. webpage)
  63. if mobj:
  64. return 'https://drive.google.com/file/d/%s' % mobj.group('id')
  65. def _download_subtitles_xml(self, video_id, subtitles_id, hl):
  66. if self._captions_xml:
  67. return
  68. self._captions_xml = self._download_xml(
  69. self._BASE_URL_CAPTIONS, video_id, query={
  70. 'id': video_id,
  71. 'vid': subtitles_id,
  72. 'hl': hl,
  73. 'v': video_id,
  74. 'type': 'list',
  75. 'tlangs': '1',
  76. 'fmts': '1',
  77. 'vssids': '1',
  78. }, note='Downloading subtitles XML',
  79. errnote='Unable to download subtitles XML', fatal=False)
  80. if self._captions_xml:
  81. for f in self._captions_xml.findall('format'):
  82. if f.attrib.get('fmt_code') and not f.attrib.get('default'):
  83. self._caption_formats_ext.append(f.attrib['fmt_code'])
  84. def _get_captions_by_type(self, video_id, subtitles_id, caption_type,
  85. origin_lang_code=None):
  86. if not subtitles_id or not caption_type:
  87. return
  88. captions = {}
  89. for caption_entry in self._captions_xml.findall(
  90. self._CAPTIONS_ENTRY_TAG[caption_type]):
  91. caption_lang_code = caption_entry.attrib.get('lang_code')
  92. if not caption_lang_code:
  93. continue
  94. caption_format_data = []
  95. for caption_format in self._caption_formats_ext:
  96. query = {
  97. 'vid': subtitles_id,
  98. 'v': video_id,
  99. 'fmt': caption_format,
  100. 'lang': (caption_lang_code if origin_lang_code is None
  101. else origin_lang_code),
  102. 'type': 'track',
  103. 'name': '',
  104. 'kind': '',
  105. }
  106. if origin_lang_code is not None:
  107. query.update({'tlang': caption_lang_code})
  108. caption_format_data.append({
  109. 'url': update_url_query(self._BASE_URL_CAPTIONS, query),
  110. 'ext': caption_format,
  111. })
  112. captions[caption_lang_code] = caption_format_data
  113. return captions
  114. def _get_subtitles(self, video_id, subtitles_id, hl):
  115. if not subtitles_id or not hl:
  116. return
  117. self._download_subtitles_xml(video_id, subtitles_id, hl)
  118. if not self._captions_xml:
  119. return
  120. return self._get_captions_by_type(video_id, subtitles_id, 'subtitles')
  121. def _get_automatic_captions(self, video_id, subtitles_id, hl):
  122. if not subtitles_id or not hl:
  123. return
  124. self._download_subtitles_xml(video_id, subtitles_id, hl)
  125. if not self._captions_xml:
  126. return
  127. track = self._captions_xml.find('track')
  128. if track is None:
  129. return
  130. origin_lang_code = track.attrib.get('lang_code')
  131. if not origin_lang_code:
  132. return
  133. return self._get_captions_by_type(
  134. video_id, subtitles_id, 'automatic_captions', origin_lang_code)
  135. def _real_extract(self, url):
  136. video_id = self._match_id(url)
  137. webpage = self._download_webpage(
  138. 'http://docs.google.com/file/d/%s' % video_id, video_id)
  139. reason = self._search_regex(
  140. r'"reason"\s*,\s*"([^"]+)', webpage, 'reason', default=None)
  141. if reason:
  142. raise ExtractorError(reason)
  143. title = self._search_regex(r'"title"\s*,\s*"([^"]+)', webpage, 'title')
  144. duration = int_or_none(self._search_regex(
  145. r'"length_seconds"\s*,\s*"([^"]+)', webpage, 'length seconds',
  146. default=None))
  147. fmt_stream_map = self._search_regex(
  148. r'"fmt_stream_map"\s*,\s*"([^"]+)', webpage,
  149. 'fmt stream map').split(',')
  150. fmt_list = self._search_regex(
  151. r'"fmt_list"\s*,\s*"([^"]+)', webpage, 'fmt_list').split(',')
  152. resolutions = {}
  153. for fmt in fmt_list:
  154. mobj = re.search(
  155. r'^(?P<format_id>\d+)/(?P<width>\d+)[xX](?P<height>\d+)', fmt)
  156. if mobj:
  157. resolutions[mobj.group('format_id')] = (
  158. int(mobj.group('width')), int(mobj.group('height')))
  159. formats = []
  160. for fmt_stream in fmt_stream_map:
  161. fmt_stream_split = fmt_stream.split('|')
  162. if len(fmt_stream_split) < 2:
  163. continue
  164. format_id, format_url = fmt_stream_split[:2]
  165. f = {
  166. 'url': lowercase_escape(format_url),
  167. 'format_id': format_id,
  168. 'ext': self._FORMATS_EXT[format_id],
  169. }
  170. resolution = resolutions.get(format_id)
  171. if resolution:
  172. f.update({
  173. 'width': resolution[0],
  174. 'height': resolution[1],
  175. })
  176. formats.append(f)
  177. self._sort_formats(formats)
  178. hl = self._search_regex(
  179. r'"hl"\s*,\s*"([^"]+)', webpage, 'hl', default=None)
  180. subtitles_id = None
  181. ttsurl = self._search_regex(
  182. r'"ttsurl"\s*,\s*"([^"]+)', webpage, 'ttsurl', default=None)
  183. if ttsurl:
  184. # the video Id for subtitles will be the last value in the ttsurl
  185. # query string
  186. subtitles_id = ttsurl.encode('utf-8').decode(
  187. 'unicode_escape').split('=')[-1]
  188. return {
  189. 'id': video_id,
  190. 'title': title,
  191. 'thumbnail': self._og_search_thumbnail(webpage, default=None),
  192. 'duration': duration,
  193. 'formats': formats,
  194. 'subtitles': self.extract_subtitles(video_id, subtitles_id, hl),
  195. 'automatic_captions': self.extract_automatic_captions(
  196. video_id, subtitles_id, hl),
  197. }