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.

150 lines
5.2 KiB

  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import base64
  4. from .common import InfoExtractor
  5. from ..compat import compat_urllib_parse_unquote
  6. from ..utils import (
  7. ExtractorError,
  8. parse_iso8601,
  9. parse_duration,
  10. )
  11. class XuiteIE(InfoExtractor):
  12. _REGEX_BASE64 = r'(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?'
  13. _VALID_URL = r'https?://vlog\.xuite\.net/(?:play|embed)/(?P<id>%s)' % _REGEX_BASE64
  14. _TESTS = [{
  15. # Audio
  16. 'url': 'http://vlog.xuite.net/play/RGkzc1ZULTM4NjA5MTQuZmx2',
  17. 'md5': '63a42c705772aa53fd4c1a0027f86adf',
  18. 'info_dict': {
  19. 'id': '3860914',
  20. 'ext': 'mp3',
  21. 'title': '孤單南半球-歐德陽',
  22. 'thumbnail': 're:^https?://.*\.jpg$',
  23. 'duration': 247.246,
  24. 'timestamp': 1314932940,
  25. 'upload_date': '20110902',
  26. 'uploader': '阿能',
  27. 'uploader_id': '15973816',
  28. 'categories': ['個人短片'],
  29. },
  30. }, {
  31. # Video with only one format
  32. 'url': 'http://vlog.xuite.net/play/TkRZNjhULTM0NDE2MjkuZmx2',
  33. 'md5': 'c45737fc8ac5dc8ac2f92ecbcecf505e',
  34. 'info_dict': {
  35. 'id': '3441629',
  36. 'ext': 'mp4',
  37. 'title': '孫燕姿 - 眼淚成詩',
  38. 'thumbnail': 're:^https?://.*\.jpg$',
  39. 'duration': 217.399,
  40. 'timestamp': 1299383640,
  41. 'upload_date': '20110306',
  42. 'uploader': 'Valen',
  43. 'uploader_id': '10400126',
  44. 'categories': ['影視娛樂'],
  45. },
  46. }, {
  47. # Video with two formats
  48. 'url': 'http://vlog.xuite.net/play/bWo1N1pLLTIxMzAxMTcwLmZsdg==',
  49. 'md5': '1166e0f461efe55b62e26a2d2a68e6de',
  50. 'info_dict': {
  51. 'id': '21301170',
  52. 'ext': 'mp4',
  53. 'title': '暗殺教室 02',
  54. 'description': '字幕:【極影字幕社】',
  55. 'thumbnail': 're:^https?://.*\.jpg$',
  56. 'duration': 1384.907,
  57. 'timestamp': 1421481240,
  58. 'upload_date': '20150117',
  59. 'uploader': '我只是想認真點',
  60. 'uploader_id': '242127761',
  61. 'categories': ['電玩動漫'],
  62. },
  63. }, {
  64. 'url': 'http://vlog.xuite.net/play/S1dDUjdyLTMyOTc3NjcuZmx2/%E5%AD%AB%E7%87%95%E5%A7%BF-%E7%9C%BC%E6%B7%9A%E6%88%90%E8%A9%A9',
  65. 'only_matching': True,
  66. }]
  67. @staticmethod
  68. def base64_decode_utf8(data):
  69. return base64.b64decode(data.encode('utf-8')).decode('utf-8')
  70. @staticmethod
  71. def base64_encode_utf8(data):
  72. return base64.b64encode(data.encode('utf-8')).decode('utf-8')
  73. def _extract_flv_config(self, media_id):
  74. base64_media_id = self.base64_encode_utf8(media_id)
  75. flv_config = self._download_xml(
  76. 'http://vlog.xuite.net/flash/player?media=%s' % base64_media_id,
  77. 'flv config')
  78. prop_dict = {}
  79. for prop in flv_config.findall('./property'):
  80. prop_id = self.base64_decode_utf8(prop.attrib['id'])
  81. # CDATA may be empty in flv config
  82. if not prop.text:
  83. continue
  84. encoded_content = self.base64_decode_utf8(prop.text)
  85. prop_dict[prop_id] = compat_urllib_parse_unquote(encoded_content)
  86. return prop_dict
  87. def _real_extract(self, url):
  88. video_id = self._match_id(url)
  89. webpage = self._download_webpage(url, video_id)
  90. error_msg = self._search_regex(
  91. r'<div id="error-message-content">([^<]+)',
  92. webpage, 'error message', default=None)
  93. if error_msg:
  94. raise ExtractorError(
  95. '%s returned error: %s' % (self.IE_NAME, error_msg),
  96. expected=True)
  97. video_id = self._html_search_regex(
  98. r'data-mediaid="(\d+)"', webpage, 'media id')
  99. flv_config = self._extract_flv_config(video_id)
  100. FORMATS = {
  101. 'audio': 'mp3',
  102. 'video': 'mp4',
  103. }
  104. formats = []
  105. for format_tag in ('src', 'hq_src'):
  106. video_url = flv_config.get(format_tag)
  107. if not video_url:
  108. continue
  109. format_id = self._search_regex(
  110. r'\bq=(.+?)\b', video_url, 'format id', default=format_tag)
  111. formats.append({
  112. 'url': video_url,
  113. 'ext': FORMATS.get(flv_config['type'], 'mp4'),
  114. 'format_id': format_id,
  115. 'height': int(format_id) if format_id.isnumeric() else None,
  116. })
  117. self._sort_formats(formats)
  118. timestamp = flv_config.get('publish_datetime')
  119. if timestamp:
  120. timestamp = parse_iso8601(timestamp + ' +0800', ' ')
  121. category = flv_config.get('category')
  122. categories = [category] if category else []
  123. return {
  124. 'id': video_id,
  125. 'title': flv_config['title'],
  126. 'description': flv_config.get('description'),
  127. 'thumbnail': flv_config.get('thumb'),
  128. 'timestamp': timestamp,
  129. 'uploader': flv_config.get('author_name'),
  130. 'uploader_id': flv_config.get('author_id'),
  131. 'duration': parse_duration(flv_config.get('duration')),
  132. 'categories': categories,
  133. 'formats': formats,
  134. }