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.

119 lines
3.9 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import time
  4. import hashlib
  5. from .common import InfoExtractor
  6. from ..utils import (
  7. ExtractorError,
  8. unescapeHTML,
  9. )
  10. class DouyuTVIE(InfoExtractor):
  11. IE_DESC = '斗鱼'
  12. _VALID_URL = r'https?://(?:www\.)?douyu(?:tv)?\.com/(?:[^/]+/)*(?P<id>[A-Za-z0-9]+)'
  13. _TESTS = [{
  14. 'url': 'http://www.douyutv.com/iseven',
  15. 'info_dict': {
  16. 'id': '17732',
  17. 'display_id': 'iseven',
  18. 'ext': 'flv',
  19. 'title': 're:^清晨醒脑!T-ARA根本停不下来! [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
  20. 'description': r're:.*m7show@163\.com.*',
  21. 'thumbnail': r're:^https?://.*\.jpg$',
  22. 'uploader': '7师傅',
  23. 'is_live': True,
  24. },
  25. 'params': {
  26. 'skip_download': True,
  27. },
  28. }, {
  29. 'url': 'http://www.douyutv.com/85982',
  30. 'info_dict': {
  31. 'id': '85982',
  32. 'display_id': '85982',
  33. 'ext': 'flv',
  34. 'title': 're:^小漠从零单排记!——CSOL2躲猫猫 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
  35. 'description': 'md5:746a2f7a253966a06755a912f0acc0d2',
  36. 'thumbnail': r're:^https?://.*\.jpg$',
  37. 'uploader': 'douyu小漠',
  38. 'is_live': True,
  39. },
  40. 'params': {
  41. 'skip_download': True,
  42. },
  43. 'skip': 'Room not found',
  44. }, {
  45. 'url': 'http://www.douyutv.com/17732',
  46. 'info_dict': {
  47. 'id': '17732',
  48. 'display_id': '17732',
  49. 'ext': 'flv',
  50. 'title': 're:^清晨醒脑!T-ARA根本停不下来! [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
  51. 'description': r're:.*m7show@163\.com.*',
  52. 'thumbnail': r're:^https?://.*\.jpg$',
  53. 'uploader': '7师傅',
  54. 'is_live': True,
  55. },
  56. 'params': {
  57. 'skip_download': True,
  58. },
  59. }, {
  60. 'url': 'http://www.douyu.com/xiaocang',
  61. 'only_matching': True,
  62. }, {
  63. # \"room_id\"
  64. 'url': 'http://www.douyu.com/t/lpl',
  65. 'only_matching': True,
  66. }]
  67. def _real_extract(self, url):
  68. video_id = self._match_id(url)
  69. if video_id.isdigit():
  70. room_id = video_id
  71. else:
  72. page = self._download_webpage(url, video_id)
  73. room_id = self._html_search_regex(
  74. r'"room_id\\?"\s*:\s*(\d+),', page, 'room id')
  75. # Grab metadata from mobile API
  76. room = self._download_json(
  77. 'http://m.douyu.com/html5/live?roomId=%s' % room_id, video_id,
  78. note='Downloading room info')['data']
  79. # 1 = live, 2 = offline
  80. if room.get('show_status') == '2':
  81. raise ExtractorError('Live stream is offline', expected=True)
  82. # Grab the URL from PC client API
  83. # The m3u8 url from mobile API requires re-authentication every 5 minutes
  84. tt = int(time.time())
  85. signContent = 'lapi/live/thirdPart/getPlay/%s?aid=pcclient&rate=0&time=%d9TUk5fjjUjg9qIMH3sdnh' % (room_id, tt)
  86. sign = hashlib.md5(signContent.encode('ascii')).hexdigest()
  87. video_url = self._download_json(
  88. 'http://coapi.douyucdn.cn/lapi/live/thirdPart/getPlay/' + room_id,
  89. video_id, note='Downloading video URL info',
  90. query={'rate': 0}, headers={
  91. 'auth': sign,
  92. 'time': str(tt),
  93. 'aid': 'pcclient'
  94. })['data']['live_url']
  95. title = self._live_title(unescapeHTML(room['room_name']))
  96. description = room.get('show_details')
  97. thumbnail = room.get('room_src')
  98. uploader = room.get('nickname')
  99. return {
  100. 'id': room_id,
  101. 'display_id': video_id,
  102. 'url': video_url,
  103. 'title': title,
  104. 'description': description,
  105. 'thumbnail': thumbnail,
  106. 'uploader': uploader,
  107. 'is_live': True,
  108. }