lego.py 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. from .common import InfoExtractor
  4. from ..compat import compat_str
  5. from ..utils import (
  6. unescapeHTML,
  7. int_or_none,
  8. )
  9. class LEGOIE(InfoExtractor):
  10. _VALID_URL = r'https?://(?:www\.)?lego\.com/(?:[^/]+/)*videos/(?:[^/]+/)*[^/?#]+-(?P<id>[0-9a-f]+)'
  11. _TEST = {
  12. 'url': 'http://www.lego.com/en-us/videos/themes/club/blocumentary-kawaguchi-55492d823b1b4d5e985787fa8c2973b1',
  13. 'md5': 'f34468f176cfd76488767fc162c405fa',
  14. 'info_dict': {
  15. 'id': '55492d823b1b4d5e985787fa8c2973b1',
  16. 'ext': 'mp4',
  17. 'title': 'Blocumentary Great Creations: Akiyuki Kawaguchi',
  18. }
  19. }
  20. _BITRATES = [256, 512, 1024, 1536, 2560]
  21. def _real_extract(self, url):
  22. video_id = self._match_id(url)
  23. webpage = self._download_webpage(
  24. 'http://www.lego.com/en-US/mediaplayer/video/' + video_id, video_id)
  25. title = self._search_regex(r'<title>(.+?)</title>', webpage, 'title')
  26. video_data = self._parse_json(unescapeHTML(self._search_regex(
  27. r"video='([^']+)'", webpage, 'video data')), video_id)
  28. progressive_base = self._search_regex(
  29. r'data-video-progressive-url="([^"]+)"',
  30. webpage, 'progressive base', default='https://lc-mediaplayerns-live-s.legocdn.com/')
  31. streaming_base = self._search_regex(
  32. r'data-video-streaming-url="([^"]+)"',
  33. webpage, 'streaming base', default='http://legoprod-f.akamaihd.net/')
  34. item_id = video_data['ItemId']
  35. net_storage_path = video_data.get('NetStoragePath') or '/'.join([item_id[:2], item_id[2:4]])
  36. base_path = '_'.join([item_id, video_data['VideoId'], video_data['Locale'], compat_str(video_data['VideoVersion'])])
  37. path = '/'.join([net_storage_path, base_path])
  38. streaming_path = ','.join(map(lambda bitrate: compat_str(bitrate), self._BITRATES))
  39. formats = self._extract_akamai_formats(
  40. '%si/s/public/%s_,%s,.mp4.csmil/master.m3u8' % (streaming_base, path, streaming_path), video_id)
  41. m3u8_formats = list(filter(
  42. lambda f: f.get('protocol') == 'm3u8_native' and f.get('vcodec') != 'none' and f.get('resolution') != 'multiple',
  43. formats))
  44. if len(m3u8_formats) == len(self._BITRATES):
  45. self._sort_formats(m3u8_formats)
  46. for bitrate, m3u8_format in zip(self._BITRATES, m3u8_formats):
  47. progressive_base_url = '%spublic/%s_%d.' % (progressive_base, path, bitrate)
  48. mp4_f = m3u8_format.copy()
  49. mp4_f.update({
  50. 'url': progressive_base_url + 'mp4',
  51. 'format_id': m3u8_format['format_id'].replace('hls', 'mp4'),
  52. 'protocol': 'http',
  53. })
  54. web_f = {
  55. 'url': progressive_base_url + 'webm',
  56. 'format_id': m3u8_format['format_id'].replace('hls', 'webm'),
  57. 'width': m3u8_format['width'],
  58. 'height': m3u8_format['height'],
  59. 'tbr': m3u8_format.get('tbr'),
  60. 'ext': 'webm',
  61. }
  62. formats.extend([web_f, mp4_f])
  63. else:
  64. for bitrate in self._BITRATES:
  65. for ext in ('web', 'mp4'):
  66. formats.append({
  67. 'format_id': '%s-%s' % (ext, bitrate),
  68. 'url': '%spublic/%s_%d.%s' % (progressive_base, path, bitrate, ext),
  69. 'tbr': bitrate,
  70. 'ext': ext,
  71. })
  72. self._sort_formats(formats)
  73. return {
  74. 'id': video_id,
  75. 'title': title,
  76. 'thumbnail': video_data.get('CoverImageUrl'),
  77. 'duration': int_or_none(video_data.get('Length')),
  78. 'formats': formats,
  79. }