]> git.bitcoin.ninja Git - youtube-dl/blob - youtube_dl/extractor/douyutv.py
[SpankBang] Use python2.6 compatible string formatting spec
[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 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     _VALID_URL = r'http://(?:www\.)?douyutv\.com/(?P<id>[A-Za-z0-9]+)'
13     _TESTS = [{
14         'url': 'http://www.douyutv.com/iseven',
15         'info_dict': {
16             'id': '17732',
17             'display_id': 'iseven',
18             'ext': 'flv',
19             'title': 're:^清晨醒脑!T-ara根本停不下来! [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
20             'description': 'md5:c93d6692dde6fe33809a46edcbecca44',
21             'thumbnail': 're:^https?://.*\.jpg$',
22             'uploader': '7师傅',
23             'uploader_id': '431925',
24             'is_live': True,
25         },
26         'params': {
27             'skip_download': True,
28         }
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     }]
46
47     def _real_extract(self, url):
48         video_id = self._match_id(url)
49
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()
61         config = self._download_json(
62             'http://www.douyutv.com/api/v1/%s&auth=%s' % (prefix, auth),
63             video_id)
64
65         data = config['data']
66
67         error_code = config.get('error', 0)
68         if error_code is not 0:
69             error_desc = 'Server reported error %i' % error_code
70             if isinstance(data, (compat_str, compat_basestring)):
71                 error_desc += ': ' + data
72             raise ExtractorError(error_desc, expected=True)
73
74         show_status = data.get('show_status')
75         # 1 = live, 2 = offline
76         if show_status == '2':
77             raise ExtractorError(
78                 'Live stream is offline', expected=True)
79
80         base_url = data['rtmp_url']
81         live_path = data['rtmp_live']
82
83         title = self._live_title(unescapeHTML(data['room_name']))
84         description = data.get('show_details')
85         thumbnail = data.get('room_src')
86
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)
101
102         return {
103             'id': room_id,
104             'display_id': video_id,
105             'title': title,
106             'description': description,
107             'thumbnail': thumbnail,
108             'uploader': uploader,
109             'uploader_id': uploader_id,
110             'formats': formats,
111             'is_live': True,
112         }