[infoq] fix http format downloading
[youtube-dl] / youtube_dl / extractor / infoq.py
1 # coding: utf-8
2
3 from __future__ import unicode_literals
4
5 import base64
6
7 from ..compat import (
8     compat_urllib_parse_unquote,
9     compat_urlparse,
10 )
11 from ..utils import (
12     determine_ext,
13     update_url_query,
14 )
15 from .bokecc import BokeCCBaseIE
16
17
18 class InfoQIE(BokeCCBaseIE):
19     _VALID_URL = r'https?://(?:www\.)?infoq\.com/(?:[^/]+/)+(?P<id>[^/]+)'
20
21     _TESTS = [{
22         'url': 'http://www.infoq.com/presentations/A-Few-of-My-Favorite-Python-Things',
23         'md5': 'b5ca0e0a8c1fed93b0e65e48e462f9a2',
24         'info_dict': {
25             'id': 'A-Few-of-My-Favorite-Python-Things',
26             'ext': 'mp4',
27             'description': 'Mike Pirnat presents some tips and tricks, standard libraries and third party packages that make programming in Python a richer experience.',
28             'title': 'A Few of My Favorite [Python] Things',
29         },
30     }, {
31         'url': 'http://www.infoq.com/fr/presentations/changez-avis-sur-javascript',
32         'only_matching': True,
33     }, {
34         'url': 'http://www.infoq.com/cn/presentations/openstack-continued-delivery',
35         'md5': '4918d0cca1497f2244572caf626687ef',
36         'info_dict': {
37             'id': 'openstack-continued-delivery',
38             'title': 'OpenStack持续交付之路',
39             'ext': 'flv',
40             'description': 'md5:308d981fb28fa42f49f9568322c683ff',
41         },
42     }, {
43         'url': 'https://www.infoq.com/presentations/Simple-Made-Easy',
44         'md5': '0e34642d4d9ef44bf86f66f6399672db',
45         'info_dict': {
46             'id': 'Simple-Made-Easy',
47             'title': 'Simple Made Easy',
48             'ext': 'mp3',
49             'description': 'md5:3e0e213a8bbd074796ef89ea35ada25b',
50         },
51         'params': {
52             'format': 'bestaudio',
53         },
54     }]
55
56     def _extract_rtmp_video(self, webpage):
57         # The server URL is hardcoded
58         video_url = 'rtmpe://video.infoq.com/cfx/st/'
59
60         # Extract video URL
61         encoded_id = self._search_regex(
62             r"jsclassref\s*=\s*'([^']*)'", webpage, 'encoded id', default=None)
63
64         real_id = compat_urllib_parse_unquote(base64.b64decode(encoded_id.encode('ascii')).decode('utf-8'))
65         playpath = 'mp4:' + real_id
66
67         return [{
68             'format_id': 'rtmp_video',
69             'url': video_url,
70             'ext': determine_ext(playpath),
71             'play_path': playpath,
72         }]
73
74     def _extract_cf_auth(self, webpage):
75         policy = self._search_regex(r'InfoQConstants\.scp\s*=\s*\'([^\']+)\'', webpage, 'policy')
76         signature = self._search_regex(r'InfoQConstants\.scs\s*=\s*\'([^\']+)\'', webpage, 'signature')
77         key_pair_id = self._search_regex(r'InfoQConstants\.sck\s*=\s*\'([^\']+)\'', webpage, 'key-pair-id')
78         return {
79             'Policy': policy,
80             'Signature': signature,
81             'Key-Pair-Id': key_pair_id,
82         }
83
84     def _extract_http_video(self, webpage):
85         http_video_url = self._search_regex(r'P\.s\s*=\s*\'([^\']+)\'', webpage, 'video URL')
86         http_video_url = update_url_query(http_video_url, self._extract_cf_auth(webpage))
87         return [{
88             'format_id': 'http_video',
89             'url': http_video_url,
90         }]
91
92     def _extract_http_audio(self, webpage, video_id):
93         fields = self._hidden_inputs(webpage)
94         http_audio_url = fields.get('filename')
95         if not http_audio_url:
96             return []
97
98         # base URL is found in the Location header in the response returned by
99         # GET https://www.infoq.com/mp3download.action?filename=... when logged in.
100         http_audio_url = compat_urlparse.urljoin('http://res.infoq.com/downloads/mp3downloads/', http_audio_url)
101         http_audio_url = update_url_query(http_audio_url, self._extract_cf_auth(webpage))
102
103         # audio file seem to be missing some times even if there is a download link
104         # so probe URL to make sure
105         if not self._is_valid_url(http_audio_url, video_id):
106             return []
107
108         return [{
109             'format_id': 'http_audio',
110             'url': http_audio_url,
111             'vcodec': 'none',
112         }]
113
114     def _real_extract(self, url):
115         video_id = self._match_id(url)
116         webpage = self._download_webpage(url, video_id)
117
118         video_title = self._html_search_regex(r'<title>(.*?)</title>', webpage, 'title')
119         video_description = self._html_search_meta('description', webpage, 'description')
120
121         if '/cn/' in url:
122             # for China videos, HTTP video URL exists but always fails with 403
123             formats = self._extract_bokecc_formats(webpage, video_id)
124         else:
125             formats = (
126                 self._extract_rtmp_video(webpage) +
127                 self._extract_http_video(webpage) +
128                 self._extract_http_audio(webpage, video_id))
129
130         self._sort_formats(formats)
131
132         return {
133             'id': video_id,
134             'title': video_title,
135             'description': video_description,
136             'formats': formats,
137         }