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