xuite.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import base64
  4. from .common import InfoExtractor
  5. from ..compat import (
  6. compat_urlparse,
  7. compat_urllib_parse_unquote,
  8. compat_parse_qs
  9. )
  10. from ..utils import (
  11. ExtractorError,
  12. parse_iso8601,
  13. parse_duration
  14. )
  15. # ref: http://stackoverflow.com/questions/475074/regex-to-parse-or-validate-base64-data
  16. REGEX_BASE64 = r'(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?'
  17. CST_ZONE = +8 # China Standard Time
  18. class XuiteIE(InfoExtractor):
  19. _VALID_URL = r'http://vlog.xuite.net/play/(?P<id>%s)(/.*)?' % REGEX_BASE64
  20. _TESTS = [{
  21. # Audio
  22. 'url': 'http://vlog.xuite.net/play/RGkzc1ZULTM4NjA5MTQuZmx2',
  23. 'md5': '63a42c705772aa53fd4c1a0027f86adf',
  24. 'info_dict': {
  25. 'id': 'RGkzc1ZULTM4NjA5MTQuZmx2',
  26. 'ext': 'mp3',
  27. 'upload_date': '20110902',
  28. 'uploader_id': '15973816',
  29. 'uploader': '阿能',
  30. 'timestamp': 1314932940,
  31. 'title': '孤單南半球-歐德陽',
  32. 'thumbnail': 're:^https?://.*\.jpg$',
  33. 'categories': ['個人短片'],
  34. 'duration': 247.246
  35. }
  36. }, {
  37. # Audio with alternative form of url
  38. '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',
  39. 'md5': 'c91645f324de53d82ebb80930e1a73d2',
  40. 'info_dict': {
  41. 'id': 'S1dDUjdyLTMyOTc3NjcuZmx2',
  42. 'ext': 'mp3',
  43. 'upload_date': '20101226',
  44. 'uploader_id': '10102699',
  45. 'uploader': '蠍',
  46. 'timestamp': 1293367080,
  47. 'title': '孫燕姿-眼淚成詩',
  48. 'thumbnail': 're:^https?://.*\.jpg$',
  49. 'categories': ['個人短片'],
  50. 'duration': 223.19
  51. }
  52. }, {
  53. # Video with only one format
  54. 'url': 'http://vlog.xuite.net/play/TkRZNjhULTM0NDE2MjkuZmx2',
  55. 'md5': 'c45737fc8ac5dc8ac2f92ecbcecf505e',
  56. 'info_dict': {
  57. 'id': 'TkRZNjhULTM0NDE2MjkuZmx2',
  58. 'ext': 'mp4',
  59. 'upload_date': '20110306',
  60. 'uploader_id': '10400126',
  61. 'uploader': 'Valen',
  62. 'timestamp': 1299383640,
  63. 'title': '孫燕姿 - 眼淚成詩',
  64. 'thumbnail': 're:^https?://.*\.jpg$',
  65. 'categories': ['影視娛樂'],
  66. 'duration': 217.399
  67. }
  68. }, {
  69. # Video with two formats
  70. 'url': 'http://vlog.xuite.net/play/bWo1N1pLLTIxMzAxMTcwLmZsdg==',
  71. 'md5': '1166e0f461efe55b62e26a2d2a68e6de',
  72. 'info_dict': {
  73. 'id': 'bWo1N1pLLTIxMzAxMTcwLmZsdg==',
  74. 'ext': 'mp4',
  75. 'upload_date': '20150117',
  76. 'uploader_id': '242127761',
  77. 'uploader': '我只是想認真點',
  78. 'timestamp': 1421481240,
  79. 'title': '暗殺教室 02',
  80. 'thumbnail': 're:^https?://.*\.jpg$',
  81. 'categories': ['電玩動漫'],
  82. 'duration': 1384.907
  83. }
  84. }]
  85. def _flv_config(self, media_id):
  86. base64_media_id = base64.b64encode(media_id.encode('utf-8')).decode('utf-8')
  87. flv_config_url = 'http://vlog.xuite.net/flash/player?media=' + base64_media_id
  88. flv_config = self._download_xml(flv_config_url, 'flv config')
  89. prop_dict = {}
  90. for prop in flv_config.findall('./property'):
  91. prop_id = base64.b64decode(prop.attrib['id']).decode('utf-8')
  92. if not prop.text:
  93. continue # CDATA may be empty in flv config
  94. encoded_content = base64.b64decode(prop.text).decode('utf-8')
  95. prop_dict[prop_id] = compat_urllib_parse_unquote(encoded_content)
  96. return prop_dict
  97. def _type_string(self, media_url):
  98. query_string = compat_urlparse.urlparse(media_url).query
  99. type_string = compat_parse_qs(query_string)['q'][0]
  100. return type_string
  101. def _guess_ext(self, media_url):
  102. type_string = self._type_string(media_url)
  103. if type_string == 'mp3':
  104. return 'mp3'
  105. elif type_string == '360' or type_string == '720':
  106. return 'mp4'
  107. else:
  108. raise ExtractorError('Unknown type string %s' % type_string)
  109. def _real_extract(self, url):
  110. video_id = self._match_id(url)
  111. page = self._download_webpage(url, video_id)
  112. media_id = self._html_search_regex(r'data-mediaid="(\d+)"', page, 'media id')
  113. flv_config = self._flv_config(media_id)
  114. timestamp_local = parse_iso8601(flv_config['publish_datetime'], ' ')
  115. timestamp_gmt = timestamp_local - CST_ZONE * 3600
  116. ret_attrs = {
  117. 'id': video_id,
  118. 'title': flv_config['title'],
  119. 'thumbnail': flv_config['thumb'],
  120. 'uploader': flv_config['author_name'],
  121. 'timestamp': timestamp_gmt,
  122. 'uploader_id': flv_config['author_id'],
  123. 'duration': parse_duration(flv_config['duration']),
  124. 'categories': [flv_config['category']]
  125. }
  126. if 'hq_src' in flv_config:
  127. urls = [flv_config['src'], flv_config['hq_src']]
  128. ret_attrs['formats'] = []
  129. for url in urls:
  130. ret_attrs['formats'].append({
  131. 'url': url,
  132. 'ext': self._guess_ext(url),
  133. 'format_id': self._type_string(url),
  134. 'height': int(self._type_string(url))
  135. })
  136. else:
  137. ret_attrs['url'] = flv_config['src']
  138. ret_attrs['ext'] = self._guess_ext(flv_config['src'])
  139. return ret_attrs