2 from __future__ import unicode_literals
7 from .common import InfoExtractor
8 from ..utils import (ExtractorError, unescapeHTML)
9 from ..compat import (compat_str, compat_basestring, compat_urllib_parse_urlencode)
12 class DouyuTVIE(InfoExtractor):
14 _VALID_URL = r'https?://(?:www\.)?douyu(?:tv)?\.com/(?P<id>[A-Za-z0-9]+)'
16 'url': 'http://www.douyutv.com/iseven',
19 'display_id': 'iseven',
21 'title': 're:^清晨醒脑!T-ara根本停不下来! [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
22 'description': 're:.*m7show@163\.com.*',
23 'thumbnail': 're:^https?://.*\.jpg$',
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小漠',
43 'skip_download': True,
45 'skip': 'Room not found',
47 'url': 'http://www.douyutv.com/17732',
50 'display_id': '17732',
52 'title': 're:^清晨醒脑!T-ara根本停不下来! [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
53 'description': 're:.*m7show@163\.com.*',
54 'thumbnail': 're:^https?://.*\.jpg$',
59 'skip_download': True,
62 'url': 'http://www.douyu.com/xiaocang',
63 'only_matching': True,
66 def _real_extract(self, url):
67 video_id = self._match_id(url)
69 if video_id.isdigit():
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')
76 room_url = 'http://m.douyu.com/html5/live?roomId=%s' % room_id
77 room_content = self._download_webpage(room_url, video_id)
78 room_json = self._parse_json(room_content, video_id, fatal=False)
80 room = room_json['data']
82 show_status = room.get('show_status')
83 # 1 = live, 2 = offline
84 if show_status == '2':
86 'Live stream is offline', expected=True)
89 # Douyu API sometimes returns error "Unable to load the requested class: eticket_redis_cache"
90 # Retry with different parameters - same parameters cause same errors
92 tt = int(time.time() / 60)
93 did = uuid.uuid4().hex.upper()
95 # Decompile core.swf in webpage by ffdec "Search SWFs in memory"
96 # core.swf is encrypted originally, but ffdec can dump memory to get the decrypted one
97 # If API changes in the future, just use this way to update
98 sign_content = '{room_id}{did}A12Svb&%1UUmf@hC{tt}'.format(room_id = room_id, did = did, tt = tt)
99 sign = hashlib.md5((sign_content).encode('utf-8')).hexdigest()
101 payload = {'cdn': 'ws', 'rate': '0', 'tt': tt, 'did': did, 'sign': sign}
102 flv_data = compat_urllib_parse_urlencode(payload)
104 flv_request_url = 'http://www.douyu.com/lapi/live/getPlay/%s' % room_id
105 flv_content = self._download_webpage(flv_request_url, video_id, data=flv_data,
106 headers={'Content-Type': 'application/x-www-form-urlencoded'})
108 flv_json = self._parse_json(flv_content, video_id, fatal=False)
109 except ExtractorError:
110 # Wait some time before retrying to get a different time() value
111 self._sleep(1, video_id, msg_template='%(video_id)s: Error occurs. '
112 'Waiting for %(timeout)s seconds before retrying')
117 raise ExtractorError('Unable to fetch API result')
119 flv = flv_json['data']
121 error_code = flv_json.get('error', 0)
122 if error_code is not 0:
123 error_desc = 'Server reported error %i' % error_code
124 if isinstance(flv, (compat_str, compat_basestring)):
125 error_desc += ': ' + flv
126 raise ExtractorError(error_desc, expected=True)
128 base_url = flv['rtmp_url']
129 live_path = flv['rtmp_live']
131 video_url = '%s/%s' % (base_url, live_path)
133 title = self._live_title(unescapeHTML(room['room_name']))
134 description = room.get('notice')
135 thumbnail = room.get('room_src')
136 uploader = room.get('nickname')
140 'display_id': video_id,
143 'description': description,
144 'thumbnail': thumbnail,
145 'uploader': uploader,