]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/douyutv.py
Prepare for upload.
[youtubedl] / youtube_dl / extractor / douyutv.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import hashlib
5 import time
6 import uuid
7
8 from .common import InfoExtractor
9 from ..compat import (
10 compat_str,
11 compat_urllib_parse_urlencode,
12 )
13 from ..utils import (
14 ExtractorError,
15 unescapeHTML,
16 )
17
18
19 class DouyuTVIE(InfoExtractor):
20 IE_DESC = '斗鱼'
21 _VALID_URL = r'https?://(?:www\.)?douyu(?:tv)?\.com/(?:[^/]+/)*(?P<id>[A-Za-z0-9]+)'
22 _TESTS = [{
23 'url': 'http://www.douyutv.com/iseven',
24 'info_dict': {
25 'id': '17732',
26 'display_id': 'iseven',
27 'ext': 'flv',
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$',
31 'uploader': '7师傅',
32 'is_live': True,
33 },
34 'params': {
35 'skip_download': True,
36 },
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',
45 'thumbnail': r're:^https?://.*\.jpg$',
46 'uploader': 'douyu小漠',
47 'is_live': True,
48 },
49 'params': {
50 'skip_download': True,
51 },
52 'skip': 'Room not found',
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}$',
60 'description': r're:.*m7show@163\.com.*',
61 'thumbnail': r're:^https?://.*\.jpg$',
62 'uploader': '7师傅',
63 'is_live': True,
64 },
65 'params': {
66 'skip_download': True,
67 },
68 }, {
69 'url': 'http://www.douyu.com/xiaocang',
70 'only_matching': True,
71 }, {
72 # \"room_id\"
73 'url': 'http://www.douyu.com/t/lpl',
74 'only_matching': True,
75 }]
76
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
81 def _real_extract(self, url):
82 video_id = self._match_id(url)
83
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(
89 r'"room_id\\?"\s*:\s*(\d+),', page, 'room id')
90
91 room = self._download_json(
92 'http://m.douyu.com/html5/live?roomId=%s' % room_id, video_id,
93 note='Downloading room info')['data']
94
95 # 1 = live, 2 = offline
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)
119 if error_code is not 0:
120 raise ExtractorError(
121 '%s reported error %i' % (self.IE_NAME, error_code),
122 expected=True)
123
124 base_url = video_info['data']['rtmp_url']
125 live_path = video_info['data']['rtmp_live']
126
127 video_url = '%s/%s' % (base_url, live_path)
128
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')
133
134 return {
135 'id': room_id,
136 'display_id': video_id,
137 'url': video_url,
138 'title': title,
139 'description': description,
140 'thumbnail': thumbnail,
141 'uploader': uploader,
142 'is_live': True,
143 }