Merge pull request #7045 from remitamine/ign
[youtube-dl] / youtube_dl / extractor / infoq.py
1 # coding: utf-8
2
3 from __future__ import unicode_literals
4
5 import base64
6
7 from .common import InfoExtractor
8 from ..compat import (
9     compat_urllib_parse_unquote,
10     compat_parse_qs,
11 )
12 from ..utils import determine_ext
13
14
15 class InfoQIE(InfoExtractor):
16     _VALID_URL = r'https?://(?:www\.)?infoq\.com/(?:[^/]+/)+(?P<id>[^/]+)'
17
18     _TESTS = [{
19         'url': 'http://www.infoq.com/presentations/A-Few-of-My-Favorite-Python-Things',
20         'md5': 'b5ca0e0a8c1fed93b0e65e48e462f9a2',
21         'info_dict': {
22             'id': 'A-Few-of-My-Favorite-Python-Things',
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',
26         },
27     }, {
28         'url': 'http://www.infoq.com/fr/presentations/changez-avis-sur-javascript',
29         'only_matching': True,
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         },
39     }]
40
41     def _extract_bokecc_videos(self, webpage, video_id):
42         # TODO: bokecc.com is a Chinese video cloud platform
43         # It should have an independent extractor but I don't have other
44         # examples using bokecc
45         player_params_str = self._html_search_regex(
46             r'<script[^>]+src="http://p\.bokecc\.com/player\?([^"]+)',
47             webpage, 'player params', default=None)
48
49         player_params = compat_parse_qs(player_params_str)
50
51         info_xml = self._download_xml(
52             'http://p.bokecc.com/servlet/playinfo?uid=%s&vid=%s&m=1' % (
53                 player_params['siteid'][0], player_params['vid'][0]), video_id)
54
55         return [{
56             'format_id': 'bokecc',
57             'url': quality.find('./copy').attrib['playurl'],
58             'preference': int(quality.attrib['value']),
59         } for quality in info_xml.findall('./video/quality')]
60
61     def _extract_rtmp_videos(self, webpage):
62         # The server URL is hardcoded
63         video_url = 'rtmpe://video.infoq.com/cfx/st/'
64
65         # Extract video URL
66         encoded_id = self._search_regex(
67             r"jsclassref\s*=\s*'([^']*)'", webpage, 'encoded id', default=None)
68
69         real_id = compat_urllib_parse_unquote(base64.b64decode(encoded_id.encode('ascii')).decode('utf-8'))
70         playpath = 'mp4:' + real_id
71
72         return [{
73             'format_id': 'rtmp',
74             'url': video_url,
75             'ext': determine_ext(playpath),
76             'play_path': playpath,
77         }]
78
79     def _extract_http_videos(self, webpage):
80         http_video_url = self._search_regex(r'P\.s\s*=\s*\'([^\']+)\'', webpage, 'video URL')
81
82         policy = self._search_regex(r'InfoQConstants.scp\s*=\s*\'([^\']+)\'', webpage, 'policy')
83         signature = self._search_regex(r'InfoQConstants.scs\s*=\s*\'([^\']+)\'', webpage, 'signature')
84         key_pair_id = self._search_regex(r'InfoQConstants.sck\s*=\s*\'([^\']+)\'', webpage, 'key-pair-id')
85
86         return [{
87             'format_id': 'http',
88             'url': http_video_url,
89             'http_headers': {
90                 'Cookie': 'CloudFront-Policy=%s; CloudFront-Signature=%s; CloudFront-Key-Pair-Id=%s' % (
91                     policy, signature, key_pair_id),
92             },
93         }]
94
95     def _real_extract(self, url):
96         video_id = self._match_id(url)
97         webpage = self._download_webpage(url, video_id)
98
99         video_title = self._html_search_regex(r'<title>(.*?)</title>', webpage, 'title')
100         video_description = self._html_search_meta('description', webpage, 'description')
101
102         if '/cn/' in url:
103             # for China videos, HTTP video URL exists but always fails with 403
104             formats = self._extract_bokecc_videos(webpage, video_id)
105         else:
106             formats = self._extract_rtmp_videos(webpage) + self._extract_http_videos(webpage)
107
108         self._sort_formats(formats)
109
110         return {
111             'id': video_id,
112             'title': video_title,
113             'description': video_description,
114             'formats': formats,
115         }