youtube-dl/youtube_dl/extractor/medaltv.py

138 lines
4.8 KiB
Python
Raw Normal View History

2020-11-25 02:27:33 +09:00
# coding: utf-8
from __future__ import unicode_literals
2020-11-25 02:29:46 +09:00
import re
2020-11-25 02:27:33 +09:00
from .common import InfoExtractor
2020-11-25 02:29:46 +09:00
from ..compat import compat_str
2020-11-25 02:27:33 +09:00
from ..utils import (
ExtractorError,
float_or_none,
2020-11-25 02:29:46 +09:00
int_or_none,
str_or_none,
try_get,
2020-11-25 02:27:33 +09:00
)
class MedalTVIE(InfoExtractor):
_VALID_URL = r'https?://(?:www\.)?medal\.tv/clips/(?P<id>[^/?#&]+)'
2020-11-25 02:27:33 +09:00
_TESTS = [{
'url': 'https://medal.tv/clips/2mA60jWAGQCBH',
2020-11-25 02:27:33 +09:00
'md5': '7b07b064331b1cf9e8e5c52a06ae68fa',
'info_dict': {
'id': '2mA60jWAGQCBH',
2020-11-25 02:27:33 +09:00
'ext': 'mp4',
'title': 'Quad Cold',
'description': 'Medal,https://medal.tv/desktop/',
'uploader': 'MowgliSB',
'timestamp': 1603165266,
'upload_date': '20201020',
'uploader_id': '10619174',
2020-11-25 02:27:33 +09:00
}
}, {
'url': 'https://medal.tv/clips/2um24TWdty0NA',
2020-11-25 02:27:33 +09:00
'md5': 'b6dc76b78195fff0b4f8bf4a33ec2148',
'info_dict': {
'id': '2um24TWdty0NA',
2020-11-25 02:27:33 +09:00
'ext': 'mp4',
'title': 'u tk me i tk u bigger',
'description': 'Medal,https://medal.tv/desktop/',
'uploader': 'Mimicc',
'timestamp': 1605580939,
'upload_date': '20201117',
'uploader_id': '5156321',
2020-11-25 02:27:33 +09:00
}
}, {
'url': 'https://medal.tv/clips/37rMeFpryCC-9',
'only_matching': True,
}, {
'url': 'https://medal.tv/clips/2WRj40tpY_EU9',
'only_matching': True,
2020-11-25 02:27:33 +09:00
}]
def _real_extract(self, url):
video_id = self._match_id(url)
webpage = self._download_webpage(url, video_id)
2020-11-25 02:29:46 +09:00
hydration_data = self._parse_json(self._search_regex(
2020-11-25 02:27:33 +09:00
r'<script[^>]*>\s*(?:var\s*)?hydrationData\s*=\s*({.+?})\s*</script>',
2020-11-25 02:29:46 +09:00
webpage, 'hydration data', default='{}'), video_id)
2020-11-25 02:27:33 +09:00
2020-11-25 02:29:46 +09:00
clip = try_get(
hydration_data, lambda x: x['clips'][video_id], dict) or {}
if not clip:
raise ExtractorError(
'Could not find video information.', video_id=video_id)
2020-11-25 02:27:33 +09:00
2020-11-25 02:29:46 +09:00
title = clip['contentTitle']
2020-11-25 02:27:33 +09:00
2020-11-25 02:29:46 +09:00
source_width = int_or_none(clip.get('sourceWidth'))
source_height = int_or_none(clip.get('sourceHeight'))
2020-11-25 02:27:33 +09:00
2020-11-25 02:29:46 +09:00
aspect_ratio = source_width / source_height if source_width and source_height else 16 / 9
2020-11-25 02:27:33 +09:00
2020-11-25 02:29:46 +09:00
def add_item(container, item_url, height, id_key='format_id', item_id=None):
item_id = item_id or '%dp' % height
if item_id not in item_url:
return
2020-11-25 02:27:33 +09:00
width = int(round(aspect_ratio * height))
2020-11-25 02:29:46 +09:00
container.append({
'url': item_url,
id_key: item_id,
2020-11-25 02:27:33 +09:00
'width': width,
'height': height
})
2020-11-25 02:29:46 +09:00
formats = []
thumbnails = []
for k, v in clip.items():
if not (v and isinstance(v, compat_str)):
continue
mobj = re.match(r'(contentUrl|thumbnail)(?:(\d+)p)?$', k)
if not mobj:
continue
prefix = mobj.group(1)
height = int_or_none(mobj.group(2))
if prefix == 'contentUrl':
add_item(
formats, v, height or source_height,
item_id=None if height else 'source')
elif prefix == 'thumbnail':
add_item(thumbnails, v, height, 'id')
error = clip.get('error')
2020-11-25 02:27:33 +09:00
if not formats and error:
2020-11-25 02:29:46 +09:00
if error == 404:
raise ExtractorError(
'That clip does not exist.',
expected=True, video_id=video_id)
2020-11-25 02:27:33 +09:00
else:
2020-11-25 02:29:46 +09:00
raise ExtractorError(
'An unknown error occurred ({0}).'.format(error),
video_id=video_id)
self._sort_formats(formats)
2020-11-25 02:27:33 +09:00
# Necessary because the id of the author is not known in advance.
# Won't raise an issue if no profile can be found as this is optional.
2020-11-25 02:29:46 +09:00
author = try_get(
hydration_data, lambda x: list(x['profiles'].values())[0], dict) or {}
author_id = str_or_none(author.get('id'))
2020-11-25 02:27:33 +09:00
author_url = 'https://medal.tv/users/{0}'.format(author_id) if author_id else None
return {
'id': video_id,
2020-11-25 02:29:46 +09:00
'title': title,
2020-11-25 02:27:33 +09:00
'formats': formats,
'thumbnails': thumbnails,
2020-11-25 02:29:46 +09:00
'description': clip.get('contentDescription'),
'uploader': author.get('displayName'),
'timestamp': float_or_none(clip.get('created'), 1000),
2020-11-25 02:27:33 +09:00
'uploader_id': author_id,
'uploader_url': author_url,
2020-11-25 02:29:46 +09:00
'duration': int_or_none(clip.get('videoLengthSeconds')),
'view_count': int_or_none(clip.get('views')),
'like_count': int_or_none(clip.get('likes')),
'comment_count': int_or_none(clip.get('comments')),
2020-11-25 02:27:33 +09:00
}