2 from __future__ 
import unicode_literals
 
   8 from .common 
import InfoExtractor
 
  11     compat_urllib_parse_urlencode
, 
  19 class DouyuTVIE(InfoExtractor
): 
  21     _VALID_URL 
= r
'https?://(?:www\.)?douyu(?:tv)?\.com/(?:[^/]+/)*(?P<id>[A-Za-z0-9]+)' 
  23         'url': 'http://www.douyutv.com/iseven', 
  26             'display_id': 'iseven', 
  28             'title': 're:^清晨醒脑!T-ara根本停不下来! [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$', 
  29             'description': r
're:.*m7show@163\.com.*', 
  30             'thumbnail': r
're:^https?://.*\.jpg$', 
  35             'skip_download': True, 
  38         'url': 'http://www.douyutv.com/85982', 
  41             'display_id': '85982', 
  43             'title': 're:^小漠从零单排记!——CSOL2躲猫猫 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$', 
  44             'description': 'md5:746a2f7a253966a06755a912f0acc0d2', 
  45             'thumbnail': r
're:^https?://.*\.jpg$', 
  46             'uploader': 'douyu小漠', 
  50             'skip_download': True, 
  52         'skip': 'Room not found', 
  54         'url': 'http://www.douyutv.com/17732', 
  57             'display_id': '17732', 
  59             'title': 're:^清晨醒脑!T-ara根本停不下来! [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$', 
  60             'description': r
're:.*m7show@163\.com.*', 
  61             'thumbnail': r
're:^https?://.*\.jpg$', 
  66             'skip_download': True, 
  69         'url': 'http://www.douyu.com/xiaocang', 
  70         'only_matching': True, 
  73         'url': 'http://www.douyu.com/t/lpl', 
  74         'only_matching': True, 
  77     # Decompile core.swf in webpage by ffdec "Search SWFs in memory". core.swf 
  78     # is encrypted originally, but ffdec can dump memory to get the decrypted one. 
  79     _API_KEY 
= 'A12Svb&%1UUmf@hC' 
  81     def _real_extract(self
, url
): 
  82         video_id 
= self
._match
_id
(url
) 
  84         if video_id
.isdigit(): 
  87             page 
= self
._download
_webpage
(url
, video_id
) 
  88             room_id 
= self
._html
_search
_regex
( 
  89                 r
'"room_id\\?"\s*:\s*(\d+),', page
, 'room id') 
  91         room 
= self
._download
_json
( 
  92             'http://m.douyu.com/html5/live?roomId=%s' % room_id
, video_id
, 
  93             note
='Downloading room info')['data'] 
  95         # 1 = live, 2 = offline 
  96         if room
.get('show_status') == '2': 
  97             raise ExtractorError('Live stream is offline', expected
=True) 
  99         tt 
= compat_str(int(time
.time() / 60)) 
 100         did 
= uuid
.uuid4().hex.upper() 
 102         sign_content 
= ''.join((room_id
, did
, self
._API
_KEY
, tt
)) 
 103         sign 
= hashlib
.md5((sign_content
).encode('utf-8')).hexdigest() 
 105         flv_data 
= compat_urllib_parse_urlencode({ 
 113         video_info 
= self
._download
_json
( 
 114             'http://www.douyu.com/lapi/live/getPlay/%s' % room_id
, video_id
, 
 115             data
=flv_data
, note
='Downloading video info', 
 116             headers
={'Content-Type': 'application/x-www-form-urlencoded'}) 
 118         error_code 
= video_info
.get('error', 0) 
 119         if error_code 
is not 0: 
 120             raise ExtractorError( 
 121                 '%s reported error %i' % (self
.IE_NAME
, error_code
), 
 124         base_url 
= video_info
['data']['rtmp_url'] 
 125         live_path 
= video_info
['data']['rtmp_live'] 
 127         video_url 
= '%s/%s' % (base_url
, live_path
) 
 129         title 
= self
._live
_title
(unescapeHTML(room
['room_name'])) 
 130         description 
= room
.get('notice') 
 131         thumbnail 
= room
.get('room_src') 
 132         uploader 
= room
.get('nickname') 
 136             'display_id': video_id
, 
 139             'description': description
, 
 140             'thumbnail': thumbnail
, 
 141             'uploader': uploader
,