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