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.

93 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. 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. formats = []
  64. for fmt, fmt_stream in zip(fmt_list, fmt_stream_map):
  65. fmt_id, fmt_url = fmt_stream.split('|')
  66. resolution = fmt.split('/')[1]
  67. width, height = resolution.split('x')
  68. formats.append({
  69. 'url': lowercase_escape(fmt_url),
  70. 'format_id': fmt_id,
  71. 'resolution': resolution,
  72. 'width': int_or_none(width),
  73. 'height': int_or_none(height),
  74. 'ext': self._FORMATS_EXT[fmt_id],
  75. })
  76. self._sort_formats(formats)
  77. return {
  78. 'id': video_id,
  79. 'title': title,
  80. 'thumbnail': self._og_search_thumbnail(webpage, default=None),
  81. 'duration': duration,
  82. 'formats': formats,
  83. }