medaltv.py 4.9 KB

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