[ard] Quote path part instead of whole URL encode
[youtube-dl] / youtube_dl / extractor / ard.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     determine_ext,
9     ExtractorError,
10     qualities,
11     compat_urllib_parse_urlparse,
12     compat_urllib_parse,
13 )
14
15
16 class ARDIE(InfoExtractor):
17     _VALID_URL = r'^https?://(?:(?:www\.)?ardmediathek\.de|mediathek\.daserste\.de)/(?:.*/)(?P<video_id>[0-9]+|[^0-9][^/\?]+)[^/\?]*(?:\?.*)?'
18
19     _TESTS = [{
20         'url': 'http://mediathek.daserste.de/sendungen_a-z/328454_anne-will/22429276_vertrauen-ist-gut-spionieren-ist-besser-geht',
21         'file': '22429276.mp4',
22         'md5': '469751912f1de0816a9fc9df8336476c',
23         'info_dict': {
24             'title': 'Vertrauen ist gut, Spionieren ist besser - Geht so deutsch-amerikanische Freundschaft?',
25             'description': 'Das Erste Mediathek [ARD]: Vertrauen ist gut, Spionieren ist besser - Geht so deutsch-amerikanische Freundschaft?, Anne Will, Über die Spionage-Affäre diskutieren Clemens Binninger, Katrin Göring-Eckardt, Georg Mascolo, Andrew B. Denison und Constanze Kurz.. Das Video zur Sendung Anne Will am Mittwoch, 16.07.2014',
26         },
27         'skip': 'Blocked outside of Germany',
28     }, {
29         'url': 'http://www.ardmediathek.de/tv/Tatort/Das-Wunder-von-Wolbeck-Video-tgl-ab-20/Das-Erste/Video?documentId=22490580&bcastId=602916',
30         'info_dict': {
31             'id': '22490580',
32             'ext': 'mp4',
33             'title': 'Das Wunder von Wolbeck (Video tgl. ab 20 Uhr)',
34             'description': 'Auf einem restaurierten Hof bei Wolbeck wird der Heilpraktiker Raffael Lembeck eines morgens von seiner Frau Stella tot aufgefunden. Das Opfer war offensichtlich in seiner Praxis zu Fall gekommen und ist dann verblutet, erklärt Prof. Boerne am Tatort.',
35         },
36         'skip': 'Blocked outside of Germany',
37     }]
38
39     def _real_extract(self, url):
40         # determine video id from url
41         m = re.match(self._VALID_URL, url)
42
43         numid = re.search(r'documentId=([0-9]+)', url)
44         if numid:
45             video_id = numid.group(1)
46         else:
47             video_id = m.group('video_id')
48
49         urlp = compat_urllib_parse_urlparse(url)
50         url = urlp._replace(path=compat_urllib_parse.quote(urlp.path.encode('utf-8'))).geturl()
51
52         webpage = self._download_webpage(url, video_id)
53
54         title = self._html_search_regex(
55             [r'<h1(?:\s+class="boxTopHeadline")?>(.*?)</h1>',
56              r'<meta name="dcterms.title" content="(.*?)"/>',
57              r'<h4 class="headline">(.*?)</h4>'],
58             webpage, 'title')
59         description = self._html_search_meta(
60             'dcterms.abstract', webpage, 'description', default=None)
61         if description is None:
62             description = self._html_search_meta(
63                 'description', webpage, 'meta description')
64
65         # Thumbnail is sometimes not present.
66         # It is in the mobile version, but that seems to use a different URL
67         # structure altogether.
68         thumbnail = self._og_search_thumbnail(webpage, default=None)
69
70         media_streams = re.findall(r'''(?x)
71             mediaCollection\.addMediaStream\([0-9]+,\s*[0-9]+,\s*"[^"]*",\s*
72             "([^"]+)"''', webpage)
73
74         if media_streams:
75             QUALITIES = qualities(['lo', 'hi', 'hq'])
76             formats = []
77             for furl in set(media_streams):
78                 if furl.endswith('.f4m'):
79                     fid = 'f4m'
80                 else:
81                     fid_m = re.match(r'.*\.([^.]+)\.[^.]+$', furl)
82                     fid = fid_m.group(1) if fid_m else None
83                 formats.append({
84                     'quality': QUALITIES(fid),
85                     'format_id': fid,
86                     'url': furl,
87                 })
88         else:  # request JSON file
89             media_info = self._download_json(
90                 'http://www.ardmediathek.de/play/media/%s' % video_id, video_id)
91             # The second element of the _mediaArray contains the standard http urls
92             streams = media_info['_mediaArray'][1]['_mediaStreamArray']
93             if not streams:
94                 if '"fsk"' in webpage:
95                     raise ExtractorError('This video is only available after 20:00')
96
97             formats = []
98             for s in streams:
99                 if type(s['_stream']) == list:
100                     for index, url in enumerate(s['_stream'][::-1]):
101                         quality = s['_quality'] + index
102                         formats.append({
103                             'quality': quality,
104                             'url': url,
105                             'format_id': '%s-%s' % (determine_ext(url), quality)
106                         })
107                     continue
108
109                 format = {
110                     'quality': s['_quality'],
111                     'url': s['_stream'],
112                 }
113
114                 format['format_id'] = '%s-%s' % (
115                     determine_ext(format['url']), format['quality'])
116
117                 formats.append(format)
118
119         self._sort_formats(formats)
120
121         return {
122             'id': video_id,
123             'title': title,
124             'description': description,
125             'formats': formats,
126             'thumbnail': thumbnail,
127         }