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