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.

149 lines
6.0 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. import uuid
  5. from .common import InfoExtractor
  6. from ..compat import compat_HTTPError
  7. from ..utils import (
  8. ExtractorError,
  9. int_or_none,
  10. qualities,
  11. )
  12. class LEGOIE(InfoExtractor):
  13. _VALID_URL = r'https?://(?:www\.)?lego\.com/(?P<locale>[a-z]{2}-[a-z]{2})/(?:[^/]+/)*videos/(?:[^/]+/)*[^/?#]+-(?P<id>[0-9a-f]{32})'
  14. _TESTS = [{
  15. 'url': 'http://www.lego.com/en-us/videos/themes/club/blocumentary-kawaguchi-55492d823b1b4d5e985787fa8c2973b1',
  16. 'md5': 'f34468f176cfd76488767fc162c405fa',
  17. 'info_dict': {
  18. 'id': '55492d82-3b1b-4d5e-9857-87fa8c2973b1_en-US',
  19. 'ext': 'mp4',
  20. 'title': 'Blocumentary Great Creations: Akiyuki Kawaguchi',
  21. 'description': 'Blocumentary Great Creations: Akiyuki Kawaguchi',
  22. },
  23. }, {
  24. # geo-restricted but the contentUrl contain a valid url
  25. 'url': 'http://www.lego.com/nl-nl/videos/themes/nexoknights/episode-20-kingdom-of-heroes-13bdc2299ab24d9685701a915b3d71e7##sp=399',
  26. 'md5': 'c7420221f7ffd03ff056f9db7f8d807c',
  27. 'info_dict': {
  28. 'id': '13bdc229-9ab2-4d96-8570-1a915b3d71e7_nl-NL',
  29. 'ext': 'mp4',
  30. 'title': 'Aflevering 20: Helden van het koninkrijk',
  31. 'description': 'md5:8ee499aac26d7fa8bcb0cedb7f9c3941',
  32. 'age_limit': 5,
  33. },
  34. }, {
  35. # with subtitle
  36. 'url': 'https://www.lego.com/nl-nl/kids/videos/classic/creative-storytelling-the-little-puppy-aa24f27c7d5242bc86102ebdc0f24cba',
  37. 'info_dict': {
  38. 'id': 'aa24f27c-7d52-42bc-8610-2ebdc0f24cba_nl-NL',
  39. 'ext': 'mp4',
  40. 'title': 'De kleine puppy',
  41. 'description': 'md5:5b725471f849348ac73f2e12cfb4be06',
  42. 'age_limit': 1,
  43. 'subtitles': {
  44. 'nl': [{
  45. 'ext': 'srt',
  46. 'url': r're:^https://.+\.srt$',
  47. }],
  48. },
  49. },
  50. 'params': {
  51. 'skip_download': True,
  52. },
  53. }]
  54. _QUALITIES = {
  55. 'Lowest': (64, 180, 320),
  56. 'Low': (64, 270, 480),
  57. 'Medium': (96, 360, 640),
  58. 'High': (128, 540, 960),
  59. 'Highest': (128, 720, 1280),
  60. }
  61. def _real_extract(self, url):
  62. locale, video_id = re.match(self._VALID_URL, url).groups()
  63. countries = [locale.split('-')[1].upper()]
  64. self._initialize_geo_bypass({
  65. 'countries': countries,
  66. })
  67. try:
  68. item = self._download_json(
  69. # https://contentfeed.services.lego.com/api/v2/item/[VIDEO_ID]?culture=[LOCALE]&contentType=Video
  70. 'https://services.slingshot.lego.com/mediaplayer/v2',
  71. video_id, query={
  72. 'videoId': '%s_%s' % (uuid.UUID(video_id), locale),
  73. }, headers=self.geo_verification_headers())
  74. except ExtractorError as e:
  75. if isinstance(e.cause, compat_HTTPError) and e.cause.code == 451:
  76. self.raise_geo_restricted(countries=countries)
  77. raise
  78. video = item['Video']
  79. video_id = video['Id']
  80. title = video['Title']
  81. q = qualities(['Lowest', 'Low', 'Medium', 'High', 'Highest'])
  82. formats = []
  83. for video_source in item.get('VideoFormats', []):
  84. video_source_url = video_source.get('Url')
  85. if not video_source_url:
  86. continue
  87. video_source_format = video_source.get('Format')
  88. if video_source_format == 'F4M':
  89. formats.extend(self._extract_f4m_formats(
  90. video_source_url, video_id,
  91. f4m_id=video_source_format, fatal=False))
  92. elif video_source_format == 'M3U8':
  93. formats.extend(self._extract_m3u8_formats(
  94. video_source_url, video_id, 'mp4', 'm3u8_native',
  95. m3u8_id=video_source_format, fatal=False))
  96. else:
  97. video_source_quality = video_source.get('Quality')
  98. format_id = []
  99. for v in (video_source_format, video_source_quality):
  100. if v:
  101. format_id.append(v)
  102. f = {
  103. 'format_id': '-'.join(format_id),
  104. 'quality': q(video_source_quality),
  105. 'url': video_source_url,
  106. }
  107. quality = self._QUALITIES.get(video_source_quality)
  108. if quality:
  109. f.update({
  110. 'abr': quality[0],
  111. 'height': quality[1],
  112. 'width': quality[2],
  113. }),
  114. formats.append(f)
  115. self._sort_formats(formats)
  116. subtitles = {}
  117. sub_file_id = video.get('SubFileId')
  118. if sub_file_id and sub_file_id != '00000000-0000-0000-0000-000000000000':
  119. net_storage_path = video.get('NetstoragePath')
  120. invariant_id = video.get('InvariantId')
  121. video_file_id = video.get('VideoFileId')
  122. video_version = video.get('VideoVersion')
  123. if net_storage_path and invariant_id and video_file_id and video_version:
  124. subtitles.setdefault(locale[:2], []).append({
  125. 'url': 'https://lc-mediaplayerns-live-s.legocdn.com/public/%s/%s_%s_%s_%s_sub.srt' % (net_storage_path, invariant_id, video_file_id, locale, video_version),
  126. })
  127. return {
  128. 'id': video_id,
  129. 'title': title,
  130. 'description': video.get('Description'),
  131. 'thumbnail': video.get('GeneratedCoverImage') or video.get('GeneratedThumbnail'),
  132. 'duration': int_or_none(video.get('Length')),
  133. 'formats': formats,
  134. 'subtitles': subtitles,
  135. 'age_limit': int_or_none(video.get('AgeFrom')),
  136. 'season': video.get('SeasonTitle'),
  137. 'season_number': int_or_none(video.get('Season')) or None,
  138. 'episode_number': int_or_none(video.get('Episode')) or None,
  139. }