logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

picarto.py (3918B)


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