[tagesschau] Relax _VALID_URL
[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
8
9
10 class TagesschauIE(InfoExtractor):
11     _VALID_URL = r'https?://(?:www\.)?tagesschau\.de/multimedia/(?:[^/]+/)*?[^/#?]+?(?P<id>-?[0-9]+)(?:~_?[^/#?]+?)?\.html'
12
13     _TESTS = [{
14         'url': 'http://www.tagesschau.de/multimedia/video/video-102143.html',
15         'md5': '917a228bc7df7850783bc47979673a09',
16         'info_dict': {
17             'id': '102143',
18             'ext': 'mp4',
19             'title': 'Regierungsumbildung in Athen: Neue Minister in Griechenland vereidigt',
20             'description': 'md5:171feccd9d9b3dd54d05d501568f6359',
21             'thumbnail': 're:^https?:.*\.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:^https?:.*\.jpg$',
32         },
33     }, {
34         'url': 'http://www.tagesschau.de/multimedia/politikimradio/audio-18407.html',
35         'md5': 'aef45de271c4bf0a5db834aa40bf774c',
36         'info_dict': {
37             'id': '18407',
38             'ext': 'mp3',
39             'title': 'Flüchtlingsdebatte: Hitzig, aber wenig hilfreich',
40             'description': 'Flüchtlingsdebatte: Hitzig, aber wenig hilfreich',
41             'thumbnail': 're:^https?:.*\.jpg$',
42         },
43     }, {
44         'url': 'http://www.tagesschau.de/multimedia/sendung/tsg-3771.html',
45         'only_matching': True,
46     }, {
47         'url': 'http://www.tagesschau.de/multimedia/sendung/tt-3827.html',
48         'only_matching': True,
49     }, {
50         'url': 'http://www.tagesschau.de/multimedia/sendung/nm-3475.html',
51         'only_matching': True,
52     }, {
53         'url': 'http://www.tagesschau.de/multimedia/sendung/weltspiegel-3167.html',
54         'only_matching': True,
55     }, {
56         'url': 'http://www.tagesschau.de/multimedia/tsvorzwanzig-959.html',
57         'only_matching': True,
58     }, {
59         'url': 'http://www.tagesschau.de/multimedia/sendung/bab/bab-3299~_bab-sendung-209.html',
60         'only_matching': True,
61     }, {
62         'url': 'http://www.tagesschau.de/multimedia/video/video-102303~_bab-sendung-211.html',
63         'only_matching': True,
64     }, {
65         'url': 'http://www.tagesschau.de/multimedia/video/video-179517~player.html',
66         'only_matching': True,
67     }]
68
69     _FORMATS = {
70         'xs': {'quality': 0},
71         's': {'width': 320, 'height': 180, 'quality': 1},
72         'm': {'width': 512, 'height': 288, 'quality': 2},
73         'l': {'width': 960, 'height': 540, 'quality': 3},
74         'xl': {'width': 1280, 'height': 720, 'quality': 4},
75         'xxl': {'quality': 5},
76     }
77
78     def _real_extract(self, url):
79         video_id = self._match_id(url)
80         display_id = video_id.lstrip('-')
81         webpage = self._download_webpage(url, display_id)
82
83         player_url = self._html_search_meta(
84             'twitter:player', webpage, 'player URL', default=None)
85         if player_url:
86             playerpage = self._download_webpage(
87                 player_url, display_id, 'Downloading player page')
88
89             formats = []
90             for media in re.finditer(
91                     r'''(?x)
92                         (?P<q_url>["\'])(?P<url>http://media.+?)(?P=q_url)
93                         ,\s*type:(?P<q_type>["\'])(?P<type>video|audio)/(?P<ext>.+?)(?P=q_type)
94                         (?:,\s*quality:(?P<q_quality>["\'])(?P<quality>.+?)(?P=q_quality))?
95                     ''', playerpage):
96                 url = media.group('url')
97                 type_ = media.group('type')
98                 ext = media.group('ext')
99                 res = media.group('quality')
100                 f = {
101                     'format_id': '%s_%s' % (res, ext) if res else ext,
102                     'url': url,
103                     'ext': ext,
104                     'vcodec': 'none' if type_ == 'audio' else None,
105                 }
106                 f.update(self._FORMATS.get(res, {}))
107                 formats.append(f)
108             thumbnail = self._og_search_thumbnail(playerpage)
109             title = self._og_search_title(webpage).strip()
110             description = self._og_search_description(webpage).strip()
111         else:
112             download_text = self._search_regex(
113                 r'(?s)<p>Wir bieten dieses Video in folgenden Formaten zum Download an:</p>\s*<div class="controls">(.*?)</div>\s*<p>',
114                 webpage, 'download links')
115             links = re.finditer(
116                 r'<div class="button" title="(?P<title>[^"]*)"><a href="(?P<url>[^"]+)">(?P<name>.+?)</a></div>',
117                 download_text)
118             formats = []
119             for l in links:
120                 format_id = self._search_regex(
121                     r'.*/[^/.]+\.([^/]+)\.[^/.]+', l.group('url'), 'format ID')
122                 format = {
123                     'format_id': format_id,
124                     'url': l.group('url'),
125                     'format_name': l.group('name'),
126                 }
127                 m = re.match(
128                     r'''(?x)
129                         Video:\s*(?P<vcodec>[a-zA-Z0-9/._-]+)\s*&\#10;
130                         (?P<width>[0-9]+)x(?P<height>[0-9]+)px&\#10;
131                         (?P<vbr>[0-9]+)kbps&\#10;
132                         Audio:\s*(?P<abr>[0-9]+)kbps,\s*(?P<audio_desc>[A-Za-z\.0-9]+)&\#10;
133                         Gr&ouml;&szlig;e:\s*(?P<filesize_approx>[0-9.,]+\s+[a-zA-Z]*B)''',
134                     l.group('title'))
135                 if m:
136                     format.update({
137                         'format_note': m.group('audio_desc'),
138                         'vcodec': m.group('vcodec'),
139                         'width': int(m.group('width')),
140                         'height': int(m.group('height')),
141                         'abr': int(m.group('abr')),
142                         'vbr': int(m.group('vbr')),
143                         'filesize_approx': parse_filesize(m.group('filesize_approx')),
144                     })
145                 formats.append(format)
146             thumbnail = self._og_search_thumbnail(webpage)
147             description = self._html_search_regex(
148                 r'(?s)<p class="teasertext">(.*?)</p>',
149                 webpage, 'description', default=None)
150             title = self._html_search_regex(
151                 r'<span class="headline".*?>(.*?)</span>', webpage, 'title')
152
153         self._sort_formats(formats)
154
155         return {
156             'id': display_id,
157             'title': title,
158             'thumbnail': thumbnail,
159             'formats': formats,
160             'description': description,
161         }