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