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