[shahid] add default fallbacks for extracting api vars
[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/(?:sendung/ts|video/video)(?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
35     _FORMATS = {
36         's': {'width': 256, 'height': 144, 'quality': 1},
37         'm': {'width': 512, 'height': 288, 'quality': 2},
38         'l': {'width': 960, 'height': 544, 'quality': 3},
39     }
40
41     def _real_extract(self, url):
42         video_id = self._match_id(url)
43         display_id = video_id.lstrip('-')
44         webpage = self._download_webpage(url, display_id)
45
46         player_url = self._html_search_meta(
47             'twitter:player', webpage, 'player URL', default=None)
48         if player_url:
49             playerpage = self._download_webpage(
50                 player_url, display_id, 'Downloading player page')
51
52             medias = re.findall(
53                 r'"(http://media.+?)", type:"video/(.+?)", quality:"(.+?)"',
54                 playerpage)
55             formats = []
56             for url, ext, res in medias:
57                 f = {
58                     'format_id': res + '_' + ext,
59                     'url': url,
60                     'ext': ext,
61                 }
62                 f.update(self._FORMATS.get(res, {}))
63                 formats.append(f)
64             thumbnail_fn = re.findall(r'"(/multimedia/.+?\.jpg)"', playerpage)[-1]
65             title = self._og_search_title(webpage).strip()
66             description = self._og_search_description(webpage).strip()
67         else:
68             download_text = self._search_regex(
69                 r'(?s)<p>Wir bieten dieses Video in folgenden Formaten zum Download an:</p>\s*<div class="controls">(.*?)</div>\s*<p>',
70                 webpage, 'download links')
71             links = re.finditer(
72                 r'<div class="button" title="(?P<title>[^"]*)"><a href="(?P<url>[^"]+)">(?P<name>.+?)</a></div>',
73                 download_text)
74             formats = []
75             for l in links:
76                 format_id = self._search_regex(
77                     r'.*/[^/.]+\.([^/]+)\.[^/.]+', l.group('url'), 'format ID')
78                 format = {
79                     'format_id': format_id,
80                     'url': l.group('url'),
81                     'format_name': l.group('name'),
82                 }
83                 m = re.match(
84                     r'''(?x)
85                         Video:\s*(?P<vcodec>[a-zA-Z0-9/._-]+)\s*&\#10;
86                         (?P<width>[0-9]+)x(?P<height>[0-9]+)px&\#10;
87                         (?P<vbr>[0-9]+)kbps&\#10;
88                         Audio:\s*(?P<abr>[0-9]+)kbps,\s*(?P<audio_desc>[A-Za-z\.0-9]+)&\#10;
89                         Gr&ouml;&szlig;e:\s*(?P<filesize_approx>[0-9.,]+\s+[a-zA-Z]*B)''',
90                     l.group('title'))
91                 if m:
92                     format.update({
93                         'format_note': m.group('audio_desc'),
94                         'vcodec': m.group('vcodec'),
95                         'width': int(m.group('width')),
96                         'height': int(m.group('height')),
97                         'abr': int(m.group('abr')),
98                         'vbr': int(m.group('vbr')),
99                         'filesize_approx': parse_filesize(m.group('filesize_approx')),
100                     })
101                 formats.append(format)
102             thumbnail_fn = self._search_regex(
103                 r'(?s)<img alt="Sendungsbild".*?src="([^"]+)"',
104                 webpage, 'thumbnail', fatal=False)
105             description = self._html_search_regex(
106                 r'(?s)<p class="teasertext">(.*?)</p>',
107                 webpage, 'description', fatal=False)
108             title = self._html_search_regex(
109                 r'<span class="headline".*?>(.*?)</span>', webpage, 'title')
110
111         self._sort_formats(formats)
112         thumbnail = 'http://www.tagesschau.de' + thumbnail_fn
113
114         return {
115             'id': display_id,
116             'title': title,
117             'thumbnail': thumbnail,
118             'formats': formats,
119             'description': description,
120         }