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.

152 lines
5.4 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/WUxxR2xCLTI1OTI1MDk5LmZsdg==',
  34. 'md5': '21f7b39c009b5a4615b4463df6eb7a46',
  35. 'info_dict': {
  36. 'id': '25925099',
  37. 'ext': 'mp4',
  38. 'title': 'BigBuckBunny_320x180',
  39. 'thumbnail': 're:^https?://.*\.jpg$',
  40. 'duration': 596.458,
  41. 'timestamp': 1454242500,
  42. 'upload_date': '20160131',
  43. 'uploader': 'yan12125',
  44. 'uploader_id': '12158353',
  45. 'categories': ['個人短片'],
  46. 'description': 'http://download.blender.org/peach/bigbuckbunny_movies/BigBuckBunny_320x180.mp4',
  47. },
  48. }, {
  49. # Video with two formats
  50. 'url': 'http://vlog.xuite.net/play/bWo1N1pLLTIxMzAxMTcwLmZsdg==',
  51. 'md5': '1166e0f461efe55b62e26a2d2a68e6de',
  52. 'info_dict': {
  53. 'id': '21301170',
  54. 'ext': 'mp4',
  55. 'title': '暗殺教室 02',
  56. 'description': '字幕:【極影字幕社】',
  57. 'thumbnail': 're:^https?://.*\.jpg$',
  58. 'duration': 1384.907,
  59. 'timestamp': 1421481240,
  60. 'upload_date': '20150117',
  61. 'uploader': '我只是想認真點',
  62. 'uploader_id': '242127761',
  63. 'categories': ['電玩動漫'],
  64. },
  65. }, {
  66. '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',
  67. 'only_matching': True,
  68. }]
  69. @staticmethod
  70. def base64_decode_utf8(data):
  71. return base64.b64decode(data.encode('utf-8')).decode('utf-8')
  72. @staticmethod
  73. def base64_encode_utf8(data):
  74. return base64.b64encode(data.encode('utf-8')).decode('utf-8')
  75. def _extract_flv_config(self, media_id):
  76. base64_media_id = self.base64_encode_utf8(media_id)
  77. flv_config = self._download_xml(
  78. 'http://vlog.xuite.net/flash/player?media=%s' % base64_media_id,
  79. 'flv config')
  80. prop_dict = {}
  81. for prop in flv_config.findall('./property'):
  82. prop_id = self.base64_decode_utf8(prop.attrib['id'])
  83. # CDATA may be empty in flv config
  84. if not prop.text:
  85. continue
  86. encoded_content = self.base64_decode_utf8(prop.text)
  87. prop_dict[prop_id] = compat_urllib_parse_unquote(encoded_content)
  88. return prop_dict
  89. def _real_extract(self, url):
  90. video_id = self._match_id(url)
  91. webpage = self._download_webpage(url, video_id)
  92. error_msg = self._search_regex(
  93. r'<div id="error-message-content">([^<]+)',
  94. webpage, 'error message', default=None)
  95. if error_msg:
  96. raise ExtractorError(
  97. '%s returned error: %s' % (self.IE_NAME, error_msg),
  98. expected=True)
  99. video_id = self._html_search_regex(
  100. r'data-mediaid="(\d+)"', webpage, 'media id')
  101. flv_config = self._extract_flv_config(video_id)
  102. FORMATS = {
  103. 'audio': 'mp3',
  104. 'video': 'mp4',
  105. }
  106. formats = []
  107. for format_tag in ('src', 'hq_src'):
  108. video_url = flv_config.get(format_tag)
  109. if not video_url:
  110. continue
  111. format_id = self._search_regex(
  112. r'\bq=(.+?)\b', video_url, 'format id', default=format_tag)
  113. formats.append({
  114. 'url': video_url,
  115. 'ext': FORMATS.get(flv_config['type'], 'mp4'),
  116. 'format_id': format_id,
  117. 'height': int(format_id) if format_id.isnumeric() else None,
  118. })
  119. self._sort_formats(formats)
  120. timestamp = flv_config.get('publish_datetime')
  121. if timestamp:
  122. timestamp = parse_iso8601(timestamp + ' +0800', ' ')
  123. category = flv_config.get('category')
  124. categories = [category] if category else []
  125. return {
  126. 'id': video_id,
  127. 'title': flv_config['title'],
  128. 'description': flv_config.get('description'),
  129. 'thumbnail': flv_config.get('thumb'),
  130. 'timestamp': timestamp,
  131. 'uploader': flv_config.get('author_name'),
  132. 'uploader_id': flv_config.get('author_id'),
  133. 'duration': parse_duration(flv_config.get('duration')),
  134. 'categories': categories,
  135. 'formats': formats,
  136. }