logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

picarto.py (4833B)


  1. import urllib.parse
  2. from .common import InfoExtractor
  3. from ..utils import (
  4. ExtractorError,
  5. str_or_none,
  6. traverse_obj,
  7. update_url,
  8. )
  9. class PicartoIE(InfoExtractor):
  10. _VALID_URL = r'https?://(?:www.)?picarto\.tv/(?P<id>[a-zA-Z0-9]+)'
  11. _TEST = {
  12. 'url': 'https://picarto.tv/Setz',
  13. 'info_dict': {
  14. 'id': 'Setz',
  15. 'ext': 'mp4',
  16. 'title': 're:^Setz [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
  17. 'timestamp': int,
  18. 'is_live': True,
  19. },
  20. 'skip': 'Stream is offline',
  21. }
  22. @classmethod
  23. def suitable(cls, url):
  24. return False if PicartoVodIE.suitable(url) else super().suitable(url)
  25. def _real_extract(self, url):
  26. channel_id = self._match_id(url)
  27. data = self._download_json(
  28. 'https://ptvintern.picarto.tv/ptvapi', channel_id, query={
  29. 'query': '''{
  30. channel(name: "%s") {
  31. adult
  32. id
  33. online
  34. stream_name
  35. title
  36. }
  37. getLoadBalancerUrl(channel_name: "%s") {
  38. url
  39. }
  40. }''' % (channel_id, channel_id), # noqa: UP031
  41. }, headers={'Accept': '*/*', 'Content-Type': 'application/json'})['data']
  42. metadata = data['channel']
  43. if metadata.get('online') == 0:
  44. raise ExtractorError('Stream is offline', expected=True)
  45. title = metadata['title']
  46. cdn_data = self._download_json(''.join((
  47. update_url(data['getLoadBalancerUrl']['url'], scheme='https'),
  48. '/stream/json_', metadata['stream_name'], '.js')),
  49. channel_id, 'Downloading load balancing info')
  50. formats = []
  51. for source in (cdn_data.get('source') or []):
  52. source_url = source.get('url')
  53. if not source_url:
  54. continue
  55. source_type = source.get('type')
  56. if source_type == 'html5/application/vnd.apple.mpegurl':
  57. formats.extend(self._extract_m3u8_formats(
  58. source_url, channel_id, 'mp4', m3u8_id='hls', fatal=False))
  59. elif source_type == 'html5/video/mp4':
  60. formats.append({
  61. 'url': source_url,
  62. })
  63. mature = metadata.get('adult')
  64. if mature is None:
  65. age_limit = None
  66. else:
  67. age_limit = 18 if mature is True else 0
  68. return {
  69. 'id': channel_id,
  70. 'title': title.strip(),
  71. 'is_live': True,
  72. 'channel': channel_id,
  73. 'channel_id': metadata.get('id'),
  74. 'channel_url': f'https://picarto.tv/{channel_id}',
  75. 'age_limit': age_limit,
  76. 'formats': formats,
  77. }
  78. class PicartoVodIE(InfoExtractor):
  79. _VALID_URL = r'https?://(?:www\.)?picarto\.tv/(?:videopopout|\w+/videos)/(?P<id>[^/?#&]+)'
  80. _TESTS = [{
  81. 'url': 'https://picarto.tv/videopopout/ArtofZod_2017.12.12.00.13.23.flv',
  82. 'md5': '3ab45ba4352c52ee841a28fb73f2d9ca',
  83. 'info_dict': {
  84. 'id': 'ArtofZod_2017.12.12.00.13.23.flv',
  85. 'ext': 'mp4',
  86. 'title': 'ArtofZod_2017.12.12.00.13.23.flv',
  87. 'thumbnail': r're:^https?://.*\.jpg',
  88. },
  89. 'skip': 'The VOD does not exist',
  90. }, {
  91. 'url': 'https://picarto.tv/ArtofZod/videos/771008',
  92. 'md5': 'abef5322f2700d967720c4c6754b2a34',
  93. 'info_dict': {
  94. 'id': '771008',
  95. 'ext': 'mp4',
  96. 'title': 'Art of Zod - Drawing and Painting',
  97. 'thumbnail': r're:^https?://.*\.jpg',
  98. 'channel': 'ArtofZod',
  99. 'age_limit': 18,
  100. },
  101. }, {
  102. 'url': 'https://picarto.tv/videopopout/Plague',
  103. 'only_matching': True,
  104. }]
  105. def _real_extract(self, url):
  106. video_id = self._match_id(url)
  107. data = self._download_json(
  108. 'https://ptvintern.picarto.tv/ptvapi', video_id, query={
  109. 'query': f'''{{
  110. video(id: "{video_id}") {{
  111. id
  112. title
  113. adult
  114. file_name
  115. video_recording_image_url
  116. channel {{
  117. name
  118. }}
  119. }}
  120. }}''',
  121. }, headers={'Accept': '*/*', 'Content-Type': 'application/json'})['data']['video']
  122. file_name = data['file_name']
  123. netloc = urllib.parse.urlparse(data['video_recording_image_url']).netloc
  124. formats = self._extract_m3u8_formats(
  125. f'https://{netloc}/stream/hls/{file_name}/index.m3u8', video_id, 'mp4', m3u8_id='hls')
  126. return {
  127. 'id': video_id,
  128. **traverse_obj(data, {
  129. 'id': ('id', {str_or_none}),
  130. 'title': ('title', {str}),
  131. 'thumbnail': 'video_recording_image_url',
  132. 'channel': ('channel', 'name', {str}),
  133. 'age_limit': ('adult', {lambda x: 18 if x else 0}),
  134. }),
  135. 'formats': formats,
  136. }