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