xuite.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. from .common import InfoExtractor
  2. from ..utils import (
  3. ExtractorError,
  4. float_or_none,
  5. get_element_by_attribute,
  6. parse_iso8601,
  7. remove_end,
  8. )
  9. class XuiteIE(InfoExtractor):
  10. IE_DESC = '隨意窩Xuite影音'
  11. _REGEX_BASE64 = r'(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?'
  12. _VALID_URL = r'https?://vlog\.xuite\.net/(?:play|embed)/(?P<id>%s)' % _REGEX_BASE64
  13. _TESTS = [{
  14. # Audio
  15. 'url': 'http://vlog.xuite.net/play/RGkzc1ZULTM4NjA5MTQuZmx2',
  16. 'md5': 'e79284c87b371424885448d11f6398c8',
  17. 'info_dict': {
  18. 'id': '3860914',
  19. 'ext': 'mp3',
  20. 'title': '孤單南半球-歐德陽',
  21. 'description': '孤單南半球-歐德陽',
  22. 'thumbnail': r'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/WUxxR2xCLTI1OTI1MDk5LmZsdg==',
  33. 'md5': '21f7b39c009b5a4615b4463df6eb7a46',
  34. 'info_dict': {
  35. 'id': '25925099',
  36. 'ext': 'mp4',
  37. 'title': 'BigBuckBunny_320x180',
  38. 'thumbnail': r're:^https?://.*\.jpg$',
  39. 'duration': 596.458,
  40. 'timestamp': 1454242500,
  41. 'upload_date': '20160131',
  42. 'uploader': '屁姥',
  43. 'uploader_id': '12158353',
  44. 'categories': ['個人短片'],
  45. 'description': 'http://download.blender.org/peach/bigbuckbunny_movies/BigBuckBunny_320x180.mp4',
  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': r're:^https?://.*\.jpg$',
  57. 'duration': 1384.907,
  58. 'timestamp': 1421481240,
  59. 'upload_date': '20150117',
  60. 'uploader': '我只是想認真點',
  61. 'uploader_id': '242127761',
  62. 'categories': ['電玩動漫'],
  63. },
  64. 'skip': 'Video removed',
  65. }, {
  66. # Video with encoded media id
  67. # from http://forgetfulbc.blogspot.com/2016/06/date.html
  68. 'url': 'http://vlog.xuite.net/embed/cE1xbENoLTI3NDQ3MzM2LmZsdg==?ar=0&as=0',
  69. 'info_dict': {
  70. 'id': '27447336',
  71. 'ext': 'mp4',
  72. 'title': '男女平權只是口號?專家解釋約會時男生是否該幫女生付錢 (中字)',
  73. 'description': 'md5:1223810fa123b179083a3aed53574706',
  74. 'timestamp': 1466160960,
  75. 'upload_date': '20160617',
  76. 'uploader': 'B.C. & Lowy',
  77. 'uploader_id': '232279340',
  78. },
  79. }, {
  80. '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',
  81. 'only_matching': True,
  82. }]
  83. def _real_extract(self, url):
  84. # /play/ URLs provide embedded video URL and more metadata
  85. url = url.replace('/embed/', '/play/')
  86. video_id = self._match_id(url)
  87. webpage = self._download_webpage(url, video_id)
  88. error_msg = self._search_regex(
  89. r'<div id="error-message-content">([^<]+)',
  90. webpage, 'error message', default=None)
  91. if error_msg:
  92. raise ExtractorError(
  93. '%s returned error: %s' % (self.IE_NAME, error_msg),
  94. expected=True)
  95. media_info = self._parse_json(self._search_regex(
  96. r'var\s+mediaInfo\s*=\s*({.*});', webpage, 'media info'), video_id)
  97. video_id = media_info['MEDIA_ID']
  98. formats = []
  99. for key in ('html5Url', 'html5HQUrl'):
  100. video_url = media_info.get(key)
  101. if not video_url:
  102. continue
  103. format_id = self._search_regex(
  104. r'\bq=(.+?)\b', video_url, 'format id', default=None)
  105. formats.append({
  106. 'url': video_url,
  107. 'ext': 'mp4' if format_id.isnumeric() else format_id,
  108. 'format_id': format_id,
  109. 'height': int(format_id) if format_id.isnumeric() else None,
  110. })
  111. timestamp = media_info.get('PUBLISH_DATETIME')
  112. if timestamp:
  113. timestamp = parse_iso8601(timestamp + ' +0800', ' ')
  114. category = media_info.get('catName')
  115. categories = [category] if category else []
  116. uploader = media_info.get('NICKNAME')
  117. uploader_url = None
  118. author_div = get_element_by_attribute('itemprop', 'author', webpage)
  119. if author_div:
  120. uploader = uploader or self._html_search_meta('name', author_div)
  121. uploader_url = self._html_search_regex(
  122. r'<link[^>]+itemprop="url"[^>]+href="([^"]+)"', author_div,
  123. 'uploader URL', fatal=False)
  124. return {
  125. 'id': video_id,
  126. 'title': media_info['TITLE'],
  127. 'description': remove_end(media_info.get('metaDesc'), ' (Xuite 影音)'),
  128. 'thumbnail': media_info.get('ogImageUrl'),
  129. 'timestamp': timestamp,
  130. 'uploader': uploader,
  131. 'uploader_id': media_info.get('MEMBER_ID'),
  132. 'uploader_url': uploader_url,
  133. 'duration': float_or_none(media_info.get('MEDIA_DURATION'), 1000000),
  134. 'categories': categories,
  135. 'formats': formats,
  136. }