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.

99 lines
3.0 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import random
  4. import re
  5. from .common import InfoExtractor
  6. from ..compat import compat_str
  7. from ..utils import (
  8. determine_ext,
  9. float_or_none,
  10. parse_age_limit,
  11. qualities,
  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. video = self._download_json(
  44. 'https://public-api.wordpress.com/rest/v1.1/videos/%s' % video_id,
  45. video_id, query={
  46. 'birth_month': random.randint(1, 12),
  47. 'birth_day': random.randint(1, 31),
  48. 'birth_year': random.randint(1950, 1995),
  49. })
  50. title = video['title']
  51. def base_url(scheme):
  52. return try_get(
  53. video, lambda x: x['file_url_base'][scheme], compat_str)
  54. base_url = base_url('https') or base_url('http')
  55. QUALITIES = ('std', 'dvd', 'hd')
  56. quality = qualities(QUALITIES)
  57. formats = []
  58. for format_id, f in video['files'].items():
  59. if not isinstance(f, dict):
  60. continue
  61. for ext, path in f.items():
  62. if ext in ('mp4', 'ogg'):
  63. formats.append({
  64. 'url': urljoin(base_url, path),
  65. 'format_id': '%s-%s' % (format_id, ext),
  66. 'ext': determine_ext(path, ext),
  67. 'quality': quality(format_id),
  68. })
  69. original_url = try_get(video, lambda x: x['original'], compat_str)
  70. if original_url:
  71. formats.append({
  72. 'url': original_url,
  73. 'format_id': 'original',
  74. 'quality': len(QUALITIES),
  75. })
  76. self._sort_formats(formats)
  77. return {
  78. 'id': video_id,
  79. 'title': title,
  80. 'description': video.get('description'),
  81. 'thumbnail': video.get('poster'),
  82. 'duration': float_or_none(video.get('duration'), 1000),
  83. 'timestamp': unified_timestamp(video.get('upload_date')),
  84. 'age_limit': parse_age_limit(video.get('rating')),
  85. 'formats': formats,
  86. }