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.

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