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.

130 lines
4.3 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'http://(?:www\.)?douyutv\.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': 'md5:f34981259a03e980a3c6404190a3ed61',
  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': 'Romm 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': 'md5:f34981259a03e980a3c6404190a3ed61',
  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. def _real_extract(self, url):
  62. video_id = self._match_id(url)
  63. if video_id.isdigit():
  64. room_id = video_id
  65. else:
  66. page = self._download_webpage(url, video_id)
  67. room_id = self._html_search_regex(
  68. r'"room_id"\s*:\s*(\d+),', page, 'room id')
  69. prefix = 'room/%s?aid=android&client_sys=android&time=%d' % (
  70. room_id, int(time.time()))
  71. auth = hashlib.md5((prefix + '1231').encode('ascii')).hexdigest()
  72. config = self._download_json(
  73. 'http://www.douyutv.com/api/v1/%s&auth=%s' % (prefix, auth),
  74. video_id)
  75. data = config['data']
  76. error_code = config.get('error', 0)
  77. if error_code is not 0:
  78. error_desc = 'Server reported error %i' % error_code
  79. if isinstance(data, (compat_str, compat_basestring)):
  80. error_desc += ': ' + data
  81. raise ExtractorError(error_desc, expected=True)
  82. show_status = data.get('show_status')
  83. # 1 = live, 2 = offline
  84. if show_status == '2':
  85. raise ExtractorError(
  86. 'Live stream is offline', expected=True)
  87. base_url = data['rtmp_url']
  88. live_path = data['rtmp_live']
  89. title = self._live_title(unescapeHTML(data['room_name']))
  90. description = data.get('show_details')
  91. thumbnail = data.get('room_src')
  92. uploader = data.get('nickname')
  93. uploader_id = data.get('owner_uid')
  94. multi_formats = data.get('rtmp_multi_bitrate')
  95. if not isinstance(multi_formats, dict):
  96. multi_formats = {}
  97. multi_formats['live'] = live_path
  98. formats = [{
  99. 'url': '%s/%s' % (base_url, format_path),
  100. 'format_id': format_id,
  101. 'preference': 1 if format_id == 'live' else 0,
  102. } for format_id, format_path in multi_formats.items()]
  103. self._sort_formats(formats)
  104. return {
  105. 'id': room_id,
  106. 'display_id': video_id,
  107. 'title': title,
  108. 'description': description,
  109. 'thumbnail': thumbnail,
  110. 'uploader': uploader,
  111. 'uploader_id': uploader_id,
  112. 'formats': formats,
  113. 'is_live': True,
  114. }