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.

128 lines
6.0 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import compat_str
  6. from ..utils import (
  7. unescapeHTML,
  8. parse_duration,
  9. get_element_by_class,
  10. )
  11. class LEGOIE(InfoExtractor):
  12. _VALID_URL = r'https?://(?:www\.)?lego\.com/(?P<locale>[^/]+)/(?:[^/]+/)*videos/(?:[^/]+/)*[^/?#]+-(?P<id>[0-9a-f]+)'
  13. _TESTS = [{
  14. 'url': 'http://www.lego.com/en-us/videos/themes/club/blocumentary-kawaguchi-55492d823b1b4d5e985787fa8c2973b1',
  15. 'md5': 'f34468f176cfd76488767fc162c405fa',
  16. 'info_dict': {
  17. 'id': '55492d823b1b4d5e985787fa8c2973b1',
  18. 'ext': 'mp4',
  19. 'title': 'Blocumentary Great Creations: Akiyuki Kawaguchi',
  20. 'description': 'Blocumentary Great Creations: Akiyuki Kawaguchi',
  21. },
  22. }, {
  23. # geo-restricted but the contentUrl contain a valid url
  24. 'url': 'http://www.lego.com/nl-nl/videos/themes/nexoknights/episode-20-kingdom-of-heroes-13bdc2299ab24d9685701a915b3d71e7##sp=399',
  25. 'md5': '4c3fec48a12e40c6e5995abc3d36cc2e',
  26. 'info_dict': {
  27. 'id': '13bdc2299ab24d9685701a915b3d71e7',
  28. 'ext': 'mp4',
  29. 'title': 'Aflevering 20 - Helden van het koninkrijk',
  30. 'description': 'md5:8ee499aac26d7fa8bcb0cedb7f9c3941',
  31. },
  32. }, {
  33. # special characters in title
  34. 'url': 'http://www.lego.com/en-us/starwars/videos/lego-star-wars-force-surprise-9685ee9d12e84ff38e84b4e3d0db533d',
  35. 'info_dict': {
  36. 'id': '9685ee9d12e84ff38e84b4e3d0db533d',
  37. 'ext': 'mp4',
  38. 'title': 'Force Surprise – LEGO® Star Wars™ Microfighters',
  39. 'description': 'md5:9c673c96ce6f6271b88563fe9dc56de3',
  40. },
  41. 'params': {
  42. 'skip_download': True,
  43. },
  44. }]
  45. _BITRATES = [256, 512, 1024, 1536, 2560]
  46. def _real_extract(self, url):
  47. locale, video_id = re.match(self._VALID_URL, url).groups()
  48. webpage = self._download_webpage(url, video_id)
  49. title = get_element_by_class('video-header', webpage).strip()
  50. progressive_base = 'https://lc-mediaplayerns-live-s.legocdn.com/'
  51. streaming_base = 'http://legoprod-f.akamaihd.net/'
  52. content_url = self._html_search_meta('contentUrl', webpage)
  53. path = self._search_regex(
  54. r'(?:https?:)?//[^/]+/(?:[iz]/s/)?public/(.+)_[0-9,]+\.(?:mp4|webm)',
  55. content_url, 'video path', default=None)
  56. if not path:
  57. player_url = self._proto_relative_url(self._search_regex(
  58. r'<iframe[^>]+src="((?:https?)?//(?:www\.)?lego\.com/[^/]+/mediaplayer/video/[^"]+)',
  59. webpage, 'player url', default=None))
  60. if not player_url:
  61. base_url = self._proto_relative_url(self._search_regex(
  62. r'data-baseurl="([^"]+)"', webpage, 'base url',
  63. default='http://www.lego.com/%s/mediaplayer/video/' % locale))
  64. player_url = base_url + video_id
  65. player_webpage = self._download_webpage(player_url, video_id)
  66. video_data = self._parse_json(unescapeHTML(self._search_regex(
  67. r"video='([^']+)'", player_webpage, 'video data')), video_id)
  68. progressive_base = self._search_regex(
  69. r'data-video-progressive-url="([^"]+)"',
  70. player_webpage, 'progressive base', default='https://lc-mediaplayerns-live-s.legocdn.com/')
  71. streaming_base = self._search_regex(
  72. r'data-video-streaming-url="([^"]+)"',
  73. player_webpage, 'streaming base', default='http://legoprod-f.akamaihd.net/')
  74. item_id = video_data['ItemId']
  75. net_storage_path = video_data.get('NetStoragePath') or '/'.join([item_id[:2], item_id[2:4]])
  76. base_path = '_'.join([item_id, video_data['VideoId'], video_data['Locale'], compat_str(video_data['VideoVersion'])])
  77. path = '/'.join([net_storage_path, base_path])
  78. streaming_path = ','.join(map(lambda bitrate: compat_str(bitrate), self._BITRATES))
  79. formats = self._extract_akamai_formats(
  80. '%si/s/public/%s_,%s,.mp4.csmil/master.m3u8' % (streaming_base, path, streaming_path), video_id)
  81. m3u8_formats = list(filter(
  82. lambda f: f.get('protocol') == 'm3u8_native' and f.get('vcodec') != 'none' and f.get('resolution') != 'multiple',
  83. formats))
  84. if len(m3u8_formats) == len(self._BITRATES):
  85. self._sort_formats(m3u8_formats)
  86. for bitrate, m3u8_format in zip(self._BITRATES, m3u8_formats):
  87. progressive_base_url = '%spublic/%s_%d.' % (progressive_base, path, bitrate)
  88. mp4_f = m3u8_format.copy()
  89. mp4_f.update({
  90. 'url': progressive_base_url + 'mp4',
  91. 'format_id': m3u8_format['format_id'].replace('hls', 'mp4'),
  92. 'protocol': 'http',
  93. })
  94. web_f = {
  95. 'url': progressive_base_url + 'webm',
  96. 'format_id': m3u8_format['format_id'].replace('hls', 'webm'),
  97. 'width': m3u8_format['width'],
  98. 'height': m3u8_format['height'],
  99. 'tbr': m3u8_format.get('tbr'),
  100. 'ext': 'webm',
  101. }
  102. formats.extend([web_f, mp4_f])
  103. else:
  104. for bitrate in self._BITRATES:
  105. for ext in ('web', 'mp4'):
  106. formats.append({
  107. 'format_id': '%s-%s' % (ext, bitrate),
  108. 'url': '%spublic/%s_%d.%s' % (progressive_base, path, bitrate, ext),
  109. 'tbr': bitrate,
  110. 'ext': ext,
  111. })
  112. self._sort_formats(formats)
  113. return {
  114. 'id': video_id,
  115. 'title': title,
  116. 'description': self._html_search_meta('description', webpage),
  117. 'thumbnail': self._html_search_meta('thumbnail', webpage),
  118. 'duration': parse_duration(self._html_search_meta('duration', webpage)),
  119. 'formats': formats,
  120. }