videopress.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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,
  11. )
  12. class VideoPressIE(InfoExtractor):
  13. _ID_REGEX = r'[\da-zA-Z]{8}'
  14. _PATH_REGEX = r'video(?:\.word)?press\.com/embed/'
  15. _VALID_URL = rf'https?://{_PATH_REGEX}(?P<id>{_ID_REGEX})'
  16. _EMBED_REGEX = [rf'<iframe[^>]+src=["\'](?P<url>(?:https?://)?{_PATH_REGEX}{_ID_REGEX})']
  17. _TESTS = [{
  18. 'url': 'https://videopress.com/embed/kUJmAcSf',
  19. 'md5': '706956a6c875873d51010921310e4bc6',
  20. 'info_dict': {
  21. 'id': 'kUJmAcSf',
  22. 'ext': 'mp4',
  23. 'title': 'VideoPress Demo',
  24. 'thumbnail': r're:^https?://.*\.jpg',
  25. 'duration': 634.6,
  26. 'timestamp': 1434983935,
  27. 'upload_date': '20150622',
  28. 'age_limit': 0,
  29. },
  30. }, {
  31. # 17+, requires birth_* params
  32. 'url': 'https://videopress.com/embed/iH3gstfZ',
  33. 'only_matching': True,
  34. }, {
  35. 'url': 'https://video.wordpress.com/embed/kUJmAcSf',
  36. 'only_matching': True,
  37. }]
  38. def _real_extract(self, url):
  39. video_id = self._match_id(url)
  40. query = random_birthday('birth_year', 'birth_month', 'birth_day')
  41. query['fields'] = 'description,duration,file_url_base,files,height,original,poster,rating,title,upload_date,width'
  42. video = self._download_json(
  43. f'https://public-api.wordpress.com/rest/v1.1/videos/{video_id}',
  44. video_id, query=query)
  45. title = video['title']
  46. file_url_base = video.get('file_url_base') or {}
  47. base_url = file_url_base.get('https') or file_url_base.get('http')
  48. QUALITIES = ('std', 'dvd', 'hd')
  49. quality = qualities(QUALITIES)
  50. formats = []
  51. for format_id, f in (video.get('files') or {}).items():
  52. if not isinstance(f, dict):
  53. continue
  54. for ext, path in f.items():
  55. if ext in ('mp4', 'ogg'):
  56. formats.append({
  57. 'url': urljoin(base_url, path),
  58. 'format_id': f'{format_id}-{ext}',
  59. 'ext': determine_ext(path, ext),
  60. 'quality': quality(format_id),
  61. })
  62. original_url = video.get('original')
  63. if original_url:
  64. formats.append({
  65. 'url': original_url,
  66. 'format_id': 'original',
  67. 'quality': len(QUALITIES),
  68. 'width': int_or_none(video.get('width')),
  69. 'height': int_or_none(video.get('height')),
  70. })
  71. return {
  72. 'id': video_id,
  73. 'title': title,
  74. 'description': video.get('description'),
  75. 'thumbnail': video.get('poster'),
  76. 'duration': float_or_none(video.get('duration'), 1000),
  77. 'timestamp': unified_timestamp(video.get('upload_date')),
  78. 'age_limit': parse_age_limit(video.get('rating')),
  79. 'formats': formats,
  80. }