logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

videopress.py (3359B)


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