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.

106 lines
3.5 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. lowercase_escape,
  8. )
  9. class GoogleDriveIE(InfoExtractor):
  10. _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,})'
  11. _TESTS = [{
  12. 'url': 'https://drive.google.com/file/d/0ByeS4oOUV-49Zzh4R1J6R09zazQ/edit?pli=1',
  13. 'md5': 'd109872761f7e7ecf353fa108c0dbe1e',
  14. 'info_dict': {
  15. 'id': '0ByeS4oOUV-49Zzh4R1J6R09zazQ',
  16. 'ext': 'mp4',
  17. 'title': 'Big Buck Bunny.mp4',
  18. 'duration': 45,
  19. }
  20. }, {
  21. # video id is longer than 28 characters
  22. 'url': 'https://drive.google.com/file/d/1ENcQ_jeCuj7y19s66_Ou9dRP4GKGsodiDQ/edit',
  23. 'only_matching': True,
  24. }]
  25. _FORMATS_EXT = {
  26. '5': 'flv',
  27. '6': 'flv',
  28. '13': '3gp',
  29. '17': '3gp',
  30. '18': 'mp4',
  31. '22': 'mp4',
  32. '34': 'flv',
  33. '35': 'flv',
  34. '36': '3gp',
  35. '37': 'mp4',
  36. '38': 'mp4',
  37. '43': 'webm',
  38. '44': 'webm',
  39. '45': 'webm',
  40. '46': 'webm',
  41. '59': 'mp4',
  42. }
  43. @staticmethod
  44. def _extract_url(webpage):
  45. mobj = re.search(
  46. r'<iframe[^>]+src="https?://(?:video\.google\.com/get_player\?.*?docid=|(?:docs|drive)\.google\.com/file/d/)(?P<id>[a-zA-Z0-9_-]{28,})',
  47. webpage)
  48. if mobj:
  49. return 'https://drive.google.com/file/d/%s' % mobj.group('id')
  50. def _real_extract(self, url):
  51. video_id = self._match_id(url)
  52. webpage = self._download_webpage(
  53. 'http://docs.google.com/file/d/%s' % video_id, video_id)
  54. reason = self._search_regex(r'"reason"\s*,\s*"([^"]+)', webpage, 'reason', default=None)
  55. if reason:
  56. raise ExtractorError(reason)
  57. title = self._search_regex(r'"title"\s*,\s*"([^"]+)', webpage, 'title')
  58. duration = int_or_none(self._search_regex(
  59. r'"length_seconds"\s*,\s*"([^"]+)', webpage, 'length seconds', default=None))
  60. fmt_stream_map = self._search_regex(
  61. r'"fmt_stream_map"\s*,\s*"([^"]+)', webpage, 'fmt stream map').split(',')
  62. fmt_list = self._search_regex(r'"fmt_list"\s*,\s*"([^"]+)', webpage, 'fmt_list').split(',')
  63. resolutions = {}
  64. for fmt in fmt_list:
  65. mobj = re.search(
  66. r'^(?P<format_id>\d+)/(?P<width>\d+)[xX](?P<height>\d+)', fmt)
  67. if mobj:
  68. resolutions[mobj.group('format_id')] = (
  69. int(mobj.group('width')), int(mobj.group('height')))
  70. formats = []
  71. for fmt_stream in fmt_stream_map:
  72. fmt_stream_split = fmt_stream.split('|')
  73. if len(fmt_stream_split) < 2:
  74. continue
  75. format_id, format_url = fmt_stream_split[:2]
  76. f = {
  77. 'url': lowercase_escape(format_url),
  78. 'format_id': format_id,
  79. 'ext': self._FORMATS_EXT[format_id],
  80. }
  81. resolution = resolutions.get(format_id)
  82. if resolution:
  83. f.update({
  84. 'width': resolution[0],
  85. 'height': resolution[0],
  86. })
  87. formats.append(f)
  88. self._sort_formats(formats)
  89. return {
  90. 'id': video_id,
  91. 'title': title,
  92. 'thumbnail': self._og_search_thumbnail(webpage, default=None),
  93. 'duration': duration,
  94. 'formats': formats,
  95. }