googledrive.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. determine_ext,
  6. ExtractorError,
  7. int_or_none,
  8. lowercase_escape,
  9. update_url_query,
  10. )
  11. class GoogleDriveIE(InfoExtractor):
  12. _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,})'
  13. _TESTS = [{
  14. 'url': 'https://drive.google.com/file/d/0ByeS4oOUV-49Zzh4R1J6R09zazQ/edit?pli=1',
  15. 'md5': '5c602afbbf2c1db91831f5d82f678554',
  16. 'info_dict': {
  17. 'id': '0ByeS4oOUV-49Zzh4R1J6R09zazQ',
  18. 'ext': 'mp4',
  19. 'title': 'Big Buck Bunny.mp4',
  20. 'duration': 45,
  21. }
  22. }, {
  23. # video can't be watched anonymously due to view count limit reached,
  24. # but can be downloaded (see https://github.com/rg3/youtube-dl/issues/14046)
  25. 'url': 'https://drive.google.com/file/d/0B-vUyvmDLdWDcEt4WjBqcmI2XzQ/view',
  26. 'md5': 'bfbd670d03a470bb1e6d4a257adec12e',
  27. 'info_dict': {
  28. 'id': '0B-vUyvmDLdWDcEt4WjBqcmI2XzQ',
  29. 'ext': 'mp4',
  30. 'title': 'Annabelle Creation (2017)- Z.V1 [TH].MP4',
  31. }
  32. }, {
  33. # video id is longer than 28 characters
  34. 'url': 'https://drive.google.com/file/d/1ENcQ_jeCuj7y19s66_Ou9dRP4GKGsodiDQ/edit',
  35. 'info_dict': {
  36. 'id': '1ENcQ_jeCuj7y19s66_Ou9dRP4GKGsodiDQ',
  37. 'ext': 'mp4',
  38. 'title': 'Andreea Banica feat Smiley - Hooky Song (Official Video).mp4',
  39. 'duration': 189,
  40. },
  41. 'only_matching': True
  42. }]
  43. _FORMATS_EXT = {
  44. '5': 'flv',
  45. '6': 'flv',
  46. '13': '3gp',
  47. '17': '3gp',
  48. '18': 'mp4',
  49. '22': 'mp4',
  50. '34': 'flv',
  51. '35': 'flv',
  52. '36': '3gp',
  53. '37': 'mp4',
  54. '38': 'mp4',
  55. '43': 'webm',
  56. '44': 'webm',
  57. '45': 'webm',
  58. '46': 'webm',
  59. '59': 'mp4',
  60. }
  61. _BASE_URL_CAPTIONS = 'https://drive.google.com/timedtext'
  62. _CAPTIONS_ENTRY_TAG = {
  63. 'subtitles': 'track',
  64. 'automatic_captions': 'target',
  65. }
  66. _caption_formats_ext = []
  67. _captions_xml = None
  68. @staticmethod
  69. def _extract_url(webpage):
  70. mobj = re.search(
  71. r'<iframe[^>]+src="https?://(?:video\.google\.com/get_player\?.*?docid=|(?:docs|drive)\.google\.com/file/d/)(?P<id>[a-zA-Z0-9_-]{28,})',
  72. webpage)
  73. if mobj:
  74. return 'https://drive.google.com/file/d/%s' % mobj.group('id')
  75. def _download_subtitles_xml(self, video_id, subtitles_id, hl):
  76. if self._captions_xml:
  77. return
  78. self._captions_xml = self._download_xml(
  79. self._BASE_URL_CAPTIONS, video_id, query={
  80. 'id': video_id,
  81. 'vid': subtitles_id,
  82. 'hl': hl,
  83. 'v': video_id,
  84. 'type': 'list',
  85. 'tlangs': '1',
  86. 'fmts': '1',
  87. 'vssids': '1',
  88. }, note='Downloading subtitles XML',
  89. errnote='Unable to download subtitles XML', fatal=False)
  90. if self._captions_xml:
  91. for f in self._captions_xml.findall('format'):
  92. if f.attrib.get('fmt_code') and not f.attrib.get('default'):
  93. self._caption_formats_ext.append(f.attrib['fmt_code'])
  94. def _get_captions_by_type(self, video_id, subtitles_id, caption_type,
  95. origin_lang_code=None):
  96. if not subtitles_id or not caption_type:
  97. return
  98. captions = {}
  99. for caption_entry in self._captions_xml.findall(
  100. self._CAPTIONS_ENTRY_TAG[caption_type]):
  101. caption_lang_code = caption_entry.attrib.get('lang_code')
  102. if not caption_lang_code:
  103. continue
  104. caption_format_data = []
  105. for caption_format in self._caption_formats_ext:
  106. query = {
  107. 'vid': subtitles_id,
  108. 'v': video_id,
  109. 'fmt': caption_format,
  110. 'lang': (caption_lang_code if origin_lang_code is None
  111. else origin_lang_code),
  112. 'type': 'track',
  113. 'name': '',
  114. 'kind': '',
  115. }
  116. if origin_lang_code is not None:
  117. query.update({'tlang': caption_lang_code})
  118. caption_format_data.append({
  119. 'url': update_url_query(self._BASE_URL_CAPTIONS, query),
  120. 'ext': caption_format,
  121. })
  122. captions[caption_lang_code] = caption_format_data
  123. return captions
  124. def _get_subtitles(self, video_id, subtitles_id, hl):
  125. if not subtitles_id or not hl:
  126. return
  127. self._download_subtitles_xml(video_id, subtitles_id, hl)
  128. if not self._captions_xml:
  129. return
  130. return self._get_captions_by_type(video_id, subtitles_id, 'subtitles')
  131. def _get_automatic_captions(self, video_id, subtitles_id, hl):
  132. if not subtitles_id or not hl:
  133. return
  134. self._download_subtitles_xml(video_id, subtitles_id, hl)
  135. if not self._captions_xml:
  136. return
  137. track = self._captions_xml.find('track')
  138. if track is None:
  139. return
  140. origin_lang_code = track.attrib.get('lang_code')
  141. if not origin_lang_code:
  142. return
  143. return self._get_captions_by_type(
  144. video_id, subtitles_id, 'automatic_captions', origin_lang_code)
  145. def _real_extract(self, url):
  146. video_id = self._match_id(url)
  147. webpage = self._download_webpage(
  148. 'http://docs.google.com/file/d/%s' % video_id, video_id)
  149. title = self._search_regex(
  150. r'"title"\s*,\s*"([^"]+)', webpage, 'title',
  151. default=None) or self._og_search_title(webpage)
  152. duration = int_or_none(self._search_regex(
  153. r'"length_seconds"\s*,\s*"([^"]+)', webpage, 'length seconds',
  154. default=None))
  155. formats = []
  156. fmt_stream_map = self._search_regex(
  157. r'"fmt_stream_map"\s*,\s*"([^"]+)', webpage,
  158. 'fmt stream map', default='').split(',')
  159. fmt_list = self._search_regex(
  160. r'"fmt_list"\s*,\s*"([^"]+)', webpage,
  161. 'fmt_list', default='').split(',')
  162. if fmt_stream_map and fmt_list:
  163. resolutions = {}
  164. for fmt in fmt_list:
  165. mobj = re.search(
  166. r'^(?P<format_id>\d+)/(?P<width>\d+)[xX](?P<height>\d+)', fmt)
  167. if mobj:
  168. resolutions[mobj.group('format_id')] = (
  169. int(mobj.group('width')), int(mobj.group('height')))
  170. for fmt_stream in fmt_stream_map:
  171. fmt_stream_split = fmt_stream.split('|')
  172. if len(fmt_stream_split) < 2:
  173. continue
  174. format_id, format_url = fmt_stream_split[:2]
  175. f = {
  176. 'url': lowercase_escape(format_url),
  177. 'format_id': format_id,
  178. 'ext': self._FORMATS_EXT[format_id],
  179. }
  180. resolution = resolutions.get(format_id)
  181. if resolution:
  182. f.update({
  183. 'width': resolution[0],
  184. 'height': resolution[1],
  185. })
  186. formats.append(f)
  187. source_url = update_url_query(
  188. 'https://drive.google.com/uc', {
  189. 'id': video_id,
  190. 'export': 'download',
  191. })
  192. urlh = self._request_webpage(
  193. source_url, video_id, note='Requesting source file',
  194. errnote='Unable to request source file', fatal=False)
  195. if urlh:
  196. def add_source_format(src_url):
  197. formats.append({
  198. 'url': src_url,
  199. 'ext': determine_ext(title, 'mp4').lower(),
  200. 'format_id': 'source',
  201. 'quality': 1,
  202. })
  203. if urlh.headers.get('Content-Disposition'):
  204. add_source_format(source_url)
  205. else:
  206. confirmation_webpage = self._webpage_read_content(
  207. urlh, url, video_id, note='Downloading confirmation page',
  208. errnote='Unable to confirm download', fatal=False)
  209. if confirmation_webpage:
  210. confirm = self._search_regex(
  211. r'confirm=([^&"\']+)', confirmation_webpage,
  212. 'confirmation code', fatal=False)
  213. if confirm:
  214. add_source_format(update_url_query(source_url, {
  215. 'confirm': confirm,
  216. }))
  217. if not formats:
  218. reason = self._search_regex(
  219. r'"reason"\s*,\s*"([^"]+)', webpage, 'reason', default=None)
  220. if reason:
  221. raise ExtractorError(reason, expected=True)
  222. self._sort_formats(formats)
  223. hl = self._search_regex(
  224. r'"hl"\s*,\s*"([^"]+)', webpage, 'hl', default=None)
  225. subtitles_id = None
  226. ttsurl = self._search_regex(
  227. r'"ttsurl"\s*,\s*"([^"]+)', webpage, 'ttsurl', default=None)
  228. if ttsurl:
  229. # the video Id for subtitles will be the last value in the ttsurl
  230. # query string
  231. subtitles_id = ttsurl.encode('utf-8').decode(
  232. 'unicode_escape').split('=')[-1]
  233. return {
  234. 'id': video_id,
  235. 'title': title,
  236. 'thumbnail': self._og_search_thumbnail(webpage, default=None),
  237. 'duration': duration,
  238. 'formats': formats,
  239. 'subtitles': self.extract_subtitles(video_id, subtitles_id, hl),
  240. 'automatic_captions': self.extract_automatic_captions(
  241. video_id, subtitles_id, hl),
  242. }