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.

108 lines
3.7 KiB

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