Fix "invalid escape sequences" error on Python 3.6
[youtube-dl] / 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
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
77     def _real_extract(self, url):
78         video_id = self._match_id(url)
79
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
87         room = self._download_json(
88             'http://m.douyu.com/html5/live?roomId=%s' % room_id, video_id,
89             note='Downloading room info')['data']
90
91         # 1 = live, 2 = offline
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)
115         if error_code is not 0:
116             raise ExtractorError(
117                 '%s reported error %i' % (self.IE_NAME, error_code),
118                 expected=True)
119
120         base_url = video_info['data']['rtmp_url']
121         live_path = video_info['data']['rtmp_live']
122
123         video_url = '%s/%s' % (base_url, live_path)
124
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')
129
130         return {
131             'id': room_id,
132             'display_id': video_id,
133             'url': video_url,
134             'title': title,
135             'description': description,
136             'thumbnail': thumbnail,
137             'uploader': uploader,
138             'is_live': True,
139         }