medaltv.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import re
  2. from .common import InfoExtractor
  3. from ..utils import (
  4. ExtractorError,
  5. float_or_none,
  6. format_field,
  7. int_or_none,
  8. str_or_none,
  9. traverse_obj,
  10. )
  11. class MedalTVIE(InfoExtractor):
  12. _VALID_URL = r'https?://(?:www\.)?medal\.tv/games/[^/?#&]+/clips/(?P<id>[^/?#&]+)'
  13. _TESTS = [{
  14. 'url': 'https://medal.tv/games/valorant/clips/jTBFnLKdLy15K',
  15. 'md5': '03e4911fdcf7fce563090705c2e79267',
  16. 'info_dict': {
  17. 'id': 'jTBFnLKdLy15K',
  18. 'ext': 'mp4',
  19. 'title': "Mornu's clutch",
  20. 'description': '',
  21. 'uploader': 'Aciel',
  22. 'timestamp': 1651628243,
  23. 'upload_date': '20220504',
  24. 'uploader_id': '19335460',
  25. 'uploader_url': 'https://medal.tv/users/19335460',
  26. 'comment_count': int,
  27. 'view_count': int,
  28. 'like_count': int,
  29. 'duration': 13,
  30. },
  31. }, {
  32. 'url': 'https://medal.tv/games/cod-cold-war/clips/2mA60jWAGQCBH',
  33. 'md5': 'fc7a3e4552ae8993c1c4006db46be447',
  34. 'info_dict': {
  35. 'id': '2mA60jWAGQCBH',
  36. 'ext': 'mp4',
  37. 'title': 'Quad Cold',
  38. 'description': 'Medal,https://medal.tv/desktop/',
  39. 'uploader': 'MowgliSB',
  40. 'timestamp': 1603165266,
  41. 'upload_date': '20201020',
  42. 'uploader_id': '10619174',
  43. 'thumbnail': 'https://cdn.medal.tv/10619174/thumbnail-34934644-720p.jpg?t=1080p&c=202042&missing',
  44. 'uploader_url': 'https://medal.tv/users/10619174',
  45. 'comment_count': int,
  46. 'view_count': int,
  47. 'like_count': int,
  48. 'duration': 23,
  49. },
  50. }, {
  51. 'url': 'https://medal.tv/games/cod-cold-war/clips/2um24TWdty0NA',
  52. 'md5': 'b6dc76b78195fff0b4f8bf4a33ec2148',
  53. 'info_dict': {
  54. 'id': '2um24TWdty0NA',
  55. 'ext': 'mp4',
  56. 'title': 'u tk me i tk u bigger',
  57. 'description': 'Medal,https://medal.tv/desktop/',
  58. 'uploader': 'Mimicc',
  59. 'timestamp': 1605580939,
  60. 'upload_date': '20201117',
  61. 'uploader_id': '5156321',
  62. 'thumbnail': 'https://cdn.medal.tv/5156321/thumbnail-36787208-360p.jpg?t=1080p&c=202046&missing',
  63. 'uploader_url': 'https://medal.tv/users/5156321',
  64. 'comment_count': int,
  65. 'view_count': int,
  66. 'like_count': int,
  67. 'duration': 9,
  68. },
  69. }, {
  70. 'url': 'https://medal.tv/games/valorant/clips/37rMeFpryCC-9',
  71. 'only_matching': True,
  72. }, {
  73. 'url': 'https://medal.tv/games/valorant/clips/2WRj40tpY_EU9',
  74. 'only_matching': True,
  75. }]
  76. def _real_extract(self, url):
  77. video_id = self._match_id(url)
  78. webpage = self._download_webpage(url, video_id, query={'mobilebypass': 'true'})
  79. hydration_data = self._search_json(
  80. r'<script[^>]*>[^<]*\bhydrationData\s*=', webpage,
  81. 'next data', video_id, end_pattern='</script>', fatal=False)
  82. clip = traverse_obj(hydration_data, ('clips', ...), get_all=False)
  83. if not clip:
  84. raise ExtractorError(
  85. 'Could not find video information.', video_id=video_id)
  86. title = clip['contentTitle']
  87. source_width = int_or_none(clip.get('sourceWidth'))
  88. source_height = int_or_none(clip.get('sourceHeight'))
  89. aspect_ratio = source_width / source_height if source_width and source_height else 16 / 9
  90. def add_item(container, item_url, height, id_key='format_id', item_id=None):
  91. item_id = item_id or '%dp' % height
  92. if item_id not in item_url:
  93. return
  94. width = int(round(aspect_ratio * height))
  95. container.append({
  96. 'url': item_url,
  97. id_key: item_id,
  98. 'width': width,
  99. 'height': height,
  100. })
  101. formats = []
  102. thumbnails = []
  103. for k, v in clip.items():
  104. if not (v and isinstance(v, str)):
  105. continue
  106. mobj = re.match(r'(contentUrl|thumbnail)(?:(\d+)p)?$', k)
  107. if not mobj:
  108. continue
  109. prefix = mobj.group(1)
  110. height = int_or_none(mobj.group(2))
  111. if prefix == 'contentUrl':
  112. add_item(
  113. formats, v, height or source_height,
  114. item_id=None if height else 'source')
  115. elif prefix == 'thumbnail':
  116. add_item(thumbnails, v, height, 'id')
  117. error = clip.get('error')
  118. if not formats and error:
  119. if error == 404:
  120. self.raise_no_formats(
  121. 'That clip does not exist.',
  122. expected=True, video_id=video_id)
  123. else:
  124. self.raise_no_formats(
  125. f'An unknown error occurred ({error}).',
  126. video_id=video_id)
  127. # Necessary because the id of the author is not known in advance.
  128. # Won't raise an issue if no profile can be found as this is optional.
  129. author = traverse_obj(hydration_data, ('profiles', ...), get_all=False) or {}
  130. author_id = str_or_none(author.get('userId'))
  131. author_url = format_field(author_id, None, 'https://medal.tv/users/%s')
  132. return {
  133. 'id': video_id,
  134. 'title': title,
  135. 'formats': formats,
  136. 'thumbnails': thumbnails,
  137. 'description': clip.get('contentDescription'),
  138. 'uploader': author.get('displayName'),
  139. 'timestamp': float_or_none(clip.get('created'), 1000),
  140. 'uploader_id': author_id,
  141. 'uploader_url': author_url,
  142. 'duration': int_or_none(clip.get('videoLengthSeconds')),
  143. 'view_count': int_or_none(clip.get('views')),
  144. 'like_count': int_or_none(clip.get('likes')),
  145. 'comment_count': int_or_none(clip.get('comments')),
  146. }