]>
Commit | Line | Data |
---|---|---|
2e7c8cab ZJ |
1 | # coding: utf-8 |
2 | from __future__ import unicode_literals | |
3 | ||
4 | from .common import InfoExtractor | |
5 | from ..compat import compat_str | |
6 | from ..utils import ( | |
7 | ExtractorError, | |
8 | qualities | |
9 | ) | |
10 | ||
11 | class PandaTVIE(InfoExtractor): | |
12 | IE_DESC = '熊猫TV' | |
13 | _VALID_URL = r'http://(?:www\.)?panda\.tv/(?P<id>[0-9]+)' | |
14 | _TESTS = [{ | |
15 | 'url': 'http://www.panda.tv/10091', | |
16 | 'info_dict': { | |
17 | 'id': '10091', | |
18 | 'title': 're:.+', | |
19 | 'uploader': '囚徒', | |
20 | 'ext': 'flv', | |
21 | 'is_live': True, | |
22 | }, | |
23 | 'params': { | |
24 | 'skip_download': True, | |
25 | }, | |
26 | }] | |
27 | ||
28 | def _real_extract(self, url): | |
29 | video_id = self._match_id(url) | |
30 | ||
31 | config = self._download_json( | |
32 | 'http://www.panda.tv/api_room?roomid=%s' % video_id, | |
33 | video_id | |
34 | ) | |
35 | ||
36 | data = config['data'] | |
37 | ||
38 | error_code = config.get('errno', 0) | |
39 | if error_code is not 0: | |
40 | error_desc = 'Server reported error %i' % error_code | |
41 | if isinstance(data, compat_str): | |
42 | error_desc += ': ' + data | |
43 | raise ExtractorError(error_desc, expected=True) | |
44 | ||
45 | video_info = data['videoinfo'] | |
46 | ||
47 | # 2 = live, 3 = offline | |
48 | if video_info.get('status') != '2': | |
49 | raise ExtractorError( | |
50 | 'Live stream is offline', expected=True) | |
51 | ||
52 | title = data['roominfo']['name'] | |
53 | uploader = data.get('hostinfo', {}).get('name') | |
54 | room_key = video_info['room_key'] | |
55 | stream_addr = video_info.get('stream_addr', {'OD': '1', 'HD': '1', 'SD': '1'}) | |
56 | ||
57 | plflag0, plflag1 = video_info['plflag'].split('_') | |
58 | plflag0 = int(plflag0) - 1 | |
59 | if plflag1 == '21': | |
60 | plflag0 = 10 | |
61 | plflag1 = '4' | |
62 | live_panda = 'live_panda' if plflag0 < 1 else '' | |
63 | ||
64 | quality_key = qualities(['OD', 'HD', 'SD']) | |
65 | suffix = ['_small', '_mid', ''] | |
66 | formats = [] | |
67 | for k, v in stream_addr.items(): | |
68 | if v == '1': | |
69 | quality = quality_key(k) | |
70 | if quality >= 0: | |
71 | formats.append({ | |
72 | 'url': 'http://pl%s.live.panda.tv/live_panda/%s%s%s.flv' % (plflag1, room_key, live_panda, suffix[quality]), | |
73 | 'format_id': k, | |
74 | 'quality': quality, | |
75 | }) | |
76 | self._sort_formats(formats) | |
77 | ||
78 | return { | |
79 | 'id': video_id, | |
80 | 'title': self._live_title(title), | |
81 | 'uploader': uploader, | |
82 | 'formats': formats, | |
83 | 'is_live': True, | |
84 | } |