streamable.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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/(?:e/)?(?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': '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': '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. @staticmethod
  49. def _extract_url(webpage):
  50. print(webpage)
  51. mobj = re.search(
  52. r'<iframe[^>]+src=(?P<q1>[\'"])(?P<src>(?:https?:)?//streamable\.com/(?:(?!\1).+))(?P=q1)',
  53. webpage)
  54. if mobj:
  55. return mobj.group('src')
  56. def _real_extract(self, url):
  57. video_id = self._match_id(url)
  58. # Note: Using the ajax API, as the public Streamable API doesn't seem
  59. # to return video info like the title properly sometimes, and doesn't
  60. # include info like the video duration
  61. video = self._download_json(
  62. 'https://streamable.com/ajax/videos/%s' % video_id, video_id)
  63. # Format IDs:
  64. # 0 The video is being uploaded
  65. # 1 The video is being processed
  66. # 2 The video has at least one file ready
  67. # 3 The video is unavailable due to an error
  68. status = video.get('status')
  69. if status != 2:
  70. raise ExtractorError(
  71. 'This video is currently unavailable. It may still be uploading or processing.',
  72. expected=True)
  73. title = video.get('reddit_title') or video['title']
  74. formats = []
  75. for key, info in video['files'].items():
  76. if not info.get('url'):
  77. continue
  78. formats.append({
  79. 'format_id': key,
  80. 'url': self._proto_relative_url(info['url']),
  81. 'width': int_or_none(info.get('width')),
  82. 'height': int_or_none(info.get('height')),
  83. 'filesize': int_or_none(info.get('size')),
  84. 'fps': int_or_none(info.get('framerate')),
  85. 'vbr': float_or_none(info.get('bitrate'), 1000)
  86. })
  87. self._sort_formats(formats)
  88. return {
  89. 'id': video_id,
  90. 'title': title,
  91. 'description': video.get('description'),
  92. 'thumbnail': self._proto_relative_url(video.get('thumbnail_url')),
  93. 'uploader': video.get('owner', {}).get('user_name'),
  94. 'timestamp': float_or_none(video.get('date_added')),
  95. 'duration': float_or_none(video.get('duration')),
  96. 'view_count': int_or_none(video.get('plays')),
  97. 'formats': formats
  98. }