logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

infoq.py (5189B)


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