]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/douyutv.py
Imported Upstream version 2015.07.21
[youtubedl] / youtube_dl / extractor / douyutv.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import hashlib
5 import time
6 from .common import InfoExtractor
7 from ..utils import (ExtractorError, unescapeHTML)
8 from ..compat import (compat_str, compat_basestring)
9
10
11 class DouyuTVIE(InfoExtractor):
12 IE_DESC = '斗鱼'
13 _VALID_URL = r'http://(?:www\.)?douyutv\.com/(?P<id>[A-Za-z0-9]+)'
14 _TESTS = [{
15 'url': 'http://www.douyutv.com/iseven',
16 'info_dict': {
17 'id': '17732',
18 'display_id': 'iseven',
19 'ext': 'flv',
20 'title': 're:^清晨醒脑!T-ara根本停不下来! [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
21 'description': 'md5:c93d6692dde6fe33809a46edcbecca44',
22 'thumbnail': 're:^https?://.*\.jpg$',
23 'uploader': '7师傅',
24 'uploader_id': '431925',
25 'is_live': True,
26 },
27 'params': {
28 'skip_download': True,
29 }
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,
45 }
46 }]
47
48 def _real_extract(self, url):
49 video_id = self._match_id(url)
50
51 if video_id.isdigit():
52 room_id = video_id
53 else:
54 page = self._download_webpage(url, video_id)
55 room_id = self._html_search_regex(
56 r'"room_id"\s*:\s*(\d+),', page, 'room id')
57
58 prefix = 'room/%s?aid=android&client_sys=android&time=%d' % (
59 room_id, int(time.time()))
60
61 auth = hashlib.md5((prefix + '1231').encode('ascii')).hexdigest()
62 config = self._download_json(
63 'http://www.douyutv.com/api/v1/%s&auth=%s' % (prefix, auth),
64 video_id)
65
66 data = config['data']
67
68 error_code = config.get('error', 0)
69 if error_code is not 0:
70 error_desc = 'Server reported error %i' % error_code
71 if isinstance(data, (compat_str, compat_basestring)):
72 error_desc += ': ' + data
73 raise ExtractorError(error_desc, expected=True)
74
75 show_status = data.get('show_status')
76 # 1 = live, 2 = offline
77 if show_status == '2':
78 raise ExtractorError(
79 'Live stream is offline', expected=True)
80
81 base_url = data['rtmp_url']
82 live_path = data['rtmp_live']
83
84 title = self._live_title(unescapeHTML(data['room_name']))
85 description = data.get('show_details')
86 thumbnail = data.get('room_src')
87
88 uploader = data.get('nickname')
89 uploader_id = data.get('owner_uid')
90
91 multi_formats = data.get('rtmp_multi_bitrate')
92 if not isinstance(multi_formats, dict):
93 multi_formats = {}
94 multi_formats['live'] = live_path
95
96 formats = [{
97 'url': '%s/%s' % (base_url, format_path),
98 'format_id': format_id,
99 'preference': 1 if format_id == 'live' else 0,
100 } for format_id, format_path in multi_formats.items()]
101 self._sort_formats(formats)
102
103 return {
104 'id': room_id,
105 'display_id': video_id,
106 'title': title,
107 'description': description,
108 'thumbnail': thumbnail,
109 'uploader': uploader,
110 'uploader_id': uploader_id,
111 'formats': formats,
112 'is_live': True,
113 }