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