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