[douyu] Fix extraction and update _TESTS
[youtube-dl] / youtube_dl / extractor / douyutv.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..utils import (
6     ExtractorError,
7     unescapeHTML,
8 )
9
10
11 class DouyuTVIE(InfoExtractor):
12     IE_DESC = '斗鱼'
13     _VALID_URL = r'https?://(?:www\.)?douyu(?:tv)?\.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': 'mp4',
20             'title': 're:^清晨醒脑!T-ARA根本停不下来! [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
21             'description': r're:.*m7show@163\.com.*',
22             'thumbnail': r're:^https?://.*\.jpg$',
23             'uploader': '7师傅',
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': 'mp4',
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': r're:^https?://.*\.jpg$',
38             'uploader': 'douyu小漠',
39             'is_live': True,
40         },
41         'params': {
42             'skip_download': True,
43         },
44         'skip': 'Room not found',
45     }, {
46         'url': 'http://www.douyutv.com/17732',
47         'info_dict': {
48             'id': '17732',
49             'display_id': '17732',
50             'ext': 'mp4',
51             'title': 're:^清晨醒脑!T-ARA根本停不下来! [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
52             'description': r're:.*m7show@163\.com.*',
53             'thumbnail': r're:^https?://.*\.jpg$',
54             'uploader': '7师傅',
55             'is_live': True,
56         },
57         'params': {
58             'skip_download': True,
59         },
60     }, {
61         'url': 'http://www.douyu.com/xiaocang',
62         'only_matching': True,
63     }, {
64         # \"room_id\"
65         'url': 'http://www.douyu.com/t/lpl',
66         'only_matching': True,
67     }]
68
69     # Decompile core.swf in webpage by ffdec "Search SWFs in memory". core.swf
70     # is encrypted originally, but ffdec can dump memory to get the decrypted one.
71     _API_KEY = 'A12Svb&%1UUmf@hC'
72
73     def _real_extract(self, url):
74         video_id = self._match_id(url)
75
76         if video_id.isdigit():
77             room_id = video_id
78         else:
79             page = self._download_webpage(url, video_id)
80             room_id = self._html_search_regex(
81                 r'"room_id\\?"\s*:\s*(\d+),', page, 'room id')
82
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         formats = self._extract_m3u8_formats(
92             room['hls_url'], video_id, ext='mp4')
93
94         title = self._live_title(unescapeHTML(room['room_name']))
95         description = room.get('show_details')
96         thumbnail = room.get('room_src')
97         uploader = room.get('nickname')
98
99         return {
100             'id': room_id,
101             'display_id': video_id,
102             'formats': formats,
103             'title': title,
104             'description': description,
105             'thumbnail': thumbnail,
106             'uploader': uploader,
107             'is_live': True,
108         }