2 from __future__ import unicode_literals
6 from .common import InfoExtractor
7 from ..utils import (ExtractorError, unescapeHTML)
8 from ..compat import (compat_str, compat_basestring)
11 class DouyuTVIE(InfoExtractor):
13 _VALID_URL = r'https?://(?:www\.)?douyu(?:tv)?\.com/(?P<id>[A-Za-z0-9]+)'
15 'url': 'http://www.douyutv.com/iseven',
18 'display_id': 'iseven',
20 'title': 're:^清晨醒脑!T-ara根本停不下来! [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
21 'description': 're:.*m7show@163\.com.*',
22 'thumbnail': 're:^https?://.*\.jpg$',
24 'uploader_id': '431925',
28 'skip_download': True,
31 'url': 'http://www.douyutv.com/85982',
34 'display_id': '85982',
36 'title': 're:^小漠从零单排记!——CSOL2躲猫猫 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
37 'description': 'md5:746a2f7a253966a06755a912f0acc0d2',
38 'thumbnail': 're:^https?://.*\.jpg$',
39 'uploader': 'douyu小漠',
40 'uploader_id': '3769985',
44 'skip_download': True,
46 'skip': 'Room not found',
48 'url': 'http://www.douyutv.com/17732',
51 'display_id': '17732',
53 'title': 're:^清晨醒脑!T-ara根本停不下来! [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
54 'description': 're:.*m7show@163\.com.*',
55 'thumbnail': 're:^https?://.*\.jpg$',
57 'uploader_id': '431925',
61 'skip_download': True,
64 'url': 'http://www.douyu.com/xiaocang',
65 'only_matching': True,
68 def _real_extract(self, url):
69 video_id = self._match_id(url)
71 if video_id.isdigit():
74 page = self._download_webpage(url, video_id)
75 room_id = self._html_search_regex(
76 r'"room_id"\s*:\s*(\d+),', page, 'room id')
79 # Douyu API sometimes returns error "Unable to load the requested class: eticket_redis_cache"
80 # Retry with different parameters - same parameters cause same errors
82 prefix = 'room/%s?aid=android&client_sys=android&time=%d' % (
83 room_id, int(time.time()))
84 auth = hashlib.md5((prefix + '1231').encode('ascii')).hexdigest()
86 config_page = self._download_webpage(
87 'http://www.douyutv.com/api/v1/%s&auth=%s' % (prefix, auth),
90 config = self._parse_json(config_page, video_id, fatal=False)
91 except ExtractorError:
92 # Wait some time before retrying to get a different time() value
93 self._sleep(1, video_id, msg_template='%(video_id)s: Error occurs. '
94 'Waiting for %(timeout)s seconds before retrying')
99 raise ExtractorError('Unable to fetch API result')
101 data = config['data']
103 error_code = config.get('error', 0)
104 if error_code is not 0:
105 error_desc = 'Server reported error %i' % error_code
106 if isinstance(data, (compat_str, compat_basestring)):
107 error_desc += ': ' + data
108 raise ExtractorError(error_desc, expected=True)
110 show_status = data.get('show_status')
111 # 1 = live, 2 = offline
112 if show_status == '2':
113 raise ExtractorError(
114 'Live stream is offline', expected=True)
116 base_url = data['rtmp_url']
117 live_path = data['rtmp_live']
119 title = self._live_title(unescapeHTML(data['room_name']))
120 description = data.get('show_details')
121 thumbnail = data.get('room_src')
123 uploader = data.get('nickname')
124 uploader_id = data.get('owner_uid')
126 multi_formats = data.get('rtmp_multi_bitrate')
127 if not isinstance(multi_formats, dict):
129 multi_formats['live'] = live_path
132 'url': '%s/%s' % (base_url, format_path),
133 'format_id': format_id,
134 'preference': 1 if format_id == 'live' else 0,
135 } for format_id, format_path in multi_formats.items()]
136 self._sort_formats(formats)
140 'display_id': video_id,
142 'description': description,
143 'thumbnail': thumbnail,
144 'uploader': uploader,
145 'uploader_id': uploader_id,