[SVTPlay] Add subtitle support
[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 )
10
11
12 class SVTBaseIE(InfoExtractor):
13     def _extract_video(self, url, video_id):
14         info = self._download_json(url, video_id)
15
16         title = info['context']['title']
17         thumbnail = info['context'].get('thumbnailImage')
18
19         video_info = info['video']
20         formats = []
21         for vr in video_info['videoReferences']:
22             vurl = vr['url']
23             ext = determine_ext(vurl)
24             if ext == 'm3u8':
25                 formats.extend(self._extract_m3u8_formats(
26                     vurl, video_id,
27                     ext='mp4', entry_protocol='m3u8_native',
28                     m3u8_id=vr.get('playerType')))
29             elif ext == 'f4m':
30                 formats.extend(self._extract_f4m_formats(
31                     vurl + '?hdcore=3.3.0', video_id,
32                     f4m_id=vr.get('playerType')))
33             else:
34                 formats.append({
35                     'format_id': vr.get('playerType'),
36                     'url': vurl,
37                 })
38         self._sort_formats(formats)
39
40         # SVT does not tell us the language, so we assume swedish.
41         subtitles = {}
42         for sr in video_info['subtitleReferences']:
43             if 'url' in sr:
44                 subtitles.setdefault('sv', []).append({'url': sr['url']})
45
46         duration = video_info.get('materialLength')
47         age_limit = 18 if video_info.get('inappropriateForChildren') else 0
48
49         return {
50             'id': video_id,
51             'title': title,
52             'formats': formats,
53             'subtitles': subtitles,
54             'thumbnail': thumbnail,
55             'duration': duration,
56             'age_limit': age_limit,
57         }
58
59
60 class SVTIE(SVTBaseIE):
61     _VALID_URL = r'https?://(?:www\.)?svt\.se/wd\?(?:.*?&)?widgetId=(?P<widget_id>\d+)&.*?\barticleId=(?P<id>\d+)'
62     _TEST = {
63         'url': 'http://www.svt.se/wd?widgetId=23991&sectionId=541&articleId=2900353&type=embed&contextSectionId=123&autostart=false',
64         'md5': '9648197555fc1b49e3dc22db4af51d46',
65         'info_dict': {
66             'id': '2900353',
67             'ext': 'flv',
68             'title': 'Här trycker Jagr till Giroux (under SVT-intervjun)',
69             'duration': 27,
70             'age_limit': 0,
71         },
72     }
73
74     @staticmethod
75     def _extract_url(webpage):
76         mobj = re.search(
77             r'(?:<iframe src|href)="(?P<url>%s[^"]*)"' % SVTIE._VALID_URL, webpage)
78         if mobj:
79             return mobj.group('url')
80
81     def _real_extract(self, url):
82         mobj = re.match(self._VALID_URL, url)
83         widget_id = mobj.group('widget_id')
84         article_id = mobj.group('id')
85         return self._extract_video(
86             'http://www.svt.se/wd?widgetId=%s&articleId=%s&format=json&type=embed&output=json' % (widget_id, article_id),
87             article_id)
88
89
90 class SVTPlayIE(SVTBaseIE):
91     IE_DESC = 'SVT Play and Öppet arkiv'
92     _VALID_URL = r'https?://(?:www\.)?(?P<host>svtplay|oppetarkiv)\.se/video/(?P<id>[0-9]+)'
93     _TESTS = [{
94         'url': 'http://www.svtplay.se/video/2609989/sm-veckan/sm-veckan-rally-final-sasong-1-sm-veckan-rally-final',
95         'md5': 'ade3def0643fa1c40587a422f98edfd9',
96         'info_dict': {
97             'id': '2609989',
98             'ext': 'flv',
99             'title': 'SM veckan vinter, Örebro - Rally, final',
100             'duration': 4500,
101             'thumbnail': 're:^https?://.*[\.-]jpg$',
102             'age_limit': 0,
103         },
104     }, {
105         'url': 'http://www.oppetarkiv.se/video/1058509/rederiet-sasong-1-avsnitt-1-av-318',
106         'md5': 'c3101a17ce9634f4c1f9800f0746c187',
107         'info_dict': {
108             'id': '1058509',
109             'ext': 'flv',
110             'title': 'Farlig kryssning',
111             'duration': 2566,
112             'thumbnail': 're:^https?://.*[\.-]jpg$',
113             'age_limit': 0,
114         },
115         'skip': 'Only works from Sweden',
116     }]
117
118     def _real_extract(self, url):
119         mobj = re.match(self._VALID_URL, url)
120         video_id = mobj.group('id')
121         host = mobj.group('host')
122         return self._extract_video(
123             'http://www.%s.se/video/%s?output=json' % (host, video_id),
124             video_id)