]>
Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/douyutv.py
   2 from __future__ 
import unicode_literals
 
   8 from .common 
import InfoExtractor
 
  17 class DouyuTVIE(InfoExtractor
): 
  19     _VALID_URL 
= r
'https?://(?:www\.)?douyu(?:tv)?\.com/(?:[^/]+/)*(?P<id>[A-Za-z0-9]+)' 
  21         'url': 'http://www.douyutv.com/iseven', 
  24             'display_id': 'iseven', 
  26             'title': 're:^清晨醒脑!根本停不下来! [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$', 
  27             'description': r
're:.*m7show@163\.com.*', 
  28             'thumbnail': r
're:^https?://.*\.jpg$', 
  33             'skip_download': True, 
  36         'url': 'http://www.douyutv.com/85982', 
  39             'display_id': '85982', 
  41             'title': 're:^小漠从零单排记!——CSOL2躲猫猫 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$', 
  42             'description': 'md5:746a2f7a253966a06755a912f0acc0d2', 
  43             'thumbnail': r
're:^https?://.*\.jpg$', 
  44             'uploader': 'douyu小漠', 
  48             'skip_download': True, 
  50         'skip': 'Room not found', 
  52         'url': 'http://www.douyutv.com/17732', 
  55             'display_id': '17732', 
  57             'title': 're:^清晨醒脑!根本停不下来! [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$', 
  58             'description': r
're:.*m7show@163\.com.*', 
  59             'thumbnail': r
're:^https?://.*\.jpg$', 
  64             'skip_download': True, 
  67         'url': 'http://www.douyu.com/xiaocang', 
  68         'only_matching': True, 
  71         'url': 'http://www.douyu.com/t/lpl', 
  72         'only_matching': True, 
  75     def _real_extract(self
, url
): 
  76         video_id 
= self
._match
_id
(url
) 
  78         if video_id
.isdigit(): 
  81             page 
= self
._download
_webpage
(url
, video_id
) 
  82             room_id 
= self
._html
_search
_regex
( 
  83                 r
'"room_id\\?"\s*:\s*(\d+),', page
, 'room id') 
  85         # Grab metadata from mobile API 
  86         room 
= self
._download
_json
( 
  87             'http://m.douyu.com/html5/live?roomId=%s' % room_id
, video_id
, 
  88             note
='Downloading room info')['data'] 
  90         # 1 = live, 2 = offline 
  91         if room
.get('show_status') == '2': 
  92             raise ExtractorError('Live stream is offline', expected
=True) 
  94         # Grab the URL from PC client API 
  95         # The m3u8 url from mobile API requires re-authentication every 5 minutes 
  97         signContent 
= 'lapi/live/thirdPart/getPlay/%s?aid=pcclient&rate=0&time=%d9TUk5fjjUjg9qIMH3sdnh' % (room_id
, tt
) 
  98         sign 
= hashlib
.md5(signContent
.encode('ascii')).hexdigest() 
  99         video_url 
= self
._download
_json
( 
 100             'http://coapi.douyucdn.cn/lapi/live/thirdPart/getPlay/' + room_id
, 
 101             video_id
, note
='Downloading video URL info', 
 102             query
={'rate': 0}, headers
={ 
 106             })['data']['live_url'] 
 108         title 
= self
._live
_title
(unescapeHTML(room
['room_name'])) 
 109         description 
= room
.get('show_details') 
 110         thumbnail 
= room
.get('room_src') 
 111         uploader 
= room
.get('nickname') 
 115             'display_id': video_id
, 
 118             'description': description
, 
 119             'thumbnail': thumbnail
, 
 120             'uploader': uploader
, 
 125 class DouyuShowIE(InfoExtractor
): 
 126     _VALID_URL 
= r
'https?://v(?:mobile)?\.douyu\.com/show/(?P<id>[0-9a-zA-Z]+)' 
 129         'url': 'https://v.douyu.com/show/rjNBdvnVXNzvE2yw', 
 130         'md5': '0c2cfd068ee2afe657801269b2d86214', 
 132             'id': 'rjNBdvnVXNzvE2yw', 
 134             'title': '陈一发儿:砒霜 我有个室友系列!04-01 22点场', 
 136             'thumbnail': r
're:^https?://.*\.jpg$', 
 138             'uploader_id': 'XrZwYelr5wbK', 
 139             'uploader_url': 'https://v.douyu.com/author/XrZwYelr5wbK', 
 140             'upload_date': '20170402', 
 143         'url': 'https://vmobile.douyu.com/show/rjNBdvnVXNzvE2yw', 
 144         'only_matching': True, 
 147     def _real_extract(self
, url
): 
 148         url 
= url
.replace('vmobile.', 'v.') 
 149         video_id 
= self
._match
_id
(url
) 
 151         webpage 
= self
._download
_webpage
(url
, video_id
) 
 153         room_info 
= self
._parse
_json
(self
._search
_regex
( 
 154             r
'var\s+\$ROOM\s*=\s*({.+});', webpage
, 'room info'), video_id
) 
 158         for trial 
in range(5): 
 159             # Sometimes Douyu rejects our request. Let's try it more times 
 161                 video_info 
= self
._download
_json
( 
 162                     'https://vmobile.douyu.com/video/getInfo', video_id
, 
 163                     query
={'vid': video_id
}, 
 166                         'x-requested-with': 'XMLHttpRequest', 
 169             except ExtractorError
: 
 170                 self
._sleep
(1, video_id
) 
 173             raise ExtractorError('Can\'t fetch video info') 
 175         formats 
= self
._extract
_m
3u8_formats
( 
 176             video_info
['data']['video_url'], video_id
, 
 177             entry_protocol
='m3u8_native', ext
='mp4') 
 179         upload_date 
= unified_strdate(self
._html
_search
_regex
( 
 180             r
'<em>上传时间:</em><span>([^<]+)</span>', webpage
, 
 181             'upload date', fatal
=False)) 
 183         uploader 
= uploader_id 
= uploader_url 
= None 
 185             r
'(?m)<a[^>]+href="/author/([0-9a-zA-Z]+)".+?<strong[^>]+title="([^"]+)"', 
 188             uploader_id
, uploader 
= mobj
.groups() 
 189             uploader_url 
= urljoin(url
, '/author/' + uploader_id
) 
 193             'title': room_info
['name'], 
 195             'duration': room_info
.get('duration'), 
 196             'thumbnail': room_info
.get('pic'), 
 197             'upload_date': upload_date
, 
 198             'uploader': uploader
, 
 199             'uploader_id': uploader_id
, 
 200             'uploader_url': uploader_url
,