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.

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