Merge remote-tracking branch 'duncankl/airmozilla'
[youtube-dl] / youtube_dl / extractor / drtv.py
1 from __future__ import unicode_literals
2
3 from .common import InfoExtractor, ExtractorError
4 from ..utils import parse_iso8601
5
6
7 class DRTVIE(InfoExtractor):
8     _VALID_URL = r'https?://(?:www\.)?dr\.dk/tv/se/(?:[^/]+/)*(?P<id>[\da-z-]+)(?:[/#?]|$)'
9
10     _TEST = {
11         'url': 'http://www.dr.dk/tv/se/partiets-mand/partiets-mand-7-8',
12         'md5': '4a7e1dd65cdb2643500a3f753c942f25',
13         'info_dict': {
14             'id': 'partiets-mand-7-8',
15             'ext': 'mp4',
16             'title': 'Partiets mand (7:8)',
17             'description': 'md5:a684b90a8f9336cd4aab94b7647d7862',
18             'timestamp': 1403047940,
19             'upload_date': '20140617',
20             'duration': 1299.040,
21         },
22     }
23
24     def _real_extract(self, url):
25         video_id = self._match_id(url)
26
27         webpage = self._download_webpage(url, video_id)
28
29         video_id = self._search_regex(
30             r'data-(?:material-identifier|episode-slug)="([^"]+)"',
31             webpage, 'video id')
32
33         programcard = self._download_json(
34             'http://www.dr.dk/mu/programcard/expanded/%s' % video_id,
35             video_id, 'Downloading video JSON')
36         data = programcard['Data'][0]
37
38         title = data['Title']
39         description = data['Description']
40         timestamp = parse_iso8601(data['CreatedTime'])
41
42         thumbnail = None
43         duration = None
44
45         restricted_to_denmark = False
46
47         formats = []
48         subtitles = {}
49
50         for asset in data['Assets']:
51             if asset['Kind'] == 'Image':
52                 thumbnail = asset['Uri']
53             elif asset['Kind'] == 'VideoResource':
54                 duration = asset['DurationInMilliseconds'] / 1000.0
55                 restricted_to_denmark = asset['RestrictedToDenmark']
56                 spoken_subtitles = asset['Target'] == 'SpokenSubtitles'
57                 for link in asset['Links']:
58                     target = link['Target']
59                     uri = link['Uri']
60                     format_id = target
61                     preference = -1 if target == 'HDS' else -2
62                     if spoken_subtitles:
63                         preference -= 2
64                         format_id += '-spoken-subtitles'
65                     formats.append({
66                         'url': uri + '?hdcore=3.3.0&plugin=aasp-3.3.0.99.43' if target == 'HDS' else uri,
67                         'format_id': format_id,
68                         'ext': link['FileFormat'],
69                         'preference': preference,
70                     })
71                 subtitles_list = asset.get('SubtitlesList')
72                 if isinstance(subtitles_list, list):
73                     LANGS = {
74                         'Danish': 'dk',
75                     }
76                     for subs in subtitles_list:
77                         lang = subs['Language']
78                         subtitles[LANGS.get(lang, lang)] = [{'url': subs['Uri'], 'ext': 'vtt'}]
79
80         if not formats and restricted_to_denmark:
81             raise ExtractorError(
82                 'Unfortunately, DR is not allowed to show this program outside Denmark.', expected=True)
83
84         self._sort_formats(formats)
85
86         return {
87             'id': video_id,
88             'title': title,
89             'description': description,
90             'thumbnail': thumbnail,
91             'timestamp': timestamp,
92             'duration': duration,
93             'formats': formats,
94             'subtitles': subtitles,
95         }