You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.9 KiB

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