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': 're:.*m7show@163\.com.*', 
  30             'thumbnail': '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': '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': 're:.*m7show@163\.com.*', 
  61             'thumbnail': 're:^https?://.*\.jpg$', 
  66             'skip_download': True, 
  69         'url': 'http://www.douyu.com/xiaocang', 
  70         'only_matching': True, 
  73     # Decompile core.swf in webpage by ffdec "Search SWFs in memory". core.swf 
  74     # is encrypted originally, but ffdec can dump memory to get the decrypted one. 
  75     _API_KEY 
= 'A12Svb&%1UUmf@hC' 
  77     def _real_extract(self
, url
): 
  78         video_id 
= self
._match
_id
(url
) 
  80         if video_id
.isdigit(): 
  83             page 
= self
._download
_webpage
(url
, video_id
) 
  84             room_id 
= self
._html
_search
_regex
( 
  85                 r
'"room_id"\s*:\s*(\d+),', page
, 'room id') 
  87         room 
= self
._download
_json
( 
  88             'http://m.douyu.com/html5/live?roomId=%s' % room_id
, video_id
, 
  89             note
='Downloading room info')['data'] 
  91         # 1 = live, 2 = offline 
  92         if room
.get('show_status') == '2': 
  93             raise ExtractorError('Live stream is offline', expected
=True) 
  95         tt 
= compat_str(int(time
.time() / 60)) 
  96         did 
= uuid
.uuid4().hex.upper() 
  98         sign_content 
= ''.join((room_id
, did
, self
._API
_KEY
, tt
)) 
  99         sign 
= hashlib
.md5((sign_content
).encode('utf-8')).hexdigest() 
 101         flv_data 
= compat_urllib_parse_urlencode({ 
 109         video_info 
= self
._download
_json
( 
 110             'http://www.douyu.com/lapi/live/getPlay/%s' % room_id
, video_id
, 
 111             data
=flv_data
, note
='Downloading video info', 
 112             headers
={'Content-Type': 'application/x-www-form-urlencoded'}) 
 114         error_code 
= video_info
.get('error', 0) 
 115         if error_code 
is not 0: 
 116             raise ExtractorError( 
 117                 '%s reported error %i' % (self
.IE_NAME
, error_code
), 
 120         base_url 
= video_info
['data']['rtmp_url'] 
 121         live_path 
= video_info
['data']['rtmp_live'] 
 123         video_url 
= '%s/%s' % (base_url
, live_path
) 
 125         title 
= self
._live
_title
(unescapeHTML(room
['room_name'])) 
 126         description 
= room
.get('notice') 
 127         thumbnail 
= room
.get('room_src') 
 128         uploader 
= room
.get('nickname') 
 132             'display_id': video_id
, 
 135             'description': description
, 
 136             'thumbnail': thumbnail
, 
 137             'uploader': uploader
,