You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
4.9 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import base64
  4. from ..compat import (
  5. compat_urllib_parse_unquote,
  6. compat_urlparse,
  7. )
  8. from ..utils import determine_ext
  9. from .bokecc import BokeCCBaseIE
  10. class InfoQIE(BokeCCBaseIE):
  11. _VALID_URL = r'https?://(?:www\.)?infoq\.com/(?:[^/]+/)+(?P<id>[^/]+)'
  12. _TESTS = [{
  13. 'url': 'http://www.infoq.com/presentations/A-Few-of-My-Favorite-Python-Things',
  14. 'md5': 'b5ca0e0a8c1fed93b0e65e48e462f9a2',
  15. 'info_dict': {
  16. 'id': 'A-Few-of-My-Favorite-Python-Things',
  17. 'ext': 'mp4',
  18. 'description': 'Mike Pirnat presents some tips and tricks, standard libraries and third party packages that make programming in Python a richer experience.',
  19. 'title': 'A Few of My Favorite [Python] Things',
  20. },
  21. }, {
  22. 'url': 'http://www.infoq.com/fr/presentations/changez-avis-sur-javascript',
  23. 'only_matching': True,
  24. }, {
  25. 'url': 'http://www.infoq.com/cn/presentations/openstack-continued-delivery',
  26. 'md5': '4918d0cca1497f2244572caf626687ef',
  27. 'info_dict': {
  28. 'id': 'openstack-continued-delivery',
  29. 'title': 'OpenStack持续交付之路',
  30. 'ext': 'flv',
  31. 'description': 'md5:308d981fb28fa42f49f9568322c683ff',
  32. },
  33. }, {
  34. 'url': 'https://www.infoq.com/presentations/Simple-Made-Easy',
  35. 'md5': '0e34642d4d9ef44bf86f66f6399672db',
  36. 'info_dict': {
  37. 'id': 'Simple-Made-Easy',
  38. 'title': 'Simple Made Easy',
  39. 'ext': 'mp3',
  40. 'description': 'md5:3e0e213a8bbd074796ef89ea35ada25b',
  41. },
  42. 'params': {
  43. 'format': 'bestaudio',
  44. },
  45. }]
  46. def _extract_rtmp_video(self, webpage):
  47. # The server URL is hardcoded
  48. video_url = 'rtmpe://video.infoq.com/cfx/st/'
  49. # Extract video URL
  50. encoded_id = self._search_regex(
  51. r"jsclassref\s*=\s*'([^']*)'", webpage, 'encoded id', default=None)
  52. real_id = compat_urllib_parse_unquote(base64.b64decode(encoded_id.encode('ascii')).decode('utf-8'))
  53. playpath = 'mp4:' + real_id
  54. return [{
  55. 'format_id': 'rtmp_video',
  56. 'url': video_url,
  57. 'ext': determine_ext(playpath),
  58. 'play_path': playpath,
  59. }]
  60. def _extract_cookies(self, webpage):
  61. policy = self._search_regex(r'InfoQConstants.scp\s*=\s*\'([^\']+)\'', webpage, 'policy')
  62. signature = self._search_regex(r'InfoQConstants.scs\s*=\s*\'([^\']+)\'', webpage, 'signature')
  63. key_pair_id = self._search_regex(r'InfoQConstants.sck\s*=\s*\'([^\']+)\'', webpage, 'key-pair-id')
  64. return 'CloudFront-Policy=%s; CloudFront-Signature=%s; CloudFront-Key-Pair-Id=%s' % (
  65. policy, signature, key_pair_id)
  66. def _extract_http_video(self, webpage):
  67. http_video_url = self._search_regex(r'P\.s\s*=\s*\'([^\']+)\'', webpage, 'video URL')
  68. return [{
  69. 'format_id': 'http_video',
  70. 'url': http_video_url,
  71. 'http_headers': {
  72. 'Cookie': self._extract_cookies(webpage)
  73. },
  74. }]
  75. def _extract_http_audio(self, webpage, video_id):
  76. fields = self._hidden_inputs(webpage)
  77. http_audio_url = fields.get('filename')
  78. if not http_audio_url:
  79. return []
  80. cookies_header = {'Cookie': self._extract_cookies(webpage)}
  81. # base URL is found in the Location header in the response returned by
  82. # GET https://www.infoq.com/mp3download.action?filename=... when logged in.
  83. http_audio_url = compat_urlparse.urljoin('http://res.infoq.com/downloads/mp3downloads/', http_audio_url)
  84. # audio file seem to be missing some times even if there is a download link
  85. # so probe URL to make sure
  86. if not self._is_valid_url(http_audio_url, video_id, headers=cookies_header):
  87. return []
  88. return [{
  89. 'format_id': 'http_audio',
  90. 'url': http_audio_url,
  91. 'vcodec': 'none',
  92. 'http_headers': cookies_header,
  93. }]
  94. def _real_extract(self, url):
  95. video_id = self._match_id(url)
  96. webpage = self._download_webpage(url, video_id)
  97. video_title = self._html_search_regex(r'<title>(.*?)</title>', webpage, 'title')
  98. video_description = self._html_search_meta('description', webpage, 'description')
  99. if '/cn/' in url:
  100. # for China videos, HTTP video URL exists but always fails with 403
  101. formats = self._extract_bokecc_formats(webpage, video_id)
  102. else:
  103. formats = (
  104. self._extract_rtmp_video(webpage) +
  105. self._extract_http_video(webpage) +
  106. self._extract_http_audio(webpage, video_id))
  107. self._sort_formats(formats)
  108. return {
  109. 'id': video_id,
  110. 'title': video_title,
  111. 'description': video_description,
  112. 'formats': formats,
  113. }