infoq.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. from __future__ import unicode_literals
  2. import base64
  3. from .common import InfoExtractor
  4. from ..compat import compat_urllib_parse_unquote
  5. class InfoQIE(InfoExtractor):
  6. _VALID_URL = r'https?://(?:www\.)?infoq\.com/(?:[^/]+/)+(?P<id>[^/]+)'
  7. _TESTS = [{
  8. 'url': 'http://www.infoq.com/presentations/A-Few-of-My-Favorite-Python-Things',
  9. 'md5': 'b5ca0e0a8c1fed93b0e65e48e462f9a2',
  10. 'info_dict': {
  11. 'id': '12-jan-pythonthings',
  12. 'ext': 'mp4',
  13. 'description': 'Mike Pirnat presents some tips and tricks, standard libraries and third party packages that make programming in Python a richer experience.',
  14. 'title': 'A Few of My Favorite [Python] Things',
  15. },
  16. }, {
  17. 'url': 'http://www.infoq.com/fr/presentations/changez-avis-sur-javascript',
  18. 'only_matching': True,
  19. }]
  20. def _real_extract(self, url):
  21. video_id = self._match_id(url)
  22. webpage = self._download_webpage(url, video_id)
  23. video_title = self._html_search_regex(r'<title>(.*?)</title>', webpage, 'title')
  24. video_description = self._html_search_meta('description', webpage, 'description')
  25. # The server URL is hardcoded
  26. video_url = 'rtmpe://video.infoq.com/cfx/st/'
  27. # Extract video URL
  28. encoded_id = self._search_regex(
  29. r"jsclassref\s*=\s*'([^']*)'", webpage, 'encoded id')
  30. real_id = compat_urllib_parse_unquote(base64.b64decode(encoded_id.encode('ascii')).decode('utf-8'))
  31. playpath = 'mp4:' + real_id
  32. video_filename = playpath.split('/')[-1]
  33. video_id, extension = video_filename.split('.')
  34. http_video_url = self._search_regex(r'P\.s\s*=\s*\'([^\']+)\'', webpage, 'video URL')
  35. policy = self._search_regex(r'InfoQConstants.scp\s*=\s*\'([^\']+)\'', webpage, 'policy')
  36. signature = self._search_regex(r'InfoQConstants.scs\s*=\s*\'([^\']+)\'', webpage, 'signature')
  37. key_pair_id = self._search_regex(r'InfoQConstants.sck\s*=\s*\'([^\']+)\'', webpage, 'key-pair-id')
  38. formats = [{
  39. 'format_id': 'rtmp',
  40. 'url': video_url,
  41. 'ext': extension,
  42. 'play_path': playpath,
  43. }, {
  44. 'format_id': 'http',
  45. 'url': http_video_url,
  46. 'http_headers': {
  47. 'Cookie': 'CloudFront-Policy=%s; CloudFront-Signature=%s; CloudFront-Key-Pair-Id=%s' % (
  48. policy, signature, key_pair_id),
  49. },
  50. }]
  51. self._sort_formats(formats)
  52. return {
  53. 'id': video_id,
  54. 'title': video_title,
  55. 'description': video_description,
  56. 'formats': formats,
  57. }