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.

143 lines
4.5 KiB

  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 ..compat import (
  8. compat_str,
  9. compat_urllib_parse_urlencode,
  10. )
  11. from ..utils import (
  12. ExtractorError,
  13. unescapeHTML,
  14. )
  15. class DouyuTVIE(InfoExtractor):
  16. IE_DESC = '斗鱼'
  17. _VALID_URL = r'https?://(?:www\.)?douyu(?:tv)?\.com/(?:[^/]+/)*(?P<id>[A-Za-z0-9]+)'
  18. _TESTS = [{
  19. 'url': 'http://www.douyutv.com/iseven',
  20. 'info_dict': {
  21. 'id': '17732',
  22. 'display_id': 'iseven',
  23. 'ext': 'flv',
  24. 'title': 're:^清晨醒脑!T-ara根本停不下来! [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
  25. 'description': r're:.*m7show@163\.com.*',
  26. 'thumbnail': r're:^https?://.*\.jpg$',
  27. 'uploader': '7师傅',
  28. 'is_live': True,
  29. },
  30. 'params': {
  31. 'skip_download': True,
  32. },
  33. }, {
  34. 'url': 'http://www.douyutv.com/85982',
  35. 'info_dict': {
  36. 'id': '85982',
  37. 'display_id': '85982',
  38. 'ext': 'flv',
  39. 'title': 're:^小漠从零单排记!——CSOL2躲猫猫 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
  40. 'description': 'md5:746a2f7a253966a06755a912f0acc0d2',
  41. 'thumbnail': r're:^https?://.*\.jpg$',
  42. 'uploader': 'douyu小漠',
  43. 'is_live': True,
  44. },
  45. 'params': {
  46. 'skip_download': True,
  47. },
  48. 'skip': 'Room not found',
  49. }, {
  50. 'url': 'http://www.douyutv.com/17732',
  51. 'info_dict': {
  52. 'id': '17732',
  53. 'display_id': '17732',
  54. 'ext': 'flv',
  55. 'title': 're:^清晨醒脑!T-ara根本停不下来! [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
  56. 'description': r're:.*m7show@163\.com.*',
  57. 'thumbnail': r're:^https?://.*\.jpg$',
  58. 'uploader': '7师傅',
  59. 'is_live': True,
  60. },
  61. 'params': {
  62. 'skip_download': True,
  63. },
  64. }, {
  65. 'url': 'http://www.douyu.com/xiaocang',
  66. 'only_matching': True,
  67. }, {
  68. # \"room_id\"
  69. 'url': 'http://www.douyu.com/t/lpl',
  70. 'only_matching': True,
  71. }]
  72. # Decompile core.swf in webpage by ffdec "Search SWFs in memory". core.swf
  73. # is encrypted originally, but ffdec can dump memory to get the decrypted one.
  74. _API_KEY = 'A12Svb&%1UUmf@hC'
  75. def _real_extract(self, url):
  76. video_id = self._match_id(url)
  77. if video_id.isdigit():
  78. room_id = video_id
  79. else:
  80. page = self._download_webpage(url, video_id)
  81. room_id = self._html_search_regex(
  82. r'"room_id\\?"\s*:\s*(\d+),', page, 'room id')
  83. room = self._download_json(
  84. 'http://m.douyu.com/html5/live?roomId=%s' % room_id, video_id,
  85. note='Downloading room info')['data']
  86. # 1 = live, 2 = offline
  87. if room.get('show_status') == '2':
  88. raise ExtractorError('Live stream is offline', expected=True)
  89. tt = compat_str(int(time.time() / 60))
  90. did = uuid.uuid4().hex.upper()
  91. sign_content = ''.join((room_id, did, self._API_KEY, tt))
  92. sign = hashlib.md5((sign_content).encode('utf-8')).hexdigest()
  93. flv_data = compat_urllib_parse_urlencode({
  94. 'cdn': 'ws',
  95. 'rate': '0',
  96. 'tt': tt,
  97. 'did': did,
  98. 'sign': sign,
  99. })
  100. video_info = self._download_json(
  101. 'http://www.douyu.com/lapi/live/getPlay/%s' % room_id, video_id,
  102. data=flv_data, note='Downloading video info',
  103. headers={'Content-Type': 'application/x-www-form-urlencoded'})
  104. error_code = video_info.get('error', 0)
  105. if error_code is not 0:
  106. raise ExtractorError(
  107. '%s reported error %i' % (self.IE_NAME, error_code),
  108. expected=True)
  109. base_url = video_info['data']['rtmp_url']
  110. live_path = video_info['data']['rtmp_live']
  111. video_url = '%s/%s' % (base_url, live_path)
  112. title = self._live_title(unescapeHTML(room['room_name']))
  113. description = room.get('notice')
  114. thumbnail = room.get('room_src')
  115. uploader = room.get('nickname')
  116. return {
  117. 'id': room_id,
  118. 'display_id': video_id,
  119. 'url': video_url,
  120. 'title': title,
  121. 'description': description,
  122. 'thumbnail': thumbnail,
  123. 'uploader': uploader,
  124. 'is_live': True,
  125. }