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.

171 lines
6.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. 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. 'skip': 'Video removed',
  66. }, {
  67. # Video with encoded media id
  68. # from http://forgetfulbc.blogspot.com/2016/06/date.html
  69. 'url': 'http://vlog.xuite.net/embed/cE1xbENoLTI3NDQ3MzM2LmZsdg==?ar=0&as=0',
  70. 'info_dict': {
  71. 'id': 'cE1xbENoLTI3NDQ3MzM2LmZsdg==',
  72. 'ext': 'mp4',
  73. 'title': '男女平權只是口號?專家解釋約會時男生是否該幫女生付錢 (中字)',
  74. 'description': 'md5:f0abdcb69df300f522a5442ef3146f2a',
  75. 'timestamp': 1466160960,
  76. 'upload_date': '20160617',
  77. 'uploader': 'B.C. & Lowy',
  78. 'uploader_id': '232279340',
  79. },
  80. }, {
  81. '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',
  82. 'only_matching': True,
  83. }]
  84. @staticmethod
  85. def base64_decode_utf8(data):
  86. return base64.b64decode(data.encode('utf-8')).decode('utf-8')
  87. @staticmethod
  88. def base64_encode_utf8(data):
  89. return base64.b64encode(data.encode('utf-8')).decode('utf-8')
  90. def _extract_flv_config(self, encoded_media_id):
  91. flv_config = self._download_xml(
  92. 'http://vlog.xuite.net/flash/player?media=%s' % encoded_media_id,
  93. 'flv config')
  94. prop_dict = {}
  95. for prop in flv_config.findall('./property'):
  96. prop_id = self.base64_decode_utf8(prop.attrib['id'])
  97. # CDATA may be empty in flv config
  98. if not prop.text:
  99. continue
  100. encoded_content = self.base64_decode_utf8(prop.text)
  101. prop_dict[prop_id] = compat_urllib_parse_unquote(encoded_content)
  102. return prop_dict
  103. def _real_extract(self, url):
  104. video_id = self._match_id(url)
  105. webpage = self._download_webpage(url, video_id)
  106. error_msg = self._search_regex(
  107. r'<div id="error-message-content">([^<]+)',
  108. webpage, 'error message', default=None)
  109. if error_msg:
  110. raise ExtractorError(
  111. '%s returned error: %s' % (self.IE_NAME, error_msg),
  112. expected=True)
  113. encoded_media_id = self._search_regex(
  114. r'attributes\.name\s*=\s*"([^"]+)"', webpage,
  115. 'encoded media id', default=None)
  116. if encoded_media_id is None:
  117. video_id = self._html_search_regex(
  118. r'data-mediaid="(\d+)"', webpage, 'media id')
  119. encoded_media_id = self.base64_encode_utf8(video_id)
  120. flv_config = self._extract_flv_config(encoded_media_id)
  121. FORMATS = {
  122. 'audio': 'mp3',
  123. 'video': 'mp4',
  124. }
  125. formats = []
  126. for format_tag in ('src', 'hq_src'):
  127. video_url = flv_config.get(format_tag)
  128. if not video_url:
  129. continue
  130. format_id = self._search_regex(
  131. r'\bq=(.+?)\b', video_url, 'format id', default=format_tag)
  132. formats.append({
  133. 'url': video_url,
  134. 'ext': FORMATS.get(flv_config['type'], 'mp4'),
  135. 'format_id': format_id,
  136. 'height': int(format_id) if format_id.isnumeric() else None,
  137. })
  138. self._sort_formats(formats)
  139. timestamp = flv_config.get('publish_datetime')
  140. if timestamp:
  141. timestamp = parse_iso8601(timestamp + ' +0800', ' ')
  142. category = flv_config.get('category')
  143. categories = [category] if category else []
  144. return {
  145. 'id': video_id,
  146. 'title': flv_config['title'],
  147. 'description': flv_config.get('description'),
  148. 'thumbnail': flv_config.get('thumb'),
  149. 'timestamp': timestamp,
  150. 'uploader': flv_config.get('author_name'),
  151. 'uploader_id': flv_config.get('author_id'),
  152. 'duration': parse_duration(flv_config.get('duration')),
  153. 'categories': categories,
  154. 'formats': formats,
  155. }