xuite.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. }
  33. }, {
  34. # Audio with alternative form of url
  35. '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',
  36. 'md5': 'c91645f324de53d82ebb80930e1a73d2',
  37. 'info_dict': {
  38. 'id': 'S1dDUjdyLTMyOTc3NjcuZmx2',
  39. 'ext': 'mp3',
  40. 'upload_date': '20101226',
  41. 'uploader_id': '10102699',
  42. 'uploader': '蠍',
  43. 'timestamp': 1293367080,
  44. 'title': '孫燕姿-眼淚成詩',
  45. }
  46. }, {
  47. # Video with only one format
  48. 'url': 'http://vlog.xuite.net/play/TkRZNjhULTM0NDE2MjkuZmx2',
  49. 'md5': 'c45737fc8ac5dc8ac2f92ecbcecf505e',
  50. 'info_dict': {
  51. 'id': 'TkRZNjhULTM0NDE2MjkuZmx2',
  52. 'ext': 'mp4',
  53. 'upload_date': '20110306',
  54. 'uploader_id': '10400126',
  55. 'uploader': 'Valen',
  56. 'timestamp': 1299383640,
  57. 'title': '孫燕姿 - 眼淚成詩',
  58. }
  59. }, {
  60. # Video with two formats
  61. 'url': 'http://vlog.xuite.net/play/bWo1N1pLLTIxMzAxMTcwLmZsdg==',
  62. 'md5': '1166e0f461efe55b62e26a2d2a68e6de',
  63. 'info_dict': {
  64. 'id': 'bWo1N1pLLTIxMzAxMTcwLmZsdg==',
  65. 'ext': 'mp4',
  66. 'upload_date': '20150117',
  67. 'uploader_id': '242127761',
  68. 'uploader': '我只是想認真點',
  69. 'timestamp': 1421481240,
  70. 'title': '暗殺教室 02',
  71. }
  72. }]
  73. def _flv_config(self, media_id):
  74. base64_media_id = base64.b64encode(media_id.encode('utf-8')).decode('utf-8')
  75. flv_config_url = 'http://vlog.xuite.net/flash/player?media=' + base64_media_id
  76. flv_config = self._download_xml(flv_config_url, 'flv config')
  77. prop_dict = {}
  78. for prop in flv_config.findall('./property'):
  79. prop_id = base64.b64decode(prop.attrib['id']).decode('utf-8')
  80. if not prop.text:
  81. continue # CDATA may be empty in flv config
  82. encoded_content = base64.b64decode(prop.text).decode('utf-8')
  83. prop_dict[prop_id] = compat_urllib_parse_unquote(encoded_content)
  84. return prop_dict
  85. def _type_string(self, media_url):
  86. query_string = compat_urlparse.urlparse(media_url).query
  87. type_string = compat_parse_qs(query_string)['q'][0]
  88. return type_string
  89. def _guess_ext(self, media_url):
  90. type_string = self._type_string(media_url)
  91. if type_string == 'mp3':
  92. return 'mp3'
  93. elif type_string == '360' or type_string == '720':
  94. return 'mp4'
  95. else:
  96. raise ExtractorError('Unknown type string %s' % type_string)
  97. def _real_extract(self, url):
  98. video_id = self._match_id(url)
  99. page = self._download_webpage(url, video_id)
  100. media_id = self._html_search_regex(r'data-mediaid="(\d+)"', page, 'media id')
  101. flv_config = self._flv_config(media_id)
  102. timestamp_local = parse_iso8601(flv_config['publish_datetime'], ' ')
  103. timestamp_gmt = timestamp_local - CST_ZONE * 3600
  104. ret_attrs = {
  105. 'id': video_id,
  106. 'title': flv_config['title'],
  107. 'thumbnail': flv_config['thumb'],
  108. 'uploader': flv_config['author_name'],
  109. 'timestamp': timestamp_gmt,
  110. 'uploader_id': flv_config['author_id'],
  111. 'duration': parse_duration(flv_config['duration']),
  112. 'categories': [flv_config['category']]
  113. }
  114. if 'hq_src' in flv_config:
  115. src = flv_config['src']
  116. src_hq = flv_config['hq_src']
  117. ret_attrs['formats'] = [{
  118. 'url': src,
  119. 'ext': self._guess_ext(src),
  120. 'format_id': self._type_string(src)
  121. }, {
  122. 'url': src_hq,
  123. 'ext': self._guess_ext(src_hq),
  124. 'format_id': self._type_string(src_hq)
  125. }]
  126. else:
  127. ret_attrs['url'] = flv_config['src']
  128. ret_attrs['ext'] = self._guess_ext(flv_config['src'])
  129. return ret_attrs