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.

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