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.

112 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. _VALID_URL = r'http://(?:www\.)?douyutv\.com/(?P<id>[A-Za-z0-9]+)'
  10. _TESTS = [{
  11. 'url': 'http://www.douyutv.com/iseven',
  12. 'info_dict': {
  13. 'id': '17732',
  14. 'display_id': 'iseven',
  15. 'ext': 'flv',
  16. 'title': 're:^清晨醒脑!T-ara根本停不下来! [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
  17. 'description': 'md5:c93d6692dde6fe33809a46edcbecca44',
  18. 'thumbnail': 're:^https?://.*\.jpg$',
  19. 'uploader': '7师傅',
  20. 'uploader_id': '431925',
  21. 'is_live': True,
  22. },
  23. 'params': {
  24. 'skip_download': True,
  25. }
  26. }, {
  27. 'url': 'http://www.douyutv.com/85982',
  28. 'info_dict': {
  29. 'id': '85982',
  30. 'display_id': '85982',
  31. 'ext': 'flv',
  32. 'title': 're:^小漠从零单排记!——CSOL2躲猫猫 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
  33. 'description': 'md5:746a2f7a253966a06755a912f0acc0d2',
  34. 'thumbnail': 're:^https?://.*\.jpg$',
  35. 'uploader': 'douyu小漠',
  36. 'uploader_id': '3769985',
  37. 'is_live': True,
  38. },
  39. 'params': {
  40. 'skip_download': True,
  41. }
  42. }]
  43. def _real_extract(self, url):
  44. video_id = self._match_id(url)
  45. if video_id.isdigit():
  46. room_id = video_id
  47. else:
  48. page = self._download_webpage(url, video_id)
  49. room_id = self._html_search_regex(
  50. r'"room_id"\s*:\s*(\d+),', page, 'room id')
  51. prefix = 'room/%s?aid=android&client_sys=android&time=%d' % (
  52. room_id, int(time.time()))
  53. auth = hashlib.md5((prefix + '1231').encode('ascii')).hexdigest()
  54. config = self._download_json(
  55. 'http://www.douyutv.com/api/v1/%s&auth=%s' % (prefix, auth),
  56. video_id)
  57. data = config['data']
  58. error_code = config.get('error', 0)
  59. if error_code is not 0:
  60. error_desc = 'Server reported error %i' % error_code
  61. if isinstance(data, (compat_str, compat_basestring)):
  62. error_desc += ': ' + data
  63. raise ExtractorError(error_desc, expected=True)
  64. show_status = data.get('show_status')
  65. # 1 = live, 2 = offline
  66. if show_status == '2':
  67. raise ExtractorError(
  68. 'Live stream is offline', expected=True)
  69. base_url = data['rtmp_url']
  70. live_path = data['rtmp_live']
  71. title = self._live_title(unescapeHTML(data['room_name']))
  72. description = data.get('show_details')
  73. thumbnail = data.get('room_src')
  74. uploader = data.get('nickname')
  75. uploader_id = data.get('owner_uid')
  76. multi_formats = data.get('rtmp_multi_bitrate')
  77. if not isinstance(multi_formats, dict):
  78. multi_formats = {}
  79. multi_formats['live'] = live_path
  80. formats = [{
  81. 'url': '%s/%s' % (base_url, format_path),
  82. 'format_id': format_id,
  83. 'preference': 1 if format_id == 'live' else 0,
  84. } for format_id, format_path in multi_formats.items()]
  85. self._sort_formats(formats)
  86. return {
  87. 'id': room_id,
  88. 'display_id': video_id,
  89. 'title': title,
  90. 'description': description,
  91. 'thumbnail': thumbnail,
  92. 'uploader': uploader,
  93. 'uploader_id': uploader_id,
  94. 'formats': formats,
  95. 'is_live': True,
  96. }