logo

youtube-dl

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

medaltv.py (4866B)


  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import compat_str
  6. from ..utils import (
  7. ExtractorError,
  8. float_or_none,
  9. int_or_none,
  10. str_or_none,
  11. try_get,
  12. )
  13. class MedalTVIE(InfoExtractor):
  14. _VALID_URL = r'https?://(?:www\.)?medal\.tv/clips/(?P<id>[^/?#&]+)'
  15. _TESTS = [{
  16. 'url': 'https://medal.tv/clips/2mA60jWAGQCBH',
  17. 'md5': '7b07b064331b1cf9e8e5c52a06ae68fa',
  18. 'info_dict': {
  19. 'id': '2mA60jWAGQCBH',
  20. 'ext': 'mp4',
  21. 'title': 'Quad Cold',
  22. 'description': 'Medal,https://medal.tv/desktop/',
  23. 'uploader': 'MowgliSB',
  24. 'timestamp': 1603165266,
  25. 'upload_date': '20201020',
  26. 'uploader_id': '10619174',
  27. }
  28. }, {
  29. 'url': 'https://medal.tv/clips/2um24TWdty0NA',
  30. 'md5': 'b6dc76b78195fff0b4f8bf4a33ec2148',
  31. 'info_dict': {
  32. 'id': '2um24TWdty0NA',
  33. 'ext': 'mp4',
  34. 'title': 'u tk me i tk u bigger',
  35. 'description': 'Medal,https://medal.tv/desktop/',
  36. 'uploader': 'Mimicc',
  37. 'timestamp': 1605580939,
  38. 'upload_date': '20201117',
  39. 'uploader_id': '5156321',
  40. }
  41. }, {
  42. 'url': 'https://medal.tv/clips/37rMeFpryCC-9',
  43. 'only_matching': True,
  44. }, {
  45. 'url': 'https://medal.tv/clips/2WRj40tpY_EU9',
  46. 'only_matching': True,
  47. }]
  48. def _real_extract(self, url):
  49. video_id = self._match_id(url)
  50. webpage = self._download_webpage(url, video_id)
  51. hydration_data = self._parse_json(self._search_regex(
  52. r'<script[^>]*>\s*(?:var\s*)?hydrationData\s*=\s*({.+?})\s*</script>',
  53. webpage, 'hydration data', default='{}'), video_id)
  54. clip = try_get(
  55. hydration_data, lambda x: x['clips'][video_id], dict) or {}
  56. if not clip:
  57. raise ExtractorError(
  58. 'Could not find video information.', video_id=video_id)
  59. title = clip['contentTitle']
  60. source_width = int_or_none(clip.get('sourceWidth'))
  61. source_height = int_or_none(clip.get('sourceHeight'))
  62. aspect_ratio = source_width / source_height if source_width and source_height else 16 / 9
  63. def add_item(container, item_url, height, id_key='format_id', item_id=None):
  64. item_id = item_id or '%dp' % height
  65. if item_id not in item_url:
  66. return
  67. width = int(round(aspect_ratio * height))
  68. container.append({
  69. 'url': item_url,
  70. id_key: item_id,
  71. 'width': width,
  72. 'height': height
  73. })
  74. formats = []
  75. thumbnails = []
  76. for k, v in clip.items():
  77. if not (v and isinstance(v, compat_str)):
  78. continue
  79. mobj = re.match(r'(contentUrl|thumbnail)(?:(\d+)p)?$', k)
  80. if not mobj:
  81. continue
  82. prefix = mobj.group(1)
  83. height = int_or_none(mobj.group(2))
  84. if prefix == 'contentUrl':
  85. add_item(
  86. formats, v, height or source_height,
  87. item_id=None if height else 'source')
  88. elif prefix == 'thumbnail':
  89. add_item(thumbnails, v, height, 'id')
  90. error = clip.get('error')
  91. if not formats and error:
  92. if error == 404:
  93. raise ExtractorError(
  94. 'That clip does not exist.',
  95. expected=True, video_id=video_id)
  96. else:
  97. raise ExtractorError(
  98. 'An unknown error occurred ({0}).'.format(error),
  99. video_id=video_id)
  100. self._sort_formats(formats)
  101. # Necessary because the id of the author is not known in advance.
  102. # Won't raise an issue if no profile can be found as this is optional.
  103. author = try_get(
  104. hydration_data, lambda x: list(x['profiles'].values())[0], dict) or {}
  105. author_id = str_or_none(author.get('id'))
  106. author_url = 'https://medal.tv/users/{0}'.format(author_id) if author_id else None
  107. return {
  108. 'id': video_id,
  109. 'title': title,
  110. 'formats': formats,
  111. 'thumbnails': thumbnails,
  112. 'description': clip.get('contentDescription'),
  113. 'uploader': author.get('displayName'),
  114. 'timestamp': float_or_none(clip.get('created'), 1000),
  115. 'uploader_id': author_id,
  116. 'uploader_url': author_url,
  117. 'duration': int_or_none(clip.get('videoLengthSeconds')),
  118. 'view_count': int_or_none(clip.get('views')),
  119. 'like_count': int_or_none(clip.get('likes')),
  120. 'comment_count': int_or_none(clip.get('comments')),
  121. }