[svt] Fix extraction for SVTPlay (closes #9809)
[youtube-dl] / youtube_dl / extractor / svt.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8     determine_ext,
9     dict_get,
10 )
11
12
13 class SVTBaseIE(InfoExtractor):
14     def _extract_video(self, info, video_id):
15         video_info = self._get_video_info(info)
16
17         formats = []
18         for vr in video_info['videoReferences']:
19             player_type = vr.get('playerType')
20             vurl = vr['url']
21             ext = determine_ext(vurl)
22             if ext == 'm3u8':
23                 formats.extend(self._extract_m3u8_formats(
24                     vurl, video_id,
25                     ext='mp4', entry_protocol='m3u8_native',
26                     m3u8_id=player_type, fatal=False))
27             elif ext == 'f4m':
28                 formats.extend(self._extract_f4m_formats(
29                     vurl + '?hdcore=3.3.0', video_id,
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))
35             else:
36                 formats.append({
37                     'format_id': player_type,
38                     'url': vurl,
39                 })
40         self._sort_formats(formats)
41
42         subtitles = {}
43         subtitle_references = dict_get(video_info, ('subtitles', 'subtitleReferences'))
44         if isinstance(subtitle_references, list):
45             for sr in subtitle_references:
46                 subtitle_url = sr.get('url')
47                 subtitle_lang = sr.get('language', 'sv')
48                 if subtitle_url:
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})
54
55         duration = video_info.get('materialLength')
56         age_limit = 18 if video_info.get('inappropriateForChildren') else 0
57
58         return {
59             'id': video_id,
60             'formats': formats,
61             'subtitles': subtitles,
62             'duration': duration,
63             'age_limit': age_limit,
64         }
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&sectionId=541&articleId=2900353&type=embed&contextSectionId=123&autostart=false',
71         'md5': '33e9a5d8f646523ce0868ecfb0eed77d',
72         'info_dict': {
73             'id': '2900353',
74             'ext': 'mp4',
75             'title': 'Stjärnorna skojar till det - under SVT-intervjun',
76             'duration': 27,
77             'age_limit': 0,
78         },
79     }
80
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
88     def _get_video_info(self, info):
89         return info['video']
90
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')
95
96         info = self._download_json(
97             'http://www.svt.se/wd?widgetId=%s&articleId=%s&format=json&type=embed&output=json' % (widget_id, article_id),
98             article_id)
99
100         info_dict = self._extract_video(info, article_id)
101         info_dict['title'] = info['context']['title']
102         return info_dict
103
104
105 class SVTPlayIE(SVTBaseIE):
106     IE_DESC = 'SVT Play and Öppet arkiv'
107     _VALID_URL = r'https?://(?:www\.)?(?:svtplay|oppetarkiv)\.se/video/(?P<id>[0-9]+)'
108     _TEST = {
109         'url': 'http://www.svtplay.se/video/5996901/flygplan-till-haile-selassie/flygplan-till-haile-selassie-2',
110         'md5': '2b6704fe4a28801e1a098bbf3c5ac611',
111         'info_dict': {
112             'id': '5996901',
113             'ext': 'mp4',
114             'title': 'Flygplan till Haile Selassie',
115             'duration': 3527,
116             'thumbnail': 're:^https?://.*[\.-]jpg$',
117             'age_limit': 0,
118             'subtitles': {
119                 'sv': [{
120                     'ext': 'wsrt',
121                 }]
122             },
123         },
124     }
125
126     def _get_video_info(self, info):
127         return info['context']['dispatcher']['stores']['VideoTitlePageStore']['data']['video']
128
129     def _real_extract(self, url):
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