lego.py 5.7 KB

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