[ie/wistia] Support password-protected videos (#11100)
[yt-dlp3.git] / yt_dlp / extractor / infoq.py
blob5274c9339fa86d96f48825063edacddae0b46541
1 import base64
2 import urllib.parse
4 from .bokecc import BokeCCBaseIE
5 from ..utils import (
6 ExtractorError,
7 determine_ext,
8 traverse_obj,
9 update_url_query,
13 class InfoQIE(BokeCCBaseIE):
14 _VALID_URL = r'https?://(?:www\.)?infoq\.com/(?:[^/]+/)+(?P<id>[^/]+)'
16 _TESTS = [{
17 'url': 'http://www.infoq.com/presentations/A-Few-of-My-Favorite-Python-Things',
18 'md5': 'b5ca0e0a8c1fed93b0e65e48e462f9a2',
19 'info_dict': {
20 'id': 'A-Few-of-My-Favorite-Python-Things',
21 'ext': 'mp4',
22 'description': 'Mike Pirnat presents some tips and tricks, standard libraries and third party packages that make programming in Python a richer experience.',
23 'title': 'A Few of My Favorite [Python] Things',
25 }, {
26 'url': 'http://www.infoq.com/fr/presentations/changez-avis-sur-javascript',
27 'only_matching': True,
28 }, {
29 'url': 'http://www.infoq.com/cn/presentations/openstack-continued-delivery',
30 'md5': '4918d0cca1497f2244572caf626687ef',
31 'info_dict': {
32 'id': 'openstack-continued-delivery',
33 'title': 'OpenStack持续交付之路',
34 'ext': 'flv',
35 'description': 'md5:308d981fb28fa42f49f9568322c683ff',
37 'skip': 'Sorry, the page you visited does not exist',
38 }, {
39 'url': 'https://www.infoq.com/presentations/Simple-Made-Easy',
40 'md5': '0e34642d4d9ef44bf86f66f6399672db',
41 'info_dict': {
42 'id': 'Simple-Made-Easy',
43 'title': 'Simple Made Easy',
44 'ext': 'mp3',
45 'description': 'md5:3e0e213a8bbd074796ef89ea35ada25b',
47 'params': {
48 'format': 'bestaudio',
52 def _extract_rtmp_video(self, webpage):
53 # The server URL is hardcoded
54 video_url = 'rtmpe://videof.infoq.com/cfx/st/'
56 # Extract video URL
57 encoded_id = self._search_regex(
58 r"jsclassref\s*=\s*'([^']*)'", webpage, 'encoded id', default=None)
60 real_id = urllib.parse.unquote(base64.b64decode(encoded_id).decode('utf-8'))
61 playpath = 'mp4:' + real_id
63 return [{
64 'format_id': 'rtmp_video',
65 'url': video_url,
66 'ext': determine_ext(playpath),
67 'play_path': playpath,
70 def _extract_cf_auth(self, webpage):
71 policy = self._search_regex(r'InfoQConstants\.scp\s*=\s*\'([^\']+)\'', webpage, 'policy')
72 signature = self._search_regex(r'InfoQConstants\.scs\s*=\s*\'([^\']+)\'', webpage, 'signature')
73 key_pair_id = self._search_regex(r'InfoQConstants\.sck\s*=\s*\'([^\']+)\'', webpage, 'key-pair-id')
74 return {
75 'Policy': policy,
76 'Signature': signature,
77 'Key-Pair-Id': key_pair_id,
80 def _extract_http_video(self, webpage):
81 http_video_url = self._search_regex(r'P\.s\s*=\s*\'([^\']+)\'', webpage, 'video URL')
82 http_video_url = update_url_query(http_video_url, self._extract_cf_auth(webpage))
83 return [{
84 'format_id': 'http_video',
85 'url': http_video_url,
86 'http_headers': {'Referer': 'https://www.infoq.com/'},
89 def _extract_http_audio(self, webpage, video_id):
90 try:
91 http_audio_url = traverse_obj(self._form_hidden_inputs('mp3Form', webpage), 'filename')
92 except ExtractorError:
93 http_audio_url = None
94 if not http_audio_url:
95 return []
97 # base URL is found in the Location header in the response returned by
98 # GET https://www.infoq.com/mp3download.action?filename=... when logged in.
99 http_audio_url = urllib.parse.urljoin('http://ress.infoq.com/downloads/mp3downloads/', http_audio_url)
100 http_audio_url = update_url_query(http_audio_url, self._extract_cf_auth(webpage))
102 # audio file seem to be missing some times even if there is a download link
103 # so probe URL to make sure
104 if not self._is_valid_url(http_audio_url, video_id):
105 return []
107 return [{
108 'format_id': 'http_audio',
109 'url': http_audio_url,
110 'vcodec': 'none',
113 def _real_extract(self, url):
114 video_id = self._match_id(url)
115 webpage = self._download_webpage(url, video_id)
117 video_title = self._html_extract_title(webpage)
118 video_description = self._html_search_meta('description', webpage, 'description')
120 if '/cn/' in url:
121 # for China videos, HTTP video URL exists but always fails with 403
122 formats = self._extract_bokecc_formats(webpage, video_id)
123 else:
124 formats = (
125 self._extract_rtmp_video(webpage)
126 + self._extract_http_video(webpage)
127 + self._extract_http_audio(webpage, video_id))
129 return {
130 'id': video_id,
131 'title': video_title,
132 'description': video_description,
133 'formats': formats,