picarto.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. from .common import InfoExtractor
  2. from ..utils import (
  3. ExtractorError,
  4. js_to_json,
  5. )
  6. class PicartoIE(InfoExtractor):
  7. _VALID_URL = r'https?://(?:www.)?picarto\.tv/(?P<id>[a-zA-Z0-9]+)'
  8. _TEST = {
  9. 'url': 'https://picarto.tv/Setz',
  10. 'info_dict': {
  11. 'id': 'Setz',
  12. 'ext': 'mp4',
  13. 'title': 're:^Setz [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
  14. 'timestamp': int,
  15. 'is_live': True
  16. },
  17. 'skip': 'Stream is offline',
  18. }
  19. @classmethod
  20. def suitable(cls, url):
  21. return False if PicartoVodIE.suitable(url) else super(PicartoIE, cls).suitable(url)
  22. def _real_extract(self, url):
  23. channel_id = self._match_id(url)
  24. data = self._download_json(
  25. 'https://ptvintern.picarto.tv/ptvapi', channel_id, query={
  26. 'query': '''{
  27. channel(name: "%s") {
  28. adult
  29. id
  30. online
  31. stream_name
  32. title
  33. }
  34. getLoadBalancerUrl(channel_name: "%s") {
  35. url
  36. }
  37. }''' % (channel_id, channel_id),
  38. })['data']
  39. metadata = data['channel']
  40. if metadata.get('online') == 0:
  41. raise ExtractorError('Stream is offline', expected=True)
  42. title = metadata['title']
  43. cdn_data = self._download_json(
  44. data['getLoadBalancerUrl']['url'] + '/stream/json_' + metadata['stream_name'] + '.js',
  45. channel_id, 'Downloading load balancing info')
  46. formats = []
  47. for source in (cdn_data.get('source') or []):
  48. source_url = source.get('url')
  49. if not source_url:
  50. continue
  51. source_type = source.get('type')
  52. if source_type == 'html5/application/vnd.apple.mpegurl':
  53. formats.extend(self._extract_m3u8_formats(
  54. source_url, channel_id, 'mp4', m3u8_id='hls', fatal=False))
  55. elif source_type == 'html5/video/mp4':
  56. formats.append({
  57. 'url': source_url,
  58. })
  59. mature = metadata.get('adult')
  60. if mature is None:
  61. age_limit = None
  62. else:
  63. age_limit = 18 if mature is True else 0
  64. return {
  65. 'id': channel_id,
  66. 'title': title.strip(),
  67. 'is_live': True,
  68. 'channel': channel_id,
  69. 'channel_id': metadata.get('id'),
  70. 'channel_url': 'https://picarto.tv/%s' % channel_id,
  71. 'age_limit': age_limit,
  72. 'formats': formats,
  73. }
  74. class PicartoVodIE(InfoExtractor):
  75. _VALID_URL = r'https?://(?:www.)?picarto\.tv/videopopout/(?P<id>[^/?#&]+)'
  76. _TESTS = [{
  77. 'url': 'https://picarto.tv/videopopout/ArtofZod_2017.12.12.00.13.23.flv',
  78. 'md5': '3ab45ba4352c52ee841a28fb73f2d9ca',
  79. 'info_dict': {
  80. 'id': 'ArtofZod_2017.12.12.00.13.23.flv',
  81. 'ext': 'mp4',
  82. 'title': 'ArtofZod_2017.12.12.00.13.23.flv',
  83. 'thumbnail': r're:^https?://.*\.jpg'
  84. },
  85. }, {
  86. 'url': 'https://picarto.tv/videopopout/Plague',
  87. 'only_matching': True,
  88. }]
  89. def _real_extract(self, url):
  90. video_id = self._match_id(url)
  91. webpage = self._download_webpage(url, video_id)
  92. vod_info = self._parse_json(
  93. self._search_regex(
  94. r'(?s)#vod-player["\']\s*,\s*(\{.+?\})\s*\)', webpage,
  95. 'vod player'),
  96. video_id, transform_source=js_to_json)
  97. formats = self._extract_m3u8_formats(
  98. vod_info['vod'], video_id, 'mp4', entry_protocol='m3u8_native',
  99. m3u8_id='hls')
  100. return {
  101. 'id': video_id,
  102. 'title': video_id,
  103. 'thumbnail': vod_info.get('vodThumb'),
  104. 'formats': formats,
  105. }