[youtube] Fix extraction.
[youtube-dl] / youtube_dl / extractor / threeqsdn.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..utils import (
7     determine_ext,
8     js_to_json,
9     mimetype2ext,
10 )
11
12
13 class ThreeQSDNIE(InfoExtractor):
14     IE_NAME = '3qsdn'
15     IE_DESC = '3Q SDN'
16     _VALID_URL = r'https?://playout\.3qsdn\.com/(?P<id>[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12})'
17     _TESTS = [{
18         # ondemand from http://www.philharmonie.tv/veranstaltung/26/
19         'url': 'http://playout.3qsdn.com/0280d6b9-1215-11e6-b427-0cc47a188158?protocol=http',
20         'md5': 'ab040e37bcfa2e0c079f92cb1dd7f6cd',
21         'info_dict': {
22             'id': '0280d6b9-1215-11e6-b427-0cc47a188158',
23             'ext': 'mp4',
24             'title': '0280d6b9-1215-11e6-b427-0cc47a188158',
25             'is_live': False,
26         },
27         'expected_warnings': ['Failed to download MPD manifest', 'Failed to parse JSON'],
28     }, {
29         # live video stream
30         'url': 'https://playout.3qsdn.com/d755d94b-4ab9-11e3-9162-0025907ad44f?js=true',
31         'info_dict': {
32             'id': 'd755d94b-4ab9-11e3-9162-0025907ad44f',
33             'ext': 'mp4',
34             'title': 're:^d755d94b-4ab9-11e3-9162-0025907ad44f [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
35             'is_live': True,
36         },
37         'params': {
38             'skip_download': True,  # m3u8 downloads
39         },
40         'expected_warnings': ['Failed to download MPD manifest'],
41     }, {
42         # live audio stream
43         'url': 'http://playout.3qsdn.com/9edf36e0-6bf2-11e2-a16a-9acf09e2db48',
44         'only_matching': True,
45     }, {
46         # live audio stream with some 404 URLs
47         'url': 'http://playout.3qsdn.com/ac5c3186-777a-11e2-9c30-9acf09e2db48',
48         'only_matching': True,
49     }, {
50         # geo restricted with 'This content is not available in your country'
51         'url': 'http://playout.3qsdn.com/d63a3ffe-75e8-11e2-9c30-9acf09e2db48',
52         'only_matching': True,
53     }, {
54         # geo restricted with 'playout.3qsdn.com/forbidden'
55         'url': 'http://playout.3qsdn.com/8e330f26-6ae2-11e2-a16a-9acf09e2db48',
56         'only_matching': True,
57     }, {
58         # live video with rtmp link
59         'url': 'https://playout.3qsdn.com/6092bb9e-8f72-11e4-a173-002590c750be',
60         'only_matching': True,
61     }]
62
63     @staticmethod
64     def _extract_url(webpage):
65         mobj = re.search(
66             r'<iframe[^>]+\b(?:data-)?src=(["\'])(?P<url>%s.*?)\1' % ThreeQSDNIE._VALID_URL, webpage)
67         if mobj:
68             return mobj.group('url')
69
70     def _real_extract(self, url):
71         video_id = self._match_id(url)
72
73         js = self._download_webpage(
74             'http://playout.3qsdn.com/%s' % video_id, video_id,
75             query={'js': 'true'})
76
77         if any(p in js for p in (
78                 '>This content is not available in your country',
79                 'playout.3qsdn.com/forbidden')):
80             self.raise_geo_restricted()
81
82         stream_content = self._search_regex(
83             r'streamContent\s*:\s*(["\'])(?P<content>.+?)\1', js,
84             'stream content', default='demand', group='content')
85
86         live = stream_content == 'live'
87
88         stream_type = self._search_regex(
89             r'streamType\s*:\s*(["\'])(?P<type>audio|video)\1', js,
90             'stream type', default='video', group='type')
91
92         formats = []
93         urls = set()
94
95         def extract_formats(item_url, item={}):
96             if not item_url or item_url in urls:
97                 return
98             urls.add(item_url)
99             ext = mimetype2ext(item.get('type')) or determine_ext(item_url, default_ext=None)
100             if ext == 'mpd':
101                 formats.extend(self._extract_mpd_formats(
102                     item_url, video_id, mpd_id='mpd', fatal=False))
103             elif ext == 'm3u8':
104                 formats.extend(self._extract_m3u8_formats(
105                     item_url, video_id, 'mp4',
106                     entry_protocol='m3u8' if live else 'm3u8_native',
107                     m3u8_id='hls', fatal=False))
108             elif ext == 'f4m':
109                 formats.extend(self._extract_f4m_formats(
110                     item_url, video_id, f4m_id='hds', fatal=False))
111             else:
112                 if not self._is_valid_url(item_url, video_id):
113                     return
114                 formats.append({
115                     'url': item_url,
116                     'format_id': item.get('quality'),
117                     'ext': 'mp4' if item_url.startswith('rtsp') else ext,
118                     'vcodec': 'none' if stream_type == 'audio' else None,
119                 })
120
121         for item_js in re.findall(r'({[^{]*?\b(?:src|source)\s*:\s*["\'].+?})', js):
122             f = self._parse_json(
123                 item_js, video_id, transform_source=js_to_json, fatal=False)
124             if not f:
125                 continue
126             extract_formats(f.get('src'), f)
127
128         # More relaxed version to collect additional URLs and acting
129         # as a future-proof fallback
130         for _, src in re.findall(r'\b(?:src|source)\s*:\s*(["\'])((?:https?|rtsp)://.+?)\1', js):
131             extract_formats(src)
132
133         self._sort_formats(formats)
134
135         title = self._live_title(video_id) if live else video_id
136
137         return {
138             'id': video_id,
139             'title': title,
140             'is_live': live,
141             'formats': formats,
142         }