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