]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/douyutv.py
Merge tag 'upstream/2017.03.07'
[youtubedl] / youtube_dl / extractor / douyutv.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import time
5 import hashlib
6
7 from .common import InfoExtractor
8 from ..utils import (
9 ExtractorError,
10 unescapeHTML,
11 )
12
13
14 class DouyuTVIE(InfoExtractor):
15 IE_DESC = '斗鱼'
16 _VALID_URL = r'https?://(?:www\.)?douyu(?:tv)?\.com/(?:[^/]+/)*(?P<id>[A-Za-z0-9]+)'
17 _TESTS = [{
18 'url': 'http://www.douyutv.com/iseven',
19 'info_dict': {
20 'id': '17732',
21 'display_id': 'iseven',
22 'ext': 'flv',
23 'title': 're:^清晨醒脑!T-ARA根本停不下来! [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
24 'description': r're:.*m7show@163\.com.*',
25 'thumbnail': r're:^https?://.*\.jpg$',
26 'uploader': '7师傅',
27 'is_live': True,
28 },
29 'params': {
30 'skip_download': True,
31 },
32 }, {
33 'url': 'http://www.douyutv.com/85982',
34 'info_dict': {
35 'id': '85982',
36 'display_id': '85982',
37 'ext': 'flv',
38 'title': 're:^小漠从零单排记!——CSOL2躲猫猫 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
39 'description': 'md5:746a2f7a253966a06755a912f0acc0d2',
40 'thumbnail': r're:^https?://.*\.jpg$',
41 'uploader': 'douyu小漠',
42 'is_live': True,
43 },
44 'params': {
45 'skip_download': True,
46 },
47 'skip': 'Room not found',
48 }, {
49 'url': 'http://www.douyutv.com/17732',
50 'info_dict': {
51 'id': '17732',
52 'display_id': '17732',
53 'ext': 'flv',
54 'title': 're:^清晨醒脑!T-ARA根本停不下来! [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
55 'description': r're:.*m7show@163\.com.*',
56 'thumbnail': r're:^https?://.*\.jpg$',
57 'uploader': '7师傅',
58 'is_live': True,
59 },
60 'params': {
61 'skip_download': True,
62 },
63 }, {
64 'url': 'http://www.douyu.com/xiaocang',
65 'only_matching': True,
66 }, {
67 # \"room_id\"
68 'url': 'http://www.douyu.com/t/lpl',
69 'only_matching': True,
70 }]
71
72 def _real_extract(self, url):
73 video_id = self._match_id(url)
74
75 if video_id.isdigit():
76 room_id = video_id
77 else:
78 page = self._download_webpage(url, video_id)
79 room_id = self._html_search_regex(
80 r'"room_id\\?"\s*:\s*(\d+),', page, 'room id')
81
82 # Grab metadata from mobile API
83 room = self._download_json(
84 'http://m.douyu.com/html5/live?roomId=%s' % room_id, video_id,
85 note='Downloading room info')['data']
86
87 # 1 = live, 2 = offline
88 if room.get('show_status') == '2':
89 raise ExtractorError('Live stream is offline', expected=True)
90
91 # Grab the URL from PC client API
92 # The m3u8 url from mobile API requires re-authentication every 5 minutes
93 tt = int(time.time())
94 signContent = 'lapi/live/thirdPart/getPlay/%s?aid=pcclient&rate=0&time=%d9TUk5fjjUjg9qIMH3sdnh' % (room_id, tt)
95 sign = hashlib.md5(signContent.encode('ascii')).hexdigest()
96 video_url = self._download_json(
97 'http://coapi.douyucdn.cn/lapi/live/thirdPart/getPlay/' + room_id,
98 video_id, note='Downloading video URL info',
99 query={'rate': 0}, headers={
100 'auth': sign,
101 'time': str(tt),
102 'aid': 'pcclient'
103 })['data']['live_url']
104
105 title = self._live_title(unescapeHTML(room['room_name']))
106 description = room.get('show_details')
107 thumbnail = room.get('room_src')
108 uploader = room.get('nickname')
109
110 return {
111 'id': room_id,
112 'display_id': video_id,
113 'url': video_url,
114 'title': title,
115 'description': description,
116 'thumbnail': thumbnail,
117 'uploader': uploader,
118 'is_live': True,
119 }