[ie/twitter:spaces] Support video spaces (#10789)
[yt-dlp3.git] / yt_dlp / extractor / videopress.py
blobd3c9c8e22ec0a67e285408af662bd447ef7f8e17
1 from .common import InfoExtractor
2 from ..utils import (
3 determine_ext,
4 float_or_none,
5 int_or_none,
6 parse_age_limit,
7 qualities,
8 random_birthday,
9 unified_timestamp,
10 urljoin,
14 class VideoPressIE(InfoExtractor):
15 _ID_REGEX = r'[\da-zA-Z]{8}'
16 _PATH_REGEX = r'video(?:\.word)?press\.com/embed/'
17 _VALID_URL = rf'https?://{_PATH_REGEX}(?P<id>{_ID_REGEX})'
18 _EMBED_REGEX = [rf'<iframe[^>]+src=["\'](?P<url>(?:https?://)?{_PATH_REGEX}{_ID_REGEX})']
19 _TESTS = [{
20 'url': 'https://videopress.com/embed/kUJmAcSf',
21 'md5': '706956a6c875873d51010921310e4bc6',
22 'info_dict': {
23 'id': 'kUJmAcSf',
24 'ext': 'mp4',
25 'title': 'VideoPress Demo',
26 'thumbnail': r're:^https?://.*\.jpg',
27 'duration': 634.6,
28 'timestamp': 1434983935,
29 'upload_date': '20150622',
30 'age_limit': 0,
32 }, {
33 # 17+, requires birth_* params
34 'url': 'https://videopress.com/embed/iH3gstfZ',
35 'only_matching': True,
36 }, {
37 'url': 'https://video.wordpress.com/embed/kUJmAcSf',
38 'only_matching': True,
41 def _real_extract(self, url):
42 video_id = self._match_id(url)
44 query = random_birthday('birth_year', 'birth_month', 'birth_day')
45 query['fields'] = 'description,duration,file_url_base,files,height,original,poster,rating,title,upload_date,width'
46 video = self._download_json(
47 f'https://public-api.wordpress.com/rest/v1.1/videos/{video_id}',
48 video_id, query=query)
50 title = video['title']
52 file_url_base = video.get('file_url_base') or {}
53 base_url = file_url_base.get('https') or file_url_base.get('http')
55 QUALITIES = ('std', 'dvd', 'hd')
56 quality = qualities(QUALITIES)
58 formats = []
59 for format_id, f in (video.get('files') or {}).items():
60 if not isinstance(f, dict):
61 continue
62 for ext, path in f.items():
63 if ext in ('mp4', 'ogg'):
64 formats.append({
65 'url': urljoin(base_url, path),
66 'format_id': f'{format_id}-{ext}',
67 'ext': determine_ext(path, ext),
68 'quality': quality(format_id),
70 original_url = video.get('original')
71 if original_url:
72 formats.append({
73 'url': original_url,
74 'format_id': 'original',
75 'quality': len(QUALITIES),
76 'width': int_or_none(video.get('width')),
77 'height': int_or_none(video.get('height')),
80 return {
81 'id': video_id,
82 'title': title,
83 'description': video.get('description'),
84 'thumbnail': video.get('poster'),
85 'duration': float_or_none(video.get('duration'), 1000),
86 'timestamp': unified_timestamp(video.get('upload_date')),
87 'age_limit': parse_age_limit(video.get('rating')),
88 'formats': formats,