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.

277 lines
10 KiB

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