]> git.bitcoin.ninja Git - youtube-dl/blob - youtube_dl/extractor/svt.py
Merge pull request #9041 from kasper93/master
[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             player_type = vr.get('playerType')
23             vurl = vr['url']
24             ext = determine_ext(vurl)
25             if ext == 'm3u8':
26                 formats.extend(self._extract_m3u8_formats(
27                     vurl, video_id,
28                     ext='mp4', entry_protocol='m3u8_native',
29                     m3u8_id=player_type, fatal=False))
30             elif ext == 'f4m':
31                 formats.extend(self._extract_f4m_formats(
32                     vurl + '?hdcore=3.3.0', video_id,
33                     f4m_id=player_type, fatal=False))
34             elif ext == 'mpd':
35                 if player_type == 'dashhbbtv':
36                     formats.extend(self._extract_mpd_formats(
37                         vurl, video_id, mpd_id=player_type, fatal=False))
38             else:
39                 formats.append({
40                     'format_id': player_type,
41                     'url': vurl,
42                 })
43         self._sort_formats(formats)
44
45         subtitles = {}
46         subtitle_references = video_info.get('subtitleReferences')
47         if isinstance(subtitle_references, list):
48             for sr in subtitle_references:
49                 subtitle_url = sr.get('url')
50                 if subtitle_url:
51                     subtitles.setdefault('sv', []).append({'url': subtitle_url})
52
53         duration = video_info.get('materialLength')
54         age_limit = 18 if video_info.get('inappropriateForChildren') else 0
55
56         return {
57             'id': video_id,
58             'title': title,
59             'formats': formats,
60             'subtitles': subtitles,
61             'thumbnail': thumbnail,
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': '9648197555fc1b49e3dc22db4af51d46',
72         'info_dict': {
73             'id': '2900353',
74             'ext': 'flv',
75             'title': 'Här trycker Jagr till Giroux (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 _real_extract(self, url):
89         mobj = re.match(self._VALID_URL, url)
90         widget_id = mobj.group('widget_id')
91         article_id = mobj.group('id')
92         return self._extract_video(
93             'http://www.svt.se/wd?widgetId=%s&articleId=%s&format=json&type=embed&output=json' % (widget_id, article_id),
94             article_id)
95
96
97 class SVTPlayIE(SVTBaseIE):
98     IE_DESC = 'SVT Play and Öppet arkiv'
99     _VALID_URL = r'https?://(?:www\.)?(?P<host>svtplay|oppetarkiv)\.se/video/(?P<id>[0-9]+)'
100     _TEST = {
101         'url': 'http://www.svtplay.se/video/5996901/flygplan-till-haile-selassie/flygplan-till-haile-selassie-2',
102         'md5': '2b6704fe4a28801e1a098bbf3c5ac611',
103         'info_dict': {
104             'id': '5996901',
105             'ext': 'mp4',
106             'title': 'Flygplan till Haile Selassie',
107             'duration': 3527,
108             'thumbnail': 're:^https?://.*[\.-]jpg$',
109             'age_limit': 0,
110             'subtitles': {
111                 'sv': [{
112                     'ext': 'wsrt',
113                 }]
114             },
115         },
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)