]>
Commit | Line | Data |
---|---|---|
a172d962 | 1 | # coding: utf-8 |
2 | from __future__ import unicode_literals | |
3 | ||
8343a033 YCH |
4 | import hashlib |
5 | import time | |
a172d962 | 6 | from .common import InfoExtractor |
8343a033 YCH |
7 | from ..utils import (ExtractorError, unescapeHTML) |
8 | from ..compat import (compat_str, compat_basestring) | |
a172d962 | 9 | |
a172d962 | 10 | |
2ca1c5aa | 11 | class DouyuTVIE(InfoExtractor): |
513cbdda | 12 | IE_DESC = '斗鱼' |
a172d962 | 13 | _VALID_URL = r'http://(?:www\.)?douyutv\.com/(?P<id>[A-Za-z0-9]+)' |
8343a033 | 14 | _TESTS = [{ |
a172d962 | 15 | 'url': 'http://www.douyutv.com/iseven', |
16 | 'info_dict': { | |
8343a033 YCH |
17 | 'id': '17732', |
18 | 'display_id': 'iseven', | |
a172d962 | 19 | 'ext': 'flv', |
2ca1c5aa | 20 | 'title': 're:^清晨醒脑!T-ara根本停不下来! [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$', |
24ca0e9c | 21 | 'description': 'md5:f34981259a03e980a3c6404190a3ed61', |
a172d962 | 22 | 'thumbnail': 're:^https?://.*\.jpg$', |
2ca1c5aa S |
23 | 'uploader': '7师傅', |
24 | 'uploader_id': '431925', | |
a172d962 | 25 | 'is_live': True, |
2ca1c5aa S |
26 | }, |
27 | 'params': { | |
28 | 'skip_download': True, | |
24ca0e9c | 29 | }, |
8343a033 YCH |
30 | }, { |
31 | 'url': 'http://www.douyutv.com/85982', | |
32 | 'info_dict': { | |
33 | 'id': '85982', | |
34 | 'display_id': '85982', | |
35 | 'ext': 'flv', | |
36 | 'title': 're:^小漠从零单排记!——CSOL2躲猫猫 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$', | |
37 | 'description': 'md5:746a2f7a253966a06755a912f0acc0d2', | |
38 | 'thumbnail': 're:^https?://.*\.jpg$', | |
39 | 'uploader': 'douyu小漠', | |
40 | 'uploader_id': '3769985', | |
41 | 'is_live': True, | |
42 | }, | |
43 | 'params': { | |
44 | 'skip_download': True, | |
24ca0e9c YCH |
45 | }, |
46 | 'skip': 'Romm not found', | |
47 | }, { | |
48 | 'url': 'http://www.douyutv.com/17732', | |
49 | 'info_dict': { | |
50 | 'id': '17732', | |
51 | 'display_id': '17732', | |
52 | 'ext': 'flv', | |
53 | 'title': 're:^清晨醒脑!T-ara根本停不下来! [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$', | |
54 | 'description': 'md5:f34981259a03e980a3c6404190a3ed61', | |
55 | 'thumbnail': 're:^https?://.*\.jpg$', | |
56 | 'uploader': '7师傅', | |
57 | 'uploader_id': '431925', | |
58 | 'is_live': True, | |
59 | }, | |
60 | 'params': { | |
61 | 'skip_download': True, | |
62 | }, | |
8343a033 | 63 | }] |
a172d962 | 64 | |
65 | def _real_extract(self, url): | |
66 | video_id = self._match_id(url) | |
a172d962 | 67 | |
8343a033 YCH |
68 | if video_id.isdigit(): |
69 | room_id = video_id | |
70 | else: | |
71 | page = self._download_webpage(url, video_id) | |
72 | room_id = self._html_search_regex( | |
73 | r'"room_id"\s*:\s*(\d+),', page, 'room id') | |
74 | ||
75 | prefix = 'room/%s?aid=android&client_sys=android&time=%d' % ( | |
76 | room_id, int(time.time())) | |
77 | ||
78 | auth = hashlib.md5((prefix + '1231').encode('ascii')).hexdigest() | |
2ca1c5aa | 79 | config = self._download_json( |
8343a033 YCH |
80 | 'http://www.douyutv.com/api/v1/%s&auth=%s' % (prefix, auth), |
81 | video_id) | |
a172d962 | 82 | |
2ca1c5aa S |
83 | data = config['data'] |
84 | ||
85 | error_code = config.get('error', 0) | |
a172d962 | 86 | if error_code is not 0: |
8343a033 | 87 | error_desc = 'Server reported error %i' % error_code |
2ddf0835 | 88 | if isinstance(data, (compat_str, compat_basestring)): |
8343a033 YCH |
89 | error_desc += ': ' + data |
90 | raise ExtractorError(error_desc, expected=True) | |
a172d962 | 91 | |
8343a033 | 92 | show_status = data.get('show_status') |
2ca1c5aa | 93 | # 1 = live, 2 = offline |
a172d962 | 94 | if show_status == '2': |
2ca1c5aa S |
95 | raise ExtractorError( |
96 | 'Live stream is offline', expected=True) | |
97 | ||
98 | base_url = data['rtmp_url'] | |
99 | live_path = data['rtmp_live'] | |
a172d962 | 100 | |
8343a033 | 101 | title = self._live_title(unescapeHTML(data['room_name'])) |
2ca1c5aa S |
102 | description = data.get('show_details') |
103 | thumbnail = data.get('room_src') | |
a172d962 | 104 | |
2ca1c5aa S |
105 | uploader = data.get('nickname') |
106 | uploader_id = data.get('owner_uid') | |
107 | ||
108 | multi_formats = data.get('rtmp_multi_bitrate') | |
109 | if not isinstance(multi_formats, dict): | |
110 | multi_formats = {} | |
111 | multi_formats['live'] = live_path | |
112 | ||
113 | formats = [{ | |
114 | 'url': '%s/%s' % (base_url, format_path), | |
115 | 'format_id': format_id, | |
116 | 'preference': 1 if format_id == 'live' else 0, | |
117 | } for format_id, format_path in multi_formats.items()] | |
118 | self._sort_formats(formats) | |
a172d962 | 119 | |
120 | return { | |
8343a033 YCH |
121 | 'id': room_id, |
122 | 'display_id': video_id, | |
a172d962 | 123 | 'title': title, |
2ca1c5aa | 124 | 'description': description, |
a172d962 | 125 | 'thumbnail': thumbnail, |
2ca1c5aa S |
126 | 'uploader': uploader, |
127 | 'uploader_id': uploader_id, | |
128 | 'formats': formats, | |
a172d962 | 129 | 'is_live': True, |
2ca1c5aa | 130 | } |