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.

139 lines
4.4 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. # Decompile core.swf in webpage by ffdec "Search SWFs in memory". core.swf
  69. # is encrypted originally, but ffdec can dump memory to get the decrypted one.
  70. _API_KEY = 'A12Svb&%1UUmf@hC'
  71. def _real_extract(self, url):
  72. video_id = self._match_id(url)
  73. if video_id.isdigit():
  74. room_id = video_id
  75. else:
  76. page = self._download_webpage(url, video_id)
  77. room_id = self._html_search_regex(
  78. r'"room_id"\s*:\s*(\d+),', page, 'room id')
  79. room = self._download_json(
  80. 'http://m.douyu.com/html5/live?roomId=%s' % room_id, video_id,
  81. note='Downloading room info')['data']
  82. # 1 = live, 2 = offline
  83. if room.get('show_status') == '2':
  84. raise ExtractorError('Live stream is offline', expected=True)
  85. tt = compat_str(int(time.time() / 60))
  86. did = uuid.uuid4().hex.upper()
  87. sign_content = ''.join((room_id, did, self._API_KEY, tt))
  88. sign = hashlib.md5((sign_content).encode('utf-8')).hexdigest()
  89. flv_data = compat_urllib_parse_urlencode({
  90. 'cdn': 'ws',
  91. 'rate': '0',
  92. 'tt': tt,
  93. 'did': did,
  94. 'sign': sign,
  95. })
  96. video_info = self._download_json(
  97. 'http://www.douyu.com/lapi/live/getPlay/%s' % room_id, video_id,
  98. data=flv_data, note='Downloading video info',
  99. headers={'Content-Type': 'application/x-www-form-urlencoded'})
  100. error_code = video_info.get('error', 0)
  101. if error_code is not 0:
  102. raise ExtractorError(
  103. '%s reported error %i' % (self.IE_NAME, error_code),
  104. expected=True)
  105. base_url = video_info['data']['rtmp_url']
  106. live_path = video_info['data']['rtmp_live']
  107. video_url = '%s/%s' % (base_url, live_path)
  108. title = self._live_title(unescapeHTML(room['room_name']))
  109. description = room.get('notice')
  110. thumbnail = room.get('room_src')
  111. uploader = room.get('nickname')
  112. return {
  113. 'id': room_id,
  114. 'display_id': video_id,
  115. 'url': video_url,
  116. 'title': title,
  117. 'description': description,
  118. 'thumbnail': thumbnail,
  119. 'uploader': uploader,
  120. 'is_live': True,
  121. }