[tagesschau] add support for more video types
[youtube-dl] / youtube_dl / extractor / tagesschau.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 parse_filesize, ExtractorError
8
9
10 class TagesschauIE(InfoExtractor):
11     _VALID_URL = r'https?://(?:www\.)?tagesschau\.de/multimedia/(?:sendung/(ts|tsg|tt|nm)|video/video|tsvorzwanzig)(?P<id>-?[0-9]+)\.html'
12
13     _TESTS = [{
14         'url': 'http://www.tagesschau.de/multimedia/video/video1399128.html',
15         'md5': 'bcdeac2194fb296d599ce7929dfa4009',
16         'info_dict': {
17             'id': '1399128',
18             'ext': 'mp4',
19             'title': 'Harald Range, Generalbundesanwalt, zu den Ermittlungen',
20             'description': 'md5:69da3c61275b426426d711bde96463ab',
21             'thumbnail': 're:^http:.*\.jpg$',
22         },
23     }, {
24         'url': 'http://www.tagesschau.de/multimedia/sendung/ts-5727.html',
25         'md5': '3c54c1f6243d279b706bde660ceec633',
26         'info_dict': {
27             'id': '5727',
28             'ext': 'mp4',
29             'description': 'md5:695c01bfd98b7e313c501386327aea59',
30             'title': 'Sendung: tagesschau \t04.12.2014 20:00 Uhr',
31             'thumbnail': 're:^http:.*\.jpg$',
32         }
33     }, {
34         'url': 'http://www.tagesschau.de/multimedia/sendung/tsg-3771.html',
35         'md5': '90757268b49ef56deae90c7b48928d58',
36         'info_dict': {
37             'id': '3771',
38             'ext': 'mp4',
39             'description': '',
40             'title': 'Sendung: tagesschau (mit Gebärdensprache) \t14.07.2015 20:00 Uhr',
41             'thumbnail': 're:^http:.*\.jpg$',
42         }
43     }, {
44         'url': 'http://www.tagesschau.de/multimedia/sendung/tt-3827.html',
45         'md5': '6e3ebdc75e8d67da966a8d06721eda71',
46         'info_dict': {
47             'id': '3827',
48             'ext': 'mp4',
49             'description': 'md5:d511d0e278b0ad341a95ad9ab992ce66',
50             'title': 'Sendung: tagesthemen \t14.07.2015 22:15 Uhr',
51             'thumbnail': 're:^http:.*\.jpg$',
52         }
53     }, {
54         'url': 'http://www.tagesschau.de/multimedia/sendung/nm-3475.html',
55         'md5': '8a8875a568f0a5ae5ceef93c501a225f',
56         'info_dict': {
57             'id': '3475',
58             'ext': 'mp4',
59             'description': 'md5:ed149f5649cda3dac86813a9d777e131',
60             'title': 'Sendung: nachtmagazin \t15.07.2015 00:15 Uhr',
61             'thumbnail': 're:^http:.*\.jpg$',
62         }
63     }, {
64         'url': 'http://www.tagesschau.de/multimedia/tsvorzwanzig-959.html',
65         'md5': 'be4d6f0421f2acd8abe25ea29f6f015b',
66         'info_dict': {
67             'id': '959',
68             'ext': 'mp4',
69             'description': '',
70             'title': 'Sendung: tagesschau vor 20 Jahren \t14.07.2015 22:45 Uhr',
71             'thumbnail': 're:^http:.*\.jpg$',
72         }
73     }]
74
75     _FORMATS = {
76         's': {'width': 256, 'height': 144, 'quality': 1},
77         'm': {'width': 512, 'height': 288, 'quality': 2},
78         'l': {'width': 960, 'height': 544, 'quality': 3},
79     }
80
81     def _real_extract(self, url):
82         video_id = self._match_id(url)
83         display_id = video_id.lstrip('-')
84         webpage = self._download_webpage(url, display_id)
85
86         player_url = self._html_search_meta(
87             'twitter:player', webpage, 'player URL', default=None)
88         if player_url:
89             playerpage = self._download_webpage(
90                 player_url, display_id, 'Downloading player page')
91
92             medias = re.findall(
93                 r'"(http://media.+?)", type:"video/(.+?)", quality:"(.+?)"',
94                 playerpage)
95             formats = []
96             for url, ext, res in medias:
97                 f = {
98                     'format_id': res + '_' + ext,
99                     'url': url,
100                     'ext': ext,
101                 }
102                 f.update(self._FORMATS.get(res, {}))
103                 formats.append(f)
104             thumbnail_fn = re.findall(r'"(/multimedia/.+?\.jpg)"', playerpage)[-1]
105             title = self._og_search_title(webpage).strip()
106             description = self._og_search_description(webpage).strip()
107         else:
108             download_text = self._search_regex(
109                 r'(?s)<p>Wir bieten dieses Video in folgenden Formaten zum Download an:</p>\s*<div class="controls">(.*?)</div>\s*<p>',
110                 webpage, 'download links')
111             links = re.finditer(
112                 r'<div class="button" title="(?P<title>[^"]*)"><a href="(?P<url>[^"]+)">(?P<name>.+?)</a></div>',
113                 download_text)
114             formats = []
115             for l in links:
116                 format_id = self._search_regex(
117                     r'.*/[^/.]+\.([^/]+)\.[^/.]+', l.group('url'), 'format ID')
118                 format = {
119                     'format_id': format_id,
120                     'url': l.group('url'),
121                     'format_name': l.group('name'),
122                 }
123                 m = re.match(
124                     r'''(?x)
125                         Video:\s*(?P<vcodec>[a-zA-Z0-9/._-]+)\s*&\#10;
126                         (?P<width>[0-9]+)x(?P<height>[0-9]+)px&\#10;
127                         (?P<vbr>[0-9]+)kbps&\#10;
128                         Audio:\s*(?P<abr>[0-9]+)kbps,\s*(?P<audio_desc>[A-Za-z\.0-9]+)&\#10;
129                         Gr&ouml;&szlig;e:\s*(?P<filesize_approx>[0-9.,]+\s+[a-zA-Z]*B)''',
130                     l.group('title'))
131                 if m:
132                     format.update({
133                         'format_note': m.group('audio_desc'),
134                         'vcodec': m.group('vcodec'),
135                         'width': int(m.group('width')),
136                         'height': int(m.group('height')),
137                         'abr': int(m.group('abr')),
138                         'vbr': int(m.group('vbr')),
139                         'filesize_approx': parse_filesize(m.group('filesize_approx')),
140                     })
141                 formats.append(format)
142             thumbnail_fn = self._search_regex(
143                 r'(?s)<img alt="Sendungsbild".*?src="([^"]+)"',
144                 webpage, 'thumbnail', fatal=False)
145             # there are some videos without description
146             description = ""
147             try:
148                 description = self._html_search_regex(
149                     r'(?s)<p class="teasertext">(.*?)</p>',
150                     webpage, 'description', fatal=False)
151             except ExtractorError:
152                 pass
153             title = self._html_search_regex(
154                 r'<span class="headline".*?>(.*?)</span>', webpage, 'title')
155
156         self._sort_formats(formats)
157         thumbnail = 'http://www.tagesschau.de' + thumbnail_fn
158
159         return {
160             'id': display_id,
161             'title': title,
162             'thumbnail': thumbnail,
163             'formats': formats,
164             'description': description,
165         }