]>
Commit | Line | Data |
---|---|---|
28f12728 | 1 | # coding: utf-8 |
1309b396 PH |
2 | from __future__ import unicode_literals |
3 | ||
df5ae3eb S |
4 | import re |
5 | ||
1309b396 PH |
6 | from .common import InfoExtractor |
7 | from ..utils import ( | |
8 | determine_ext, | |
e4f90ea0 | 9 | dict_get, |
1309b396 PH |
10 | ) |
11 | ||
12 | ||
79998cd5 | 13 | class SVTBaseIE(InfoExtractor): |
e4f90ea0 YCH |
14 | def _extract_video(self, info, video_id): |
15 | video_info = self._get_video_info(info) | |
1309b396 | 16 | |
1309b396 PH |
17 | formats = [] |
18 | for vr in video_info['videoReferences']: | |
edfd9351 | 19 | player_type = vr.get('playerType') |
1309b396 | 20 | vurl = vr['url'] |
df5ae3eb S |
21 | ext = determine_ext(vurl) |
22 | if ext == 'm3u8': | |
1309b396 PH |
23 | formats.extend(self._extract_m3u8_formats( |
24 | vurl, video_id, | |
25 | ext='mp4', entry_protocol='m3u8_native', | |
edfd9351 | 26 | m3u8_id=player_type, fatal=False)) |
df5ae3eb S |
27 | elif ext == 'f4m': |
28 | formats.extend(self._extract_f4m_formats( | |
29 | vurl + '?hdcore=3.3.0', video_id, | |
edfd9351 | 30 | f4m_id=player_type, fatal=False)) |
31 | elif ext == 'mpd': | |
32 | if player_type == 'dashhbbtv': | |
33 | formats.extend(self._extract_mpd_formats( | |
34 | vurl, video_id, mpd_id=player_type, fatal=False)) | |
1309b396 PH |
35 | else: |
36 | formats.append({ | |
edfd9351 | 37 | 'format_id': player_type, |
1309b396 PH |
38 | 'url': vurl, |
39 | }) | |
40 | self._sort_formats(formats) | |
41 | ||
1f16b958 | 42 | subtitles = {} |
e4f90ea0 | 43 | subtitle_references = dict_get(video_info, ('subtitles', 'subtitleReferences')) |
594c4d79 S |
44 | if isinstance(subtitle_references, list): |
45 | for sr in subtitle_references: | |
46 | subtitle_url = sr.get('url') | |
e4f90ea0 | 47 | subtitle_lang = sr.get('language', 'sv') |
594c4d79 | 48 | if subtitle_url: |
e4f90ea0 YCH |
49 | if determine_ext(subtitle_url) == 'm3u8': |
50 | # TODO(yan12125): handle WebVTT in m3u8 manifests | |
51 | continue | |
52 | ||
53 | subtitles.setdefault(subtitle_lang, []).append({'url': subtitle_url}) | |
1f16b958 | 54 | |
1309b396 | 55 | duration = video_info.get('materialLength') |
df5ae3eb | 56 | age_limit = 18 if video_info.get('inappropriateForChildren') else 0 |
1309b396 PH |
57 | |
58 | return { | |
59 | 'id': video_id, | |
1309b396 | 60 | 'formats': formats, |
1f16b958 | 61 | 'subtitles': subtitles, |
1309b396 | 62 | 'duration': duration, |
df5ae3eb | 63 | 'age_limit': age_limit, |
1309b396 | 64 | } |
79998cd5 S |
65 | |
66 | ||
67 | class SVTIE(SVTBaseIE): | |
68 | _VALID_URL = r'https?://(?:www\.)?svt\.se/wd\?(?:.*?&)?widgetId=(?P<widget_id>\d+)&.*?\barticleId=(?P<id>\d+)' | |
69 | _TEST = { | |
70 | 'url': 'http://www.svt.se/wd?widgetId=23991§ionId=541&articleId=2900353&type=embed&contextSectionId=123&autostart=false', | |
e4f90ea0 | 71 | 'md5': '33e9a5d8f646523ce0868ecfb0eed77d', |
79998cd5 S |
72 | 'info_dict': { |
73 | 'id': '2900353', | |
e4f90ea0 YCH |
74 | 'ext': 'mp4', |
75 | 'title': 'Stjärnorna skojar till det - under SVT-intervjun', | |
79998cd5 S |
76 | 'duration': 27, |
77 | 'age_limit': 0, | |
78 | }, | |
79 | } | |
80 | ||
bab19a8e S |
81 | @staticmethod |
82 | def _extract_url(webpage): | |
83 | mobj = re.search( | |
84 | r'(?:<iframe src|href)="(?P<url>%s[^"]*)"' % SVTIE._VALID_URL, webpage) | |
85 | if mobj: | |
86 | return mobj.group('url') | |
87 | ||
e4f90ea0 YCH |
88 | def _get_video_info(self, info): |
89 | return info['video'] | |
90 | ||
79998cd5 S |
91 | def _real_extract(self, url): |
92 | mobj = re.match(self._VALID_URL, url) | |
93 | widget_id = mobj.group('widget_id') | |
94 | article_id = mobj.group('id') | |
e4f90ea0 YCH |
95 | |
96 | info = self._download_json( | |
79998cd5 S |
97 | 'http://www.svt.se/wd?widgetId=%s&articleId=%s&format=json&type=embed&output=json' % (widget_id, article_id), |
98 | article_id) | |
99 | ||
e4f90ea0 YCH |
100 | info_dict = self._extract_video(info, article_id) |
101 | info_dict['title'] = info['context']['title'] | |
102 | return info_dict | |
103 | ||
79998cd5 S |
104 | |
105 | class SVTPlayIE(SVTBaseIE): | |
106 | IE_DESC = 'SVT Play and Öppet arkiv' | |
e4f90ea0 | 107 | _VALID_URL = r'https?://(?:www\.)?(?:svtplay|oppetarkiv)\.se/video/(?P<id>[0-9]+)' |
594c4d79 S |
108 | _TEST = { |
109 | 'url': 'http://www.svtplay.se/video/5996901/flygplan-till-haile-selassie/flygplan-till-haile-selassie-2', | |
110 | 'md5': '2b6704fe4a28801e1a098bbf3c5ac611', | |
79998cd5 | 111 | 'info_dict': { |
594c4d79 S |
112 | 'id': '5996901', |
113 | 'ext': 'mp4', | |
114 | 'title': 'Flygplan till Haile Selassie', | |
115 | 'duration': 3527, | |
79998cd5 S |
116 | 'thumbnail': 're:^https?://.*[\.-]jpg$', |
117 | 'age_limit': 0, | |
594c4d79 S |
118 | 'subtitles': { |
119 | 'sv': [{ | |
120 | 'ext': 'wsrt', | |
121 | }] | |
122 | }, | |
79998cd5 | 123 | }, |
594c4d79 | 124 | } |
79998cd5 | 125 | |
e4f90ea0 YCH |
126 | def _get_video_info(self, info): |
127 | return info['context']['dispatcher']['stores']['VideoTitlePageStore']['data']['video'] | |
128 | ||
79998cd5 | 129 | def _real_extract(self, url): |
e4f90ea0 YCH |
130 | video_id = self._match_id(url) |
131 | ||
132 | webpage = self._download_webpage(url, video_id) | |
133 | ||
134 | data = self._parse_json(self._search_regex( | |
135 | r'root\["__svtplay"\]\s*=\s*([^;]+);', webpage, 'embedded data'), video_id) | |
136 | ||
137 | thumbnail = self._og_search_thumbnail(webpage) | |
138 | ||
139 | info_dict = self._extract_video(data, video_id) | |
140 | info_dict.update({ | |
141 | 'title': data['context']['dispatcher']['stores']['MetaStore']['title'], | |
142 | 'thumbnail': thumbnail, | |
143 | }) | |
144 | ||
145 | return info_dict |