[infoq] Fix extraction for HTTP URLs (closes #7739)
[youtube-dl] / youtube_dl / extractor / infoq.py
1 from __future__ import unicode_literals
2
3 import base64
4
5 from .common import InfoExtractor
6 from ..compat import compat_urllib_parse_unquote
7
8
9 class InfoQIE(InfoExtractor):
10     _VALID_URL = r'https?://(?:www\.)?infoq\.com/(?:[^/]+/)+(?P<id>[^/]+)'
11
12     _TESTS = [{
13         'url': 'http://www.infoq.com/presentations/A-Few-of-My-Favorite-Python-Things',
14         'md5': 'b5ca0e0a8c1fed93b0e65e48e462f9a2',
15         'info_dict': {
16             'id': '12-jan-pythonthings',
17             'ext': 'mp4',
18             'description': 'Mike Pirnat presents some tips and tricks, standard libraries and third party packages that make programming in Python a richer experience.',
19             'title': 'A Few of My Favorite [Python] Things',
20         },
21     }, {
22         'url': 'http://www.infoq.com/fr/presentations/changez-avis-sur-javascript',
23         'only_matching': True,
24     }]
25
26     def _real_extract(self, url):
27         video_id = self._match_id(url)
28         webpage = self._download_webpage(url, video_id)
29
30         video_title = self._html_search_regex(r'<title>(.*?)</title>', webpage, 'title')
31         video_description = self._html_search_meta('description', webpage, 'description')
32
33         # The server URL is hardcoded
34         video_url = 'rtmpe://video.infoq.com/cfx/st/'
35
36         # Extract video URL
37         encoded_id = self._search_regex(
38             r"jsclassref\s*=\s*'([^']*)'", webpage, 'encoded id')
39         real_id = compat_urllib_parse_unquote(base64.b64decode(encoded_id.encode('ascii')).decode('utf-8'))
40         playpath = 'mp4:' + real_id
41
42         video_filename = playpath.split('/')[-1]
43         video_id, extension = video_filename.split('.')
44
45         http_video_url = self._search_regex(r'P\.s\s*=\s*\'([^\']+)\'', webpage, 'video URL')
46
47         policy = self._search_regex(r'InfoQConstants.scp\s*=\s*\'([^\']+)\'', webpage, 'policy')
48         signature = self._search_regex(r'InfoQConstants.scs\s*=\s*\'([^\']+)\'', webpage, 'signature')
49         key_pair_id = self._search_regex(r'InfoQConstants.sck\s*=\s*\'([^\']+)\'', webpage, 'key-pair-id')
50
51         formats = [{
52             'format_id': 'rtmp',
53             'url': video_url,
54             'ext': extension,
55             'play_path': playpath,
56         }, {
57             'format_id': 'http',
58             'url': http_video_url,
59             'http_headers': {
60                 'Cookie': 'CloudFront-Policy=%s; CloudFront-Signature=%s; CloudFront-Key-Pair-Id=%s' % (
61                     policy, signature, key_pair_id),
62             },
63         }]
64         self._sort_formats(formats)
65
66         return {
67             'id': video_id,
68             'title': video_title,
69             'description': video_description,
70             'formats': formats,
71         }