]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/infoq.py
[misc] Add `hatch`, `ruff`, `pre-commit` and improve dev docs (#7409)
[yt-dlp.git] / yt_dlp / extractor / infoq.py
CommitLineData
e897bd82 1from .bokecc import BokeCCBaseIE
45024183 2from ..compat import (
cf282071 3 compat_b64decode,
45024183
MW
4 compat_urllib_parse_unquote,
5 compat_urlparse,
6)
e650659b 7from ..utils import (
b3602f68 8 ExtractorError,
e650659b 9 determine_ext,
b3602f68 10 traverse_obj,
e897bd82 11 update_url_query,
e650659b 12)
fda7d31a
PH
13
14
5633b4d3 15class 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 },
b3602f68 39 'skip': 'Sorry, the page you visited does not exist',
45024183
MW
40 }, {
41 'url': 'https://www.infoq.com/presentations/Simple-Made-Easy',
42 'md5': '0e34642d4d9ef44bf86f66f6399672db',
43 'info_dict': {
44 'id': 'Simple-Made-Easy',
45 'title': 'Simple Made Easy',
46 'ext': 'mp3',
47 'description': 'md5:3e0e213a8bbd074796ef89ea35ada25b',
48 },
49 'params': {
50 'format': 'bestaudio',
51 },
533f67d3 52 }]
fda7d31a 53
45024183 54 def _extract_rtmp_video(self, webpage):
7560096d 55 # The server URL is hardcoded
a0566bbf 56 video_url = 'rtmpe://videof.infoq.com/cfx/st/'
7560096d
KW
57
58 # Extract video URL
edec83a0 59 encoded_id = self._search_regex(
02a63fad
YCH
60 r"jsclassref\s*=\s*'([^']*)'", webpage, 'encoded id', default=None)
61
cf282071 62 real_id = compat_urllib_parse_unquote(compat_b64decode(encoded_id).decode('utf-8'))
7560096d 63 playpath = 'mp4:' + real_id
fda7d31a 64
02a63fad 65 return [{
45024183 66 'format_id': 'rtmp_video',
02a63fad
YCH
67 'url': video_url,
68 'ext': determine_ext(playpath),
69 'play_path': playpath,
70 }]
fda7d31a 71
e650659b 72 def _extract_cf_auth(self, webpage):
197224b7
S
73 policy = self._search_regex(r'InfoQConstants\.scp\s*=\s*\'([^\']+)\'', webpage, 'policy')
74 signature = self._search_regex(r'InfoQConstants\.scs\s*=\s*\'([^\']+)\'', webpage, 'signature')
75 key_pair_id = self._search_regex(r'InfoQConstants\.sck\s*=\s*\'([^\']+)\'', webpage, 'key-pair-id')
e650659b
RA
76 return {
77 'Policy': policy,
78 'Signature': signature,
79 'Key-Pair-Id': key_pair_id,
80 }
edec83a0 81
45024183
MW
82 def _extract_http_video(self, webpage):
83 http_video_url = self._search_regex(r'P\.s\s*=\s*\'([^\']+)\'', webpage, 'video URL')
e650659b 84 http_video_url = update_url_query(http_video_url, self._extract_cf_auth(webpage))
02a63fad 85 return [{
45024183 86 'format_id': 'http_video',
22d07ba4 87 'url': http_video_url,
a0566bbf 88 'http_headers': {'Referer': 'https://www.infoq.com/'},
edec83a0 89 }]
02a63fad 90
45024183 91 def _extract_http_audio(self, webpage, video_id):
b3602f68
ES
92 try:
93 http_audio_url = traverse_obj(self._form_hidden_inputs('mp3Form', webpage), 'filename')
94 except ExtractorError:
95 http_audio_url = None
74da8565 96 if not http_audio_url:
45024183
MW
97 return []
98
45024183
MW
99 # base URL is found in the Location header in the response returned by
100 # GET https://www.infoq.com/mp3download.action?filename=... when logged in.
a0566bbf 101 http_audio_url = compat_urlparse.urljoin('http://ress.infoq.com/downloads/mp3downloads/', http_audio_url)
e650659b 102 http_audio_url = update_url_query(http_audio_url, self._extract_cf_auth(webpage))
45024183
MW
103
104 # audio file seem to be missing some times even if there is a download link
105 # so probe URL to make sure
e650659b 106 if not self._is_valid_url(http_audio_url, video_id):
45024183
MW
107 return []
108
109 return [{
110 'format_id': 'http_audio',
111 'url': http_audio_url,
112 'vcodec': 'none',
45024183
MW
113 }]
114
02a63fad
YCH
115 def _real_extract(self, url):
116 video_id = self._match_id(url)
117 webpage = self._download_webpage(url, video_id)
118
04f3fd2c 119 video_title = self._html_extract_title(webpage)
02a63fad
YCH
120 video_description = self._html_search_meta('description', webpage, 'description')
121
122 if '/cn/' in url:
123 # for China videos, HTTP video URL exists but always fails with 403
5633b4d3 124 formats = self._extract_bokecc_formats(webpage, video_id)
02a63fad 125 else:
45024183 126 formats = (
3089bc74
S
127 self._extract_rtmp_video(webpage)
128 + self._extract_http_video(webpage)
129 + self._extract_http_audio(webpage, video_id))
02a63fad 130
c0a7c608 131 return {
fda7d31a 132 'id': video_id,
fda7d31a 133 'title': video_title,
fda7d31a 134 'description': video_description,
edec83a0 135 'formats': formats,
c0a7c608 136 }