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.

153 lines
5.1 KiB

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