logo

youtube-dl

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

streamable.py (3891B)


  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. ExtractorError,
  7. float_or_none,
  8. int_or_none,
  9. )
  10. class StreamableIE(InfoExtractor):
  11. _VALID_URL = r'https?://streamable\.com/(?:[es]/)?(?P<id>\w+)'
  12. _TESTS = [
  13. {
  14. 'url': 'https://streamable.com/dnd1',
  15. 'md5': '3e3bc5ca088b48c2d436529b64397fef',
  16. 'info_dict': {
  17. 'id': 'dnd1',
  18. 'ext': 'mp4',
  19. 'title': 'Mikel Oiarzabal scores to make it 0-3 for La Real against Espanyol',
  20. 'thumbnail': r're:https?://.*\.jpg$',
  21. 'uploader': 'teabaker',
  22. 'timestamp': 1454964157.35115,
  23. 'upload_date': '20160208',
  24. 'duration': 61.516,
  25. 'view_count': int,
  26. }
  27. },
  28. # older video without bitrate, width/height, etc. info
  29. {
  30. 'url': 'https://streamable.com/moo',
  31. 'md5': '2cf6923639b87fba3279ad0df3a64e73',
  32. 'info_dict': {
  33. 'id': 'moo',
  34. 'ext': 'mp4',
  35. 'title': '"Please don\'t eat me!"',
  36. 'thumbnail': r're:https?://.*\.jpg$',
  37. 'timestamp': 1426115495,
  38. 'upload_date': '20150311',
  39. 'duration': 12,
  40. 'view_count': int,
  41. }
  42. },
  43. {
  44. 'url': 'https://streamable.com/e/dnd1',
  45. 'only_matching': True,
  46. },
  47. {
  48. 'url': 'https://streamable.com/s/okkqk/drxjds',
  49. 'only_matching': True,
  50. }
  51. ]
  52. @staticmethod
  53. def _extract_url(webpage):
  54. mobj = re.search(
  55. r'<iframe[^>]+src=(?P<q1>[\'"])(?P<src>(?:https?:)?//streamable\.com/(?:(?!\1).+))(?P=q1)',
  56. webpage)
  57. if mobj:
  58. return mobj.group('src')
  59. def _real_extract(self, url):
  60. video_id = self._match_id(url)
  61. # Note: Using the ajax API, as the public Streamable API doesn't seem
  62. # to return video info like the title properly sometimes, and doesn't
  63. # include info like the video duration
  64. video = self._download_json(
  65. 'https://ajax.streamable.com/videos/%s' % video_id, video_id)
  66. # Format IDs:
  67. # 0 The video is being uploaded
  68. # 1 The video is being processed
  69. # 2 The video has at least one file ready
  70. # 3 The video is unavailable due to an error
  71. status = video.get('status')
  72. if status != 2:
  73. raise ExtractorError(
  74. 'This video is currently unavailable. It may still be uploading or processing.',
  75. expected=True)
  76. title = video.get('reddit_title') or video['title']
  77. formats = []
  78. for key, info in video['files'].items():
  79. if not info.get('url'):
  80. continue
  81. formats.append({
  82. 'format_id': key,
  83. 'url': self._proto_relative_url(info['url']),
  84. 'width': int_or_none(info.get('width')),
  85. 'height': int_or_none(info.get('height')),
  86. 'filesize': int_or_none(info.get('size')),
  87. 'fps': int_or_none(info.get('framerate')),
  88. 'vbr': float_or_none(info.get('bitrate'), 1000)
  89. })
  90. self._sort_formats(formats)
  91. return {
  92. 'id': video_id,
  93. 'title': title,
  94. 'description': video.get('description'),
  95. 'thumbnail': self._proto_relative_url(video.get('thumbnail_url')),
  96. 'uploader': video.get('owner', {}).get('user_name'),
  97. 'timestamp': float_or_none(video.get('date_added')),
  98. 'duration': float_or_none(video.get('duration')),
  99. 'view_count': int_or_none(video.get('plays')),
  100. 'formats': formats
  101. }