infoq.py 4.9 KB

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