]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/pandatv.py
debian/control: Mark my GitHub trees as the preferred repos.
[youtubedl] / youtube_dl / extractor / pandatv.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..utils import (
6 ExtractorError,
7 qualities,
8 )
9
10
11 class PandaTVIE(InfoExtractor):
12 IE_DESC = '熊猫TV'
13 _VALID_URL = r'https?://(?:www\.)?panda\.tv/(?P<id>[0-9]+)'
14 _TESTS = [{
15 'url': 'http://www.panda.tv/66666',
16 'info_dict': {
17 'id': '66666',
18 'title': 're:.+',
19 'uploader': '刘杀鸡',
20 'ext': 'flv',
21 'is_live': True,
22 },
23 'params': {
24 'skip_download': True,
25 },
26 'skip': 'Live stream is offline',
27 }, {
28 'url': 'https://www.panda.tv/66666',
29 'only_matching': True,
30 }]
31
32 def _real_extract(self, url):
33 video_id = self._match_id(url)
34
35 config = self._download_json(
36 'https://www.panda.tv/api_room_v2?roomid=%s' % video_id, video_id)
37
38 error_code = config.get('errno', 0)
39 if error_code != 0:
40 raise ExtractorError(
41 '%s returned error %s: %s'
42 % (self.IE_NAME, error_code, config['errmsg']),
43 expected=True)
44
45 data = config['data']
46 video_info = data['videoinfo']
47
48 # 2 = live, 3 = offline
49 if video_info.get('status') != '2':
50 raise ExtractorError(
51 'Live stream is offline', expected=True)
52
53 title = data['roominfo']['name']
54 uploader = data.get('hostinfo', {}).get('name')
55 room_key = video_info['room_key']
56 stream_addr = video_info.get(
57 'stream_addr', {'OD': '1', 'HD': '1', 'SD': '1'})
58
59 # Reverse engineered from web player swf
60 # (http://s6.pdim.gs/static/07153e425f581151.swf at the moment of
61 # writing).
62 plflag0, plflag1 = video_info['plflag'].split('_')
63 plflag0 = int(plflag0) - 1
64 if plflag1 == '21':
65 plflag0 = 10
66 plflag1 = '4'
67 live_panda = 'live_panda' if plflag0 < 1 else ''
68
69 plflag_auth = self._parse_json(video_info['plflag_list'], video_id)
70 sign = plflag_auth['auth']['sign']
71 ts = plflag_auth['auth']['time']
72 rid = plflag_auth['auth']['rid']
73
74 quality_key = qualities(['OD', 'HD', 'SD'])
75 suffix = ['_small', '_mid', '']
76 formats = []
77 for k, v in stream_addr.items():
78 if v != '1':
79 continue
80 quality = quality_key(k)
81 if quality <= 0:
82 continue
83 for pref, (ext, pl) in enumerate((('m3u8', '-hls'), ('flv', ''))):
84 formats.append({
85 'url': 'https://pl%s%s.live.panda.tv/live_panda/%s%s%s.%s?sign=%s&ts=%s&rid=%s'
86 % (pl, plflag1, room_key, live_panda, suffix[quality], ext, sign, ts, rid),
87 'format_id': '%s-%s' % (k, ext),
88 'quality': quality,
89 'source_preference': pref,
90 })
91 self._sort_formats(formats)
92
93 return {
94 'id': video_id,
95 'title': self._live_title(title),
96 'uploader': uploader,
97 'formats': formats,
98 'is_live': True,
99 }