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.

148 lines
5.2 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import hashlib
  4. import time
  5. from .common import InfoExtractor
  6. from ..utils import (ExtractorError, unescapeHTML)
  7. from ..compat import (compat_str, compat_basestring)
  8. class DouyuTVIE(InfoExtractor):
  9. IE_DESC = '斗鱼'
  10. _VALID_URL = r'https?://(?:www\.)?douyu(?:tv)?\.com/(?P<id>[A-Za-z0-9]+)'
  11. _TESTS = [{
  12. 'url': 'http://www.douyutv.com/iseven',
  13. 'info_dict': {
  14. 'id': '17732',
  15. 'display_id': 'iseven',
  16. 'ext': 'flv',
  17. 'title': 're:^清晨醒脑!T-ara根本停不下来! [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
  18. 'description': 're:.*m7show@163\.com.*',
  19. 'thumbnail': 're:^https?://.*\.jpg$',
  20. 'uploader': '7师傅',
  21. 'uploader_id': '431925',
  22. 'is_live': True,
  23. },
  24. 'params': {
  25. 'skip_download': True,
  26. },
  27. }, {
  28. 'url': 'http://www.douyutv.com/85982',
  29. 'info_dict': {
  30. 'id': '85982',
  31. 'display_id': '85982',
  32. 'ext': 'flv',
  33. 'title': 're:^小漠从零单排记!——CSOL2躲猫猫 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
  34. 'description': 'md5:746a2f7a253966a06755a912f0acc0d2',
  35. 'thumbnail': 're:^https?://.*\.jpg$',
  36. 'uploader': 'douyu小漠',
  37. 'uploader_id': '3769985',
  38. 'is_live': True,
  39. },
  40. 'params': {
  41. 'skip_download': True,
  42. },
  43. 'skip': 'Room not found',
  44. }, {
  45. 'url': 'http://www.douyutv.com/17732',
  46. 'info_dict': {
  47. 'id': '17732',
  48. 'display_id': '17732',
  49. 'ext': 'flv',
  50. 'title': 're:^清晨醒脑!T-ara根本停不下来! [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
  51. 'description': 're:.*m7show@163\.com.*',
  52. 'thumbnail': 're:^https?://.*\.jpg$',
  53. 'uploader': '7师傅',
  54. 'uploader_id': '431925',
  55. 'is_live': True,
  56. },
  57. 'params': {
  58. 'skip_download': True,
  59. },
  60. }, {
  61. 'url': 'http://www.douyu.com/xiaocang',
  62. 'only_matching': True,
  63. }]
  64. def _real_extract(self, url):
  65. video_id = self._match_id(url)
  66. if video_id.isdigit():
  67. room_id = video_id
  68. else:
  69. page = self._download_webpage(url, video_id)
  70. room_id = self._html_search_regex(
  71. r'"room_id"\s*:\s*(\d+),', page, 'room id')
  72. config = None
  73. # Douyu API sometimes returns error "Unable to load the requested class: eticket_redis_cache"
  74. # Retry with different parameters - same parameters cause same errors
  75. for i in range(5):
  76. prefix = 'room/%s?aid=android&client_sys=android&time=%d' % (
  77. room_id, int(time.time()))
  78. auth = hashlib.md5((prefix + '1231').encode('ascii')).hexdigest()
  79. config_page = self._download_webpage(
  80. 'http://www.douyutv.com/api/v1/%s&auth=%s' % (prefix, auth),
  81. video_id)
  82. try:
  83. config = self._parse_json(config_page, video_id, fatal=False)
  84. except ExtractorError:
  85. # Wait some time before retrying to get a different time() value
  86. self._sleep(1, video_id, msg_template='%(video_id)s: Error occurs. '
  87. 'Waiting for %(timeout)s seconds before retrying')
  88. continue
  89. else:
  90. break
  91. if config is None:
  92. raise ExtractorError('Unable to fetch API result')
  93. data = config['data']
  94. error_code = config.get('error', 0)
  95. if error_code is not 0:
  96. error_desc = 'Server reported error %i' % error_code
  97. if isinstance(data, (compat_str, compat_basestring)):
  98. error_desc += ': ' + data
  99. raise ExtractorError(error_desc, expected=True)
  100. show_status = data.get('show_status')
  101. # 1 = live, 2 = offline
  102. if show_status == '2':
  103. raise ExtractorError(
  104. 'Live stream is offline', expected=True)
  105. base_url = data['rtmp_url']
  106. live_path = data['rtmp_live']
  107. title = self._live_title(unescapeHTML(data['room_name']))
  108. description = data.get('show_details')
  109. thumbnail = data.get('room_src')
  110. uploader = data.get('nickname')
  111. uploader_id = data.get('owner_uid')
  112. multi_formats = data.get('rtmp_multi_bitrate')
  113. if not isinstance(multi_formats, dict):
  114. multi_formats = {}
  115. multi_formats['live'] = live_path
  116. formats = [{
  117. 'url': '%s/%s' % (base_url, format_path),
  118. 'format_id': format_id,
  119. 'preference': 1 if format_id == 'live' else 0,
  120. } for format_id, format_path in multi_formats.items()]
  121. self._sort_formats(formats)
  122. return {
  123. 'id': room_id,
  124. 'display_id': video_id,
  125. 'title': title,
  126. 'description': description,
  127. 'thumbnail': thumbnail,
  128. 'uploader': uploader,
  129. 'uploader_id': uploader_id,
  130. 'formats': formats,
  131. 'is_live': True,
  132. }