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