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.

91 lines
2.8 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'http://(?:www\.)?panda\.tv/(?P<id>[0-9]+)'
  11. _TEST = {
  12. 'url': 'http://www.panda.tv/10091',
  13. 'info_dict': {
  14. 'id': '10091',
  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. def _real_extract(self, url):
  26. video_id = self._match_id(url)
  27. config = self._download_json(
  28. 'http://www.panda.tv/api_room?roomid=%s' % video_id, video_id)
  29. error_code = config.get('errno', 0)
  30. if error_code is not 0:
  31. raise ExtractorError(
  32. '%s returned error %s: %s'
  33. % (self.IE_NAME, error_code, config['errmsg']),
  34. expected=True)
  35. data = config['data']
  36. video_info = data['videoinfo']
  37. # 2 = live, 3 = offline
  38. if video_info.get('status') != '2':
  39. raise ExtractorError(
  40. 'Live stream is offline', expected=True)
  41. title = data['roominfo']['name']
  42. uploader = data.get('hostinfo', {}).get('name')
  43. room_key = video_info['room_key']
  44. stream_addr = video_info.get(
  45. 'stream_addr', {'OD': '1', 'HD': '1', 'SD': '1'})
  46. # Reverse engineered from web player swf
  47. # (http://s6.pdim.gs/static/07153e425f581151.swf at the moment of
  48. # writing).
  49. plflag0, plflag1 = video_info['plflag'].split('_')
  50. plflag0 = int(plflag0) - 1
  51. if plflag1 == '21':
  52. plflag0 = 10
  53. plflag1 = '4'
  54. live_panda = 'live_panda' if plflag0 < 1 else ''
  55. quality_key = qualities(['OD', 'HD', 'SD'])
  56. suffix = ['_small', '_mid', '']
  57. formats = []
  58. for k, v in stream_addr.items():
  59. if v != '1':
  60. continue
  61. quality = quality_key(k)
  62. if quality <= 0:
  63. continue
  64. for pref, (ext, pl) in enumerate((('m3u8', '-hls'), ('flv', ''))):
  65. formats.append({
  66. 'url': 'http://pl%s%s.live.panda.tv/live_panda/%s%s%s.%s'
  67. % (pl, plflag1, room_key, live_panda, suffix[quality], ext),
  68. 'format_id': '%s-%s' % (k, ext),
  69. 'quality': quality,
  70. 'source_preference': pref,
  71. })
  72. self._sort_formats(formats)
  73. return {
  74. 'id': video_id,
  75. 'title': self._live_title(title),
  76. 'uploader': uploader,
  77. 'formats': formats,
  78. 'is_live': True,
  79. }