infoq.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. from ..compat import (
  2. compat_b64decode,
  3. compat_urllib_parse_unquote,
  4. compat_urlparse,
  5. )
  6. from ..utils import (
  7. ExtractorError,
  8. determine_ext,
  9. update_url_query,
  10. traverse_obj,
  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. 'skip': 'Sorry, the page you visited does not exist',
  37. }, {
  38. 'url': 'https://www.infoq.com/presentations/Simple-Made-Easy',
  39. 'md5': '0e34642d4d9ef44bf86f66f6399672db',
  40. 'info_dict': {
  41. 'id': 'Simple-Made-Easy',
  42. 'title': 'Simple Made Easy',
  43. 'ext': 'mp3',
  44. 'description': 'md5:3e0e213a8bbd074796ef89ea35ada25b',
  45. },
  46. 'params': {
  47. 'format': 'bestaudio',
  48. },
  49. }]
  50. def _extract_rtmp_video(self, webpage):
  51. # The server URL is hardcoded
  52. video_url = 'rtmpe://videof.infoq.com/cfx/st/'
  53. # Extract video URL
  54. encoded_id = self._search_regex(
  55. r"jsclassref\s*=\s*'([^']*)'", webpage, 'encoded id', default=None)
  56. real_id = compat_urllib_parse_unquote(compat_b64decode(encoded_id).decode('utf-8'))
  57. playpath = 'mp4:' + real_id
  58. return [{
  59. 'format_id': 'rtmp_video',
  60. 'url': video_url,
  61. 'ext': determine_ext(playpath),
  62. 'play_path': playpath,
  63. }]
  64. def _extract_cf_auth(self, webpage):
  65. policy = self._search_regex(r'InfoQConstants\.scp\s*=\s*\'([^\']+)\'', webpage, 'policy')
  66. signature = self._search_regex(r'InfoQConstants\.scs\s*=\s*\'([^\']+)\'', webpage, 'signature')
  67. key_pair_id = self._search_regex(r'InfoQConstants\.sck\s*=\s*\'([^\']+)\'', webpage, 'key-pair-id')
  68. return {
  69. 'Policy': policy,
  70. 'Signature': signature,
  71. 'Key-Pair-Id': key_pair_id,
  72. }
  73. def _extract_http_video(self, webpage):
  74. http_video_url = self._search_regex(r'P\.s\s*=\s*\'([^\']+)\'', webpage, 'video URL')
  75. http_video_url = update_url_query(http_video_url, self._extract_cf_auth(webpage))
  76. return [{
  77. 'format_id': 'http_video',
  78. 'url': http_video_url,
  79. 'http_headers': {'Referer': 'https://www.infoq.com/'},
  80. }]
  81. def _extract_http_audio(self, webpage, video_id):
  82. try:
  83. http_audio_url = traverse_obj(self._form_hidden_inputs('mp3Form', webpage), 'filename')
  84. except ExtractorError:
  85. http_audio_url = None
  86. if not http_audio_url:
  87. return []
  88. # base URL is found in the Location header in the response returned by
  89. # GET https://www.infoq.com/mp3download.action?filename=... when logged in.
  90. http_audio_url = compat_urlparse.urljoin('http://ress.infoq.com/downloads/mp3downloads/', http_audio_url)
  91. http_audio_url = update_url_query(http_audio_url, self._extract_cf_auth(webpage))
  92. # audio file seem to be missing some times even if there is a download link
  93. # so probe URL to make sure
  94. if not self._is_valid_url(http_audio_url, video_id):
  95. return []
  96. return [{
  97. 'format_id': 'http_audio',
  98. 'url': http_audio_url,
  99. 'vcodec': 'none',
  100. }]
  101. def _real_extract(self, url):
  102. video_id = self._match_id(url)
  103. webpage = self._download_webpage(url, video_id)
  104. video_title = self._html_extract_title(webpage)
  105. video_description = self._html_search_meta('description', webpage, 'description')
  106. if '/cn/' in url:
  107. # for China videos, HTTP video URL exists but always fails with 403
  108. formats = self._extract_bokecc_formats(webpage, video_id)
  109. else:
  110. formats = (
  111. self._extract_rtmp_video(webpage)
  112. + self._extract_http_video(webpage)
  113. + self._extract_http_audio(webpage, video_id))
  114. return {
  115. 'id': video_id,
  116. 'title': video_title,
  117. 'description': video_description,
  118. 'formats': formats,
  119. }