[dvtv] Parse adaptive formats as well
[youtube-dl] / youtube_dl / extractor / dvtv.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     js_to_json,
9     unescapeHTML,
10     ExtractorError,
11 )
12
13
14 class DVTVIE(InfoExtractor):
15     IE_NAME = 'dvtv'
16     IE_DESC = 'http://video.aktualne.cz/'
17
18     _VALID_URL = r'https?://video\.aktualne\.cz/(?:[^/]+/)+r~(?P<id>[0-9a-f]{32})'
19
20     _TESTS = [{
21         'url': 'http://video.aktualne.cz/dvtv/vondra-o-ceskem-stoleti-pri-pohledu-na-havla-mi-bylo-trapne/r~e5efe9ca855511e4833a0025900fea04/',
22         'md5': '67cb83e4a955d36e1b5d31993134a0c2',
23         'info_dict': {
24             'id': 'dc0768de855511e49e4b0025900fea04',
25             'ext': 'mp4',
26             'title': 'Vondra o Českém století: Při pohledu na Havla mi bylo trapně',
27         }
28     }, {
29         'url': 'http://video.aktualne.cz/dvtv/stropnicky-policie-vrbetice-preventivne-nekontrolovala/r~82ed4322849211e4a10c0025900fea04/',
30         'md5': '6388f1941b48537dbd28791f712af8bf',
31         'info_dict': {
32             'id': '72c02230849211e49f60002590604f2e',
33             'ext': 'mp4',
34             'title': 'Stropnický: Policie Vrbětice preventivně nekontrolovala',
35         }
36     }, {
37         'url': 'http://video.aktualne.cz/dvtv/dvtv-16-12-2014-utok-talibanu-boj-o-kliniku-uprchlici/r~973eb3bc854e11e498be002590604f2e/',
38         'info_dict': {
39             'title': 'DVTV 16. 12. 2014: útok Talibanu, boj o kliniku, uprchlíci',
40             'id': '973eb3bc854e11e498be002590604f2e',
41         },
42         'playlist': [{
43             'md5': 'da7ca6be4935532241fa9520b3ad91e4',
44             'info_dict': {
45                 'id': 'b0b40906854d11e4bdad0025900fea04',
46                 'ext': 'mp4',
47                 'title': 'Drtinová Veselovský TV 16. 12. 2014: Témata dne'
48             }
49         }, {
50             'md5': '5f7652a08b05009c1292317b449ffea2',
51             'info_dict': {
52                 'id': '420ad9ec854a11e4bdad0025900fea04',
53                 'ext': 'mp4',
54                 'title': 'Školní masakr možná změní boj s Talibanem, říká novinářka'
55             }
56         }, {
57             'md5': '498eb9dfa97169f409126c617e2a3d64',
58             'info_dict': {
59                 'id': '95d35580846a11e4b6d20025900fea04',
60                 'ext': 'mp4',
61                 'title': 'Boj o kliniku: Veřejný zájem, nebo právo na majetek?'
62             }
63         }, {
64             'md5': 'b8dc6b744844032dab6ba3781a7274b9',
65             'info_dict': {
66                 'id': '6fe14d66853511e4833a0025900fea04',
67                 'ext': 'mp4',
68                 'title': 'Pánek: Odmítání syrských uprchlíků je ostudou české vlády'
69             }
70         }],
71     }, {
72         'url': 'http://video.aktualne.cz/v-cechach-poprve-zazni-zelenkova-zrestaurovana-mse/r~45b4b00483ec11e4883b002590604f2e/',
73         'only_matching': True,
74     }, {
75         'url': 'https://video.aktualne.cz/dvtv/zeman-si-jen-leci-mindraky-sobotku-nenavidi-a-babis-se-mu-te/r~960cdb3a365a11e7a83b0025900fea04/',
76         'md5': 'f8efe9656017da948369aa099788c8ea',
77         'info_dict': {
78             'id': '3c496fec365911e7a6500025900fea04',
79             'ext': 'm3u8',
80             'title': 'Zeman si jen léčí mindráky, Sobotku nenávidí a Babiš se mu teď hodí, tvrdí Kmenta',
81         }
82     }]
83
84     def _parse_video_metadata(self, js, video_id):
85         metadata = self._parse_json(js, video_id, transform_source=js_to_json)
86
87         formats = []
88         for video in metadata['sources']:
89             ext = video['type'][6:]
90             if video['label'] != 'adaptive':
91                 formats.append({
92                     'url': video['file'],
93                     'ext': ext,
94                     'format_id': '%s-%s' % (ext, video['label']),
95                     'height': int(video['label'].rstrip('p')),
96                     'fps': 25,
97                 })
98             elif video['type'] == 'application/dash+xml':
99                 formats.extend(self._extract_mpd_formats(video['file'],
100                                                          video_id,
101                                                          fatal=False))
102             elif video['type'] == 'application/vnd.apple.mpegurl':
103                 formats.extend(self._extract_m3u8_formats(video['file'],
104                                                           video_id,
105                                                           fatal=False))
106
107         self._sort_formats(formats)
108
109         return {
110             'id': metadata['mediaid'],
111             'title': unescapeHTML(metadata['title']),
112             'thumbnail': self._proto_relative_url(metadata['image'], 'http:'),
113             'formats': formats
114         }
115
116     def _real_extract(self, url):
117         video_id = self._match_id(url)
118
119         webpage = self._download_webpage(url, video_id)
120
121         # single video
122         item = self._search_regex(
123             r"(?s)embedData[0-9a-f]{32}\['asset'\]\s*=\s*(\{.+?\});",
124             webpage, 'video', default=None, fatal=False)
125
126         if item:
127             return self._parse_video_metadata(item, video_id)
128
129         # playlist
130         items = re.findall(
131             r"(?s)BBX\.context\.assets\['[0-9a-f]{32}'\]\.push\(({.+?})\);",
132             webpage)
133
134         if items:
135             return {
136                 '_type': 'playlist',
137                 'id': video_id,
138                 'title': self._og_search_title(webpage),
139                 'entries': [self._parse_video_metadata(i, video_id) for i in items]
140             }
141
142         raise ExtractorError('Could not find neither video nor playlist')