]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/douyutv.py
Fix "invalid escape sequences" error on Python 3.6
[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 = '斗鱼'
3bb33568 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,
8343a033 71 }]
a172d962 72
3b4b82d4
YCH
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'
76
a172d962 77 def _real_extract(self, url):
78 video_id = self._match_id(url)
a172d962 79
8343a033
YCH
80 if video_id.isdigit():
81 room_id = video_id
82 else:
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')
86
3b4b82d4
YCH
87 room = self._download_json(
88 'http://m.douyu.com/html5/live?roomId=%s' % room_id, video_id,
89 note='Downloading room info')['data']
b281aad2 90
b281aad2 91 # 1 = live, 2 = offline
3b4b82d4
YCH
92 if room.get('show_status') == '2':
93 raise ExtractorError('Live stream is offline', expected=True)
94
95 tt = compat_str(int(time.time() / 60))
96 did = uuid.uuid4().hex.upper()
97
98 sign_content = ''.join((room_id, did, self._API_KEY, tt))
99 sign = hashlib.md5((sign_content).encode('utf-8')).hexdigest()
100
101 flv_data = compat_urllib_parse_urlencode({
102 'cdn': 'ws',
103 'rate': '0',
104 'tt': tt,
105 'did': did,
106 'sign': sign,
107 })
108
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'})
113
114 error_code = video_info.get('error', 0)
a172d962 115 if error_code is not 0:
3b4b82d4
YCH
116 raise ExtractorError(
117 '%s reported error %i' % (self.IE_NAME, error_code),
118 expected=True)
a172d962 119
3b4b82d4
YCH
120 base_url = video_info['data']['rtmp_url']
121 live_path = video_info['data']['rtmp_live']
2ca1c5aa 122
b281aad2 123 video_url = '%s/%s' % (base_url, live_path)
2ca1c5aa 124
b281aad2 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')
a172d962 129
130 return {
8343a033
YCH
131 'id': room_id,
132 'display_id': video_id,
b281aad2 133 'url': video_url,
a172d962 134 'title': title,
2ca1c5aa 135 'description': description,
a172d962 136 'thumbnail': thumbnail,
2ca1c5aa 137 'uploader': uploader,
a172d962 138 'is_live': True,
2ca1c5aa 139 }