1
0
mirror of https://codeberg.org/polarisfm/youtube-dl synced 2024-12-22 22:47:55 +01:00
youtube-dl/youtube_dl/extractor/pandatv.py

100 lines
3.1 KiB
Python
Raw Normal View History

2016-03-09 16:43:27 +01:00
# coding: utf-8
from __future__ import unicode_literals
from .common import InfoExtractor
from ..utils import (
ExtractorError,
qualities,
2016-03-09 16:43:27 +01:00
)
2016-03-09 16:43:27 +01:00
class PandaTVIE(InfoExtractor):
IE_DESC = '熊猫TV'
2017-06-26 17:00:31 +02:00
_VALID_URL = r'https?://(?:www\.)?panda\.tv/(?P<id>[0-9]+)'
_TESTS = [{
'url': 'http://www.panda.tv/66666',
2016-03-09 16:43:27 +01:00
'info_dict': {
2017-06-26 17:00:31 +02:00
'id': '66666',
2016-03-09 16:43:27 +01:00
'title': 're:.+',
2017-06-26 17:00:31 +02:00
'uploader': '刘杀鸡',
2016-03-09 16:43:27 +01:00
'ext': 'flv',
'is_live': True,
},
'params': {
'skip_download': True,
},
'skip': 'Live stream is offline',
2017-06-26 17:00:31 +02:00
}, {
'url': 'https://www.panda.tv/66666',
'only_matching': True,
}]
2016-03-09 16:43:27 +01:00
def _real_extract(self, url):
video_id = self._match_id(url)
config = self._download_json(
'https://www.panda.tv/api_room_v2?roomid=%s' % video_id, video_id)
2016-03-09 16:43:27 +01:00
error_code = config.get('errno', 0)
2019-03-11 15:37:19 +01:00
if error_code != 0:
raise ExtractorError(
'%s returned error %s: %s'
% (self.IE_NAME, error_code, config['errmsg']),
expected=True)
2016-03-09 16:43:27 +01:00
data = config['data']
2016-03-09 16:43:27 +01:00
video_info = data['videoinfo']
# 2 = live, 3 = offline
if video_info.get('status') != '2':
raise ExtractorError(
'Live stream is offline', expected=True)
title = data['roominfo']['name']
uploader = data.get('hostinfo', {}).get('name')
room_key = video_info['room_key']
stream_addr = video_info.get(
'stream_addr', {'OD': '1', 'HD': '1', 'SD': '1'})
2016-03-09 16:43:27 +01:00
# Reverse engineered from web player swf
# (http://s6.pdim.gs/static/07153e425f581151.swf at the moment of
# writing).
2016-03-09 16:43:27 +01:00
plflag0, plflag1 = video_info['plflag'].split('_')
plflag0 = int(plflag0) - 1
if plflag1 == '21':
plflag0 = 10
plflag1 = '4'
live_panda = 'live_panda' if plflag0 < 1 else ''
2017-11-09 17:25:43 +01:00
plflag_auth = self._parse_json(video_info['plflag_list'], video_id)
sign = plflag_auth['auth']['sign']
ts = plflag_auth['auth']['time']
rid = plflag_auth['auth']['rid']
2016-03-09 16:43:27 +01:00
quality_key = qualities(['OD', 'HD', 'SD'])
suffix = ['_small', '_mid', '']
formats = []
for k, v in stream_addr.items():
if v != '1':
continue
quality = quality_key(k)
if quality <= 0:
continue
for pref, (ext, pl) in enumerate((('m3u8', '-hls'), ('flv', ''))):
formats.append({
'url': 'https://pl%s%s.live.panda.tv/live_panda/%s%s%s.%s?sign=%s&ts=%s&rid=%s'
% (pl, plflag1, room_key, live_panda, suffix[quality], ext, sign, ts, rid),
'format_id': '%s-%s' % (k, ext),
'quality': quality,
'source_preference': pref,
})
2016-03-09 16:43:27 +01:00
self._sort_formats(formats)
return {
'id': video_id,
'title': self._live_title(title),
'uploader': uploader,
'formats': formats,
'is_live': True,
}