[infoq] Fix extraction (closes #6141)
[youtube-dl] / youtube_dl / extractor / infoq.py
1 from __future__ import unicode_literals
2
3 import base64
4
5 from .common import InfoExtractor
6 from ..compat import (
7     compat_urllib_parse,
8     compat_urlparse,
9 )
10
11
12 class InfoQIE(InfoExtractor):
13     _VALID_URL = r'https?://(?:www\.)?infoq\.com/(?:[^/]+/)+(?P<id>[^/]+)'
14
15     _TESTS = [{
16         'url': 'http://www.infoq.com/presentations/A-Few-of-My-Favorite-Python-Things',
17         'md5': 'b5ca0e0a8c1fed93b0e65e48e462f9a2',
18         'info_dict': {
19             'id': '12-jan-pythonthings',
20             'ext': 'mp4',
21             'description': 'Mike Pirnat presents some tips and tricks, standard libraries and third party packages that make programming in Python a richer experience.',
22             'title': 'A Few of My Favorite [Python] Things',
23         },
24     }, {
25         'url': 'http://www.infoq.com/fr/presentations/changez-avis-sur-javascript',
26         'only_matching': True,
27     }]
28
29     def _real_extract(self, url):
30         video_id = self._match_id(url)
31         webpage = self._download_webpage(url, video_id)
32
33         video_title = self._html_search_regex(r'<title>(.*?)</title>', webpage, 'title')
34         video_description = self._html_search_meta('description', webpage, 'description')
35
36         # The server URL is hardcoded
37         video_url = 'rtmpe://video.infoq.com/cfx/st/'
38
39         # Extract video URL
40         encoded_id = self._search_regex(
41             r"jsclassref\s*=\s*'([^']*)'", webpage, 'encoded id')
42         real_id = compat_urllib_parse.unquote(base64.b64decode(encoded_id.encode('ascii')).decode('utf-8'))
43         playpath = 'mp4:' + real_id
44
45         video_filename = playpath.split('/')[-1]
46         video_id, extension = video_filename.split('.')
47
48         http_base = self._search_regex(
49             r'EXPRESSINSTALL_SWF\s*=\s*[^"]*"((?:https?:)?//[^/"]+/)', webpage,
50             'HTTP base URL')
51
52         formats = [{
53             'format_id': 'rtmp',
54             'url': video_url,
55             'ext': extension,
56             'play_path': playpath,
57         }, {
58             'format_id': 'http',
59             'url': compat_urlparse.urljoin(url, http_base) + real_id,
60         }]
61         self._sort_formats(formats)
62
63         return {
64             'id': video_id,
65             'title': video_title,
66             'description': video_description,
67             'formats': formats,
68         }