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.

165 lines
5.5 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import time
  4. from .common import InfoExtractor
  5. from ..compat import compat_str
  6. from ..utils import (
  7. ExtractorError,
  8. js_to_json,
  9. try_get,
  10. update_url_query,
  11. urlencode_postdata,
  12. )
  13. class PicartoIE(InfoExtractor):
  14. _VALID_URL = r'https?://(?:www.)?picarto\.tv/(?P<id>[a-zA-Z0-9]+)'
  15. _TEST = {
  16. 'url': 'https://picarto.tv/Setz',
  17. 'info_dict': {
  18. 'id': 'Setz',
  19. 'ext': 'mp4',
  20. 'title': 're:^Setz [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
  21. 'timestamp': int,
  22. 'is_live': True
  23. },
  24. 'skip': 'Stream is offline',
  25. }
  26. @classmethod
  27. def suitable(cls, url):
  28. return False if PicartoVodIE.suitable(url) else super(PicartoIE, cls).suitable(url)
  29. def _real_extract(self, url):
  30. channel_id = self._match_id(url)
  31. stream_page = self._download_webpage(url, channel_id)
  32. if '>This channel does not exist' in stream_page:
  33. raise ExtractorError(
  34. 'Channel %s does not exist' % channel_id, expected=True)
  35. player = self._parse_json(
  36. self._search_regex(
  37. r'(?s)playerSettings\[\d+\]\s*=\s*(\{.+?\}\s*\n)', stream_page,
  38. 'player settings'),
  39. channel_id, transform_source=js_to_json)
  40. if player.get('online') is False:
  41. raise ExtractorError('Stream is offline', expected=True)
  42. cdn_data = self._download_json(
  43. 'https://picarto.tv/process/channel', channel_id,
  44. data=urlencode_postdata({'loadbalancinginfo': channel_id}),
  45. note='Downloading load balancing info')
  46. def get_event(key):
  47. return try_get(player, lambda x: x['event'][key], compat_str) or ''
  48. params = {
  49. 'token': player.get('token') or '',
  50. 'ticket': get_event('ticket'),
  51. 'con': int(time.time() * 1000),
  52. 'type': get_event('ticket'),
  53. 'scope': get_event('scope'),
  54. }
  55. prefered_edge = cdn_data.get('preferedEdge')
  56. default_tech = player.get('defaultTech')
  57. formats = []
  58. for edge in cdn_data['edges']:
  59. edge_ep = edge.get('ep')
  60. if not edge_ep or not isinstance(edge_ep, compat_str):
  61. continue
  62. edge_id = edge.get('id')
  63. for tech in cdn_data['techs']:
  64. tech_label = tech.get('label')
  65. tech_type = tech.get('type')
  66. preference = 0
  67. if edge_id == prefered_edge:
  68. preference += 1
  69. if tech_type == default_tech:
  70. preference += 1
  71. format_id = []
  72. if edge_id:
  73. format_id.append(edge_id)
  74. if tech_type == 'application/x-mpegurl' or tech_label == 'HLS':
  75. format_id.append('hls')
  76. formats.extend(self._extract_m3u8_formats(
  77. update_url_query(
  78. 'https://%s/hls/%s/index.m3u8'
  79. % (edge_ep, channel_id), params),
  80. channel_id, 'mp4', preference=preference,
  81. m3u8_id='-'.join(format_id), fatal=False))
  82. continue
  83. elif tech_type == 'video/mp4' or tech_label == 'MP4':
  84. format_id.append('mp4')
  85. formats.append({
  86. 'url': update_url_query(
  87. 'https://%s/mp4/%s.mp4' % (edge_ep, channel_id),
  88. params),
  89. 'format_id': '-'.join(format_id),
  90. 'preference': preference,
  91. })
  92. else:
  93. # rtmp format does not seem to work
  94. continue
  95. self._sort_formats(formats)
  96. mature = player.get('mature')
  97. if mature is None:
  98. age_limit = None
  99. else:
  100. age_limit = 18 if mature is True else 0
  101. return {
  102. 'id': channel_id,
  103. 'title': self._live_title(channel_id),
  104. 'is_live': True,
  105. 'thumbnail': player.get('vodThumb'),
  106. 'age_limit': age_limit,
  107. 'formats': formats,
  108. }
  109. class PicartoVodIE(InfoExtractor):
  110. _VALID_URL = r'https?://(?:www.)?picarto\.tv/videopopout/(?P<id>[^/?#&]+)'
  111. _TESTS = [{
  112. 'url': 'https://picarto.tv/videopopout/ArtofZod_2017.12.12.00.13.23.flv',
  113. 'md5': '3ab45ba4352c52ee841a28fb73f2d9ca',
  114. 'info_dict': {
  115. 'id': 'ArtofZod_2017.12.12.00.13.23.flv',
  116. 'ext': 'mp4',
  117. 'title': 'ArtofZod_2017.12.12.00.13.23.flv',
  118. 'thumbnail': r're:^https?://.*\.jpg'
  119. },
  120. }, {
  121. 'url': 'https://picarto.tv/videopopout/Plague',
  122. 'only_matching': True,
  123. }]
  124. def _real_extract(self, url):
  125. video_id = self._match_id(url)
  126. webpage = self._download_webpage(url, video_id)
  127. vod_info = self._parse_json(
  128. self._search_regex(
  129. r'(?s)#vod-player["\']\s*,\s*(\{.+?\})\s*\)', webpage,
  130. video_id),
  131. video_id, transform_source=js_to_json)
  132. formats = self._extract_m3u8_formats(
  133. vod_info['vod'], video_id, 'mp4', entry_protocol='m3u8_native',
  134. m3u8_id='hls')
  135. self._sort_formats(formats)
  136. return {
  137. 'id': video_id,
  138. 'title': video_id,
  139. 'thumbnail': vod_info.get('vodThumb'),
  140. 'formats': formats,
  141. }