douyutv.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import hashlib
  4. import time
  5. import uuid
  6. from .common import InfoExtractor
  7. from ..utils import (ExtractorError, unescapeHTML)
  8. from ..compat import (compat_str, compat_basestring, compat_urllib_parse_urlencode)
  9. class DouyuTVIE(InfoExtractor):
  10. IE_DESC = '斗鱼'
  11. _VALID_URL = r'https?://(?:www\.)?douyu(?:tv)?\.com/(?P<id>[A-Za-z0-9]+)'
  12. _TESTS = [{
  13. 'url': 'http://www.douyutv.com/iseven',
  14. 'info_dict': {
  15. 'id': '17732',
  16. 'display_id': 'iseven',
  17. 'ext': 'flv',
  18. 'title': 're:^清晨醒脑!T-ara根本停不下来! [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
  19. 'description': 're:.*m7show@163\.com.*',
  20. 'thumbnail': 're:^https?://.*\.jpg$',
  21. 'uploader': '7师傅',
  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. 'is_live': True,
  38. },
  39. 'params': {
  40. 'skip_download': True,
  41. },
  42. 'skip': 'Room not found',
  43. }, {
  44. 'url': 'http://www.douyutv.com/17732',
  45. 'info_dict': {
  46. 'id': '17732',
  47. 'display_id': '17732',
  48. 'ext': 'flv',
  49. 'title': 're:^清晨醒脑!T-ara根本停不下来! [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
  50. 'description': 're:.*m7show@163\.com.*',
  51. 'thumbnail': 're:^https?://.*\.jpg$',
  52. 'uploader': '7师傅',
  53. 'is_live': True,
  54. },
  55. 'params': {
  56. 'skip_download': True,
  57. },
  58. }, {
  59. 'url': 'http://www.douyu.com/xiaocang',
  60. 'only_matching': True,
  61. }]
  62. def _real_extract(self, url):
  63. video_id = self._match_id(url)
  64. if video_id.isdigit():
  65. room_id = video_id
  66. else:
  67. page = self._download_webpage(url, video_id)
  68. room_id = self._html_search_regex(
  69. r'"room_id"\s*:\s*(\d+),', page, 'room id')
  70. room_url = 'http://m.douyu.com/html5/live?roomId=%s' % room_id
  71. room_content = self._download_webpage(room_url, video_id)
  72. room_json = self._parse_json(room_content, video_id, fatal=False)
  73. room = room_json['data']
  74. show_status = room.get('show_status')
  75. # 1 = live, 2 = offline
  76. if show_status == '2':
  77. raise ExtractorError(
  78. 'Live stream is offline', expected=True)
  79. flv_json = None
  80. # Douyu API sometimes returns error "Unable to load the requested class: eticket_redis_cache"
  81. # Retry with different parameters - same parameters cause same errors
  82. for i in range(5):
  83. tt = int(time.time() / 60)
  84. did = uuid.uuid4().hex.upper()
  85. # Decompile core.swf in webpage by ffdec "Search SWFs in memory"
  86. # core.swf is encrypted originally, but ffdec can dump memory to get the decrypted one
  87. # If API changes in the future, just use this way to update
  88. sign_content = '{room_id}{did}A12Svb&%1UUmf@hC{tt}'.format(room_id = room_id, did = did, tt = tt)
  89. sign = hashlib.md5((sign_content).encode('utf-8')).hexdigest()
  90. payload = {'cdn': 'ws', 'rate': '0', 'tt': tt, 'did': did, 'sign': sign}
  91. flv_data = compat_urllib_parse_urlencode(payload)
  92. flv_request_url = 'http://www.douyu.com/lapi/live/getPlay/%s' % room_id
  93. flv_content = self._download_webpage(flv_request_url, video_id, data=flv_data,
  94. headers={'Content-Type': 'application/x-www-form-urlencoded'})
  95. try:
  96. flv_json = self._parse_json(flv_content, video_id, fatal=False)
  97. except ExtractorError:
  98. # Wait some time before retrying to get a different time() value
  99. self._sleep(1, video_id, msg_template='%(video_id)s: Error occurs. '
  100. 'Waiting for %(timeout)s seconds before retrying')
  101. continue
  102. else:
  103. break
  104. if flv_json is None:
  105. raise ExtractorError('Unable to fetch API result')
  106. flv = flv_json['data']
  107. error_code = flv_json.get('error', 0)
  108. if error_code is not 0:
  109. error_desc = 'Server reported error %i' % error_code
  110. if isinstance(flv, (compat_str, compat_basestring)):
  111. error_desc += ': ' + flv
  112. raise ExtractorError(error_desc, expected=True)
  113. base_url = flv['rtmp_url']
  114. live_path = flv['rtmp_live']
  115. video_url = '%s/%s' % (base_url, live_path)
  116. title = self._live_title(unescapeHTML(room['room_name']))
  117. description = room.get('notice')
  118. thumbnail = room.get('room_src')
  119. uploader = room.get('nickname')
  120. return {
  121. 'id': room_id,
  122. 'display_id': video_id,
  123. 'url': video_url,
  124. 'title': title,
  125. 'description': description,
  126. 'thumbnail': thumbnail,
  127. 'uploader': uploader,
  128. 'is_live': True,
  129. }