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.

115 lines
4.3 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import base64
  4. from .common import InfoExtractor
  5. from ..compat import (
  6. compat_urllib_parse_unquote,
  7. compat_parse_qs,
  8. )
  9. from ..utils import determine_ext
  10. class InfoQIE(InfoExtractor):
  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. def _extract_bokecc_videos(self, webpage, video_id):
  35. # TODO: bokecc.com is a Chinese video cloud platform
  36. # It should have an independent extractor but I don't have other
  37. # examples using bokecc
  38. player_params_str = self._html_search_regex(
  39. r'<script[^>]+src="http://p\.bokecc\.com/player\?([^"]+)',
  40. webpage, 'player params', default=None)
  41. player_params = compat_parse_qs(player_params_str)
  42. info_xml = self._download_xml(
  43. 'http://p.bokecc.com/servlet/playinfo?uid=%s&vid=%s&m=1' % (
  44. player_params['siteid'][0], player_params['vid'][0]), video_id)
  45. return [{
  46. 'format_id': 'bokecc',
  47. 'url': quality.find('./copy').attrib['playurl'],
  48. 'preference': int(quality.attrib['value']),
  49. } for quality in info_xml.findall('./video/quality')]
  50. def _extract_rtmp_videos(self, webpage):
  51. # The server URL is hardcoded
  52. video_url = 'rtmpe://video.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(base64.b64decode(encoded_id.encode('ascii')).decode('utf-8'))
  57. playpath = 'mp4:' + real_id
  58. return [{
  59. 'format_id': 'rtmp',
  60. 'url': video_url,
  61. 'ext': determine_ext(playpath),
  62. 'play_path': playpath,
  63. }]
  64. def _extract_http_videos(self, webpage):
  65. http_video_url = self._search_regex(r'P\.s\s*=\s*\'([^\']+)\'', webpage, 'video URL')
  66. policy = self._search_regex(r'InfoQConstants.scp\s*=\s*\'([^\']+)\'', webpage, 'policy')
  67. signature = self._search_regex(r'InfoQConstants.scs\s*=\s*\'([^\']+)\'', webpage, 'signature')
  68. key_pair_id = self._search_regex(r'InfoQConstants.sck\s*=\s*\'([^\']+)\'', webpage, 'key-pair-id')
  69. return [{
  70. 'format_id': 'http',
  71. 'url': http_video_url,
  72. 'http_headers': {
  73. 'Cookie': 'CloudFront-Policy=%s; CloudFront-Signature=%s; CloudFront-Key-Pair-Id=%s' % (
  74. policy, signature, key_pair_id),
  75. },
  76. }]
  77. def _real_extract(self, url):
  78. video_id = self._match_id(url)
  79. webpage = self._download_webpage(url, video_id)
  80. video_title = self._html_search_regex(r'<title>(.*?)</title>', webpage, 'title')
  81. video_description = self._html_search_meta('description', webpage, 'description')
  82. if '/cn/' in url:
  83. # for China videos, HTTP video URL exists but always fails with 403
  84. formats = self._extract_bokecc_videos(webpage, video_id)
  85. else:
  86. formats = self._extract_rtmp_videos(webpage) + self._extract_http_videos(webpage)
  87. self._sort_formats(formats)
  88. return {
  89. 'id': video_id,
  90. 'title': video_title,
  91. 'description': video_description,
  92. 'formats': formats,
  93. }