]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/douyutv.py
release 2017.02.27
[yt-dlp.git] / youtube_dl / extractor / douyutv.py
CommitLineData
a172d962 1# coding: utf-8
2from __future__ import unicode_literals
3
8343a033
YCH
4import hashlib
5import time
b281aad2 6import uuid
3b4b82d4 7
a172d962 8from .common import InfoExtractor
3b4b82d4
YCH
9from ..compat import (
10 compat_str,
11 compat_urllib_parse_urlencode,
12)
13from ..utils import (
14 ExtractorError,
15 unescapeHTML,
16)
a172d962 17
a172d962 18
2ca1c5aa 19class DouyuTVIE(InfoExtractor):
513cbdda 20 IE_DESC = '斗鱼'
33da98f4 21 _VALID_URL = r'https?://(?:www\.)?douyu(?:tv)?\.com/(?:[^/]+/)*(?P<id>[A-Za-z0-9]+)'
8343a033 22 _TESTS = [{
a172d962 23 'url': 'http://www.douyutv.com/iseven',
24 'info_dict': {
8343a033
YCH
25 'id': '17732',
26 'display_id': 'iseven',
a172d962 27 'ext': 'flv',
2ca1c5aa 28 'title': 're:^清晨醒脑!T-ara根本停不下来! [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
ec85ded8
YCH
29 'description': r're:.*m7show@163\.com.*',
30 'thumbnail': r're:^https?://.*\.jpg$',
2ca1c5aa 31 'uploader': '7师傅',
a172d962 32 'is_live': True,
2ca1c5aa
S
33 },
34 'params': {
35 'skip_download': True,
24ca0e9c 36 },
8343a033
YCH
37 }, {
38 'url': 'http://www.douyutv.com/85982',
39 'info_dict': {
40 'id': '85982',
41 'display_id': '85982',
42 'ext': 'flv',
43 'title': 're:^小漠从零单排记!——CSOL2躲猫猫 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
44 'description': 'md5:746a2f7a253966a06755a912f0acc0d2',
ec85ded8 45 'thumbnail': r're:^https?://.*\.jpg$',
8343a033 46 'uploader': 'douyu小漠',
8343a033
YCH
47 'is_live': True,
48 },
49 'params': {
50 'skip_download': True,
24ca0e9c 51 },
aa9dc24f 52 'skip': 'Room not found',
24ca0e9c
YCH
53 }, {
54 'url': 'http://www.douyutv.com/17732',
55 'info_dict': {
56 'id': '17732',
57 'display_id': '17732',
58 'ext': 'flv',
59 'title': 're:^清晨醒脑!T-ara根本停不下来! [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
ec85ded8
YCH
60 'description': r're:.*m7show@163\.com.*',
61 'thumbnail': r're:^https?://.*\.jpg$',
24ca0e9c 62 'uploader': '7师傅',
24ca0e9c
YCH
63 'is_live': True,
64 },
65 'params': {
66 'skip_download': True,
67 },
3bb33568
YCH
68 }, {
69 'url': 'http://www.douyu.com/xiaocang',
70 'only_matching': True,
33da98f4
J
71 }, {
72 # \"room_id\"
73 'url': 'http://www.douyu.com/t/lpl',
74 'only_matching': True,
8343a033 75 }]
a172d962 76
3b4b82d4
YCH
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'
80
a172d962 81 def _real_extract(self, url):
82 video_id = self._match_id(url)
a172d962 83
8343a033
YCH
84 if video_id.isdigit():
85 room_id = video_id
86 else:
87 page = self._download_webpage(url, video_id)
88 room_id = self._html_search_regex(
33da98f4 89 r'"room_id\\?"\s*:\s*(\d+),', page, 'room id')
8343a033 90
3b4b82d4
YCH
91 room = self._download_json(
92 'http://m.douyu.com/html5/live?roomId=%s' % room_id, video_id,
93 note='Downloading room info')['data']
b281aad2 94
b281aad2 95 # 1 = live, 2 = offline
3b4b82d4
YCH
96 if room.get('show_status') == '2':
97 raise ExtractorError('Live stream is offline', expected=True)
98
99 tt = compat_str(int(time.time() / 60))
100 did = uuid.uuid4().hex.upper()
101
102 sign_content = ''.join((room_id, did, self._API_KEY, tt))
103 sign = hashlib.md5((sign_content).encode('utf-8')).hexdigest()
104
105 flv_data = compat_urllib_parse_urlencode({
106 'cdn': 'ws',
107 'rate': '0',
108 'tt': tt,
109 'did': did,
110 'sign': sign,
111 })
112
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'})
117
118 error_code = video_info.get('error', 0)
a172d962 119 if error_code is not 0:
3b4b82d4
YCH
120 raise ExtractorError(
121 '%s reported error %i' % (self.IE_NAME, error_code),
122 expected=True)
a172d962 123
3b4b82d4
YCH
124 base_url = video_info['data']['rtmp_url']
125 live_path = video_info['data']['rtmp_live']
2ca1c5aa 126
b281aad2 127 video_url = '%s/%s' % (base_url, live_path)
2ca1c5aa 128
b281aad2 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')
a172d962 133
134 return {
8343a033
YCH
135 'id': room_id,
136 'display_id': video_id,
b281aad2 137 'url': video_url,
a172d962 138 'title': title,
2ca1c5aa 139 'description': description,
a172d962 140 'thumbnail': thumbnail,
2ca1c5aa 141 'uploader': uploader,
a172d962 142 'is_live': True,
2ca1c5aa 143 }