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