e1ecdf4d3672215068bf223c282daa5f9609a02e
[youtube-dl] / youtube_dl / extractor / ard.py
1 import re
2
3 from .common import InfoExtractor
4 from ..utils import (
5     ExtractorError,
6 )
7
8 class ARDIE(InfoExtractor):
9     _VALID_URL = r'^(?:https?://)?(?:(?:www\.)?ardmediathek\.de|mediathek\.daserste\.de)/(?:.*/)(?P<video_id>[^/\?]+)(?:\?.*)?'
10     _TITLE = r'<h1(?: class="boxTopHeadline")?>(?P<title>.*)</h1>'
11     _MEDIA_STREAM = r'mediaCollection\.addMediaStream\((?P<media_type>\d+), (?P<quality>\d+), "(?P<rtmp_url>[^"]*)", "(?P<video_url>[^"]*)", "[^"]*"\)'
12
13     def _real_extract(self, url):
14         # determine video id from url
15         m = re.match(self._VALID_URL, url)
16
17         numid = re.search(r'documentId=([0-9]+)', url)
18         if numid:
19             video_id = numid.group(1)
20         else:
21             video_id = m.group('video_id')
22
23         # determine title and media streams from webpage
24         html = self._download_webpage(url, video_id)
25         title = re.search(self._TITLE, html).group('title')
26         streams = [m.groupdict() for m in re.finditer(self._MEDIA_STREAM, html)]
27         if not streams:
28             assert '"fsk"' in html
29             raise ExtractorError(u'This video is only available after 8:00 pm')
30
31         # choose default media type and highest quality for now
32         stream = max([s for s in streams if int(s["media_type"]) == 0],
33                      key=lambda s: int(s["quality"]))
34
35         # there's two possibilities: RTMP stream or HTTP download
36         info = {'id': video_id, 'title': title, 'ext': 'mp4'}
37         if stream['rtmp_url']:
38             self.to_screen(u'RTMP download detected')
39             assert stream['video_url'].startswith('mp4:')
40             info["url"] = stream["rtmp_url"]
41             info["play_path"] = stream['video_url']
42         else:
43             assert stream["video_url"].endswith('.mp4')
44             info["url"] = stream["video_url"]
45         return [info]