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.

142 lines
5.0 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. def _extract_flv_config(self, media_id):
  68. base64_media_id = base64.b64encode(media_id.encode('utf-8')).decode('utf-8')
  69. flv_config = self._download_xml(
  70. 'http://vlog.xuite.net/flash/player?media=%s' % base64_media_id,
  71. 'flv config')
  72. prop_dict = {}
  73. for prop in flv_config.findall('./property'):
  74. prop_id = base64.b64decode(prop.attrib['id']).decode('utf-8')
  75. # CDATA may be empty in flv config
  76. if not prop.text:
  77. continue
  78. encoded_content = base64.b64decode(prop.text).decode('utf-8')
  79. prop_dict[prop_id] = compat_urllib_parse_unquote(encoded_content)
  80. return prop_dict
  81. def _real_extract(self, url):
  82. video_id = self._match_id(url)
  83. webpage = self._download_webpage(url, video_id)
  84. error_msg = self._search_regex(
  85. r'<div id="error-message-content">([^<]+)',
  86. webpage, 'error message', default=None)
  87. if error_msg:
  88. raise ExtractorError(
  89. '%s returned error: %s' % (self.IE_NAME, error_msg),
  90. expected=True)
  91. video_id = self._html_search_regex(
  92. r'data-mediaid="(\d+)"', webpage, 'media id')
  93. flv_config = self._extract_flv_config(video_id)
  94. FORMATS = {
  95. 'audio': 'mp3',
  96. 'video': 'mp4',
  97. }
  98. formats = []
  99. for format_tag in ('src', 'hq_src'):
  100. video_url = flv_config.get(format_tag)
  101. if not video_url:
  102. continue
  103. format_id = self._search_regex(
  104. r'\bq=(.+?)\b', video_url, 'format id', default=format_tag)
  105. formats.append({
  106. 'url': video_url,
  107. 'ext': FORMATS.get(flv_config['type'], 'mp4'),
  108. 'format_id': format_id,
  109. 'height': int(format_id) if format_id.isnumeric() else None,
  110. })
  111. self._sort_formats(formats)
  112. timestamp = flv_config.get('publish_datetime')
  113. if timestamp:
  114. timestamp = parse_iso8601(timestamp + ' +0800', ' ')
  115. category = flv_config.get('category')
  116. categories = [category] if category else []
  117. return {
  118. 'id': video_id,
  119. 'title': flv_config['title'],
  120. 'description': flv_config.get('description'),
  121. 'thumbnail': flv_config.get('thumb'),
  122. 'timestamp': timestamp,
  123. 'uploader': flv_config.get('author_name'),
  124. 'uploader_id': flv_config.get('author_id'),
  125. 'duration': parse_duration(flv_config.get('duration')),
  126. 'categories': categories,
  127. 'formats': formats,
  128. }