You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
3.1 KiB

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