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.

136 lines
4.9 KiB

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