wistia.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. ExtractorError,
  6. int_or_none,
  7. float_or_none,
  8. unescapeHTML,
  9. )
  10. class WistiaIE(InfoExtractor):
  11. _VALID_URL = r'(?:wistia:|https?://(?:fast\.)?wistia\.(?:net|com)/embed/(?:iframe|medias)/)(?P<id>[a-z0-9]{10})'
  12. _EMBED_BASE_URL = 'http://fast.wistia.com/embed/'
  13. _TESTS = [{
  14. 'url': 'http://fast.wistia.net/embed/iframe/sh7fpupwlt',
  15. 'md5': 'cafeb56ec0c53c18c97405eecb3133df',
  16. 'info_dict': {
  17. 'id': 'sh7fpupwlt',
  18. 'ext': 'mov',
  19. 'title': 'Being Resourceful',
  20. 'description': 'a Clients From Hell Video Series video from worldwidewebhosting',
  21. 'upload_date': '20131204',
  22. 'timestamp': 1386185018,
  23. 'duration': 117,
  24. },
  25. }, {
  26. 'url': 'wistia:sh7fpupwlt',
  27. 'only_matching': True,
  28. }, {
  29. # with hls video
  30. 'url': 'wistia:807fafadvk',
  31. 'only_matching': True,
  32. }, {
  33. 'url': 'http://fast.wistia.com/embed/iframe/sh7fpupwlt',
  34. 'only_matching': True,
  35. }, {
  36. 'url': 'http://fast.wistia.net/embed/medias/sh7fpupwlt.json',
  37. 'only_matching': True,
  38. }]
  39. # https://wistia.com/support/embed-and-share/video-on-your-website
  40. @staticmethod
  41. def _extract_url(webpage):
  42. urls = WistiaIE._extract_urls(webpage)
  43. return urls[0] if urls else None
  44. @staticmethod
  45. def _extract_urls(webpage):
  46. urls = []
  47. for match in re.finditer(
  48. r'<(?:meta[^>]+?content|(?:iframe|script)[^>]+?src)=["\'](?P<url>(?:https?:)?//(?:fast\.)?wistia\.(?:net|com)/embed/(?:iframe|medias)/[a-z0-9]{10})', webpage):
  49. urls.append(unescapeHTML(match.group('url')))
  50. for match in re.finditer(
  51. r'''(?sx)
  52. <div[^>]+class=(["'])(?:(?!\1).)*?\bwistia_async_(?P<id>[a-z0-9]{10})\b(?:(?!\1).)*?\1
  53. ''', webpage):
  54. urls.append('wistia:%s' % match.group('id'))
  55. for match in re.finditer(r'(?:data-wistia-?id=["\']|Wistia\.embed\(["\']|id=["\']wistia_)(?P<id>[a-z0-9]{10})', webpage):
  56. urls.append('wistia:%s' % match.group('id'))
  57. return urls
  58. def _real_extract(self, url):
  59. video_id = self._match_id(url)
  60. data_json = self._download_json(
  61. self._EMBED_BASE_URL + 'medias/%s.json' % video_id, video_id,
  62. # Some videos require this.
  63. headers={
  64. 'Referer': url if url.startswith('http') else self._EMBED_BASE_URL + 'iframe/' + video_id,
  65. })
  66. if data_json.get('error'):
  67. raise ExtractorError(
  68. 'Error while getting the playlist', expected=True)
  69. data = data_json['media']
  70. title = data['name']
  71. formats = []
  72. thumbnails = []
  73. for a in data['assets']:
  74. aurl = a.get('url')
  75. if not aurl:
  76. continue
  77. astatus = a.get('status')
  78. atype = a.get('type')
  79. if (astatus is not None and astatus != 2) or atype in ('preview', 'storyboard'):
  80. continue
  81. elif atype in ('still', 'still_image'):
  82. thumbnails.append({
  83. 'url': aurl,
  84. 'width': int_or_none(a.get('width')),
  85. 'height': int_or_none(a.get('height')),
  86. 'filesize': int_or_none(a.get('size')),
  87. })
  88. else:
  89. aext = a.get('ext')
  90. display_name = a.get('display_name')
  91. format_id = atype
  92. if atype and atype.endswith('_video') and display_name:
  93. format_id = '%s-%s' % (atype[:-6], display_name)
  94. f = {
  95. 'format_id': format_id,
  96. 'url': aurl,
  97. 'tbr': int_or_none(a.get('bitrate')) or None,
  98. 'preference': 1 if atype == 'original' else None,
  99. }
  100. if display_name == 'Audio':
  101. f.update({
  102. 'vcodec': 'none',
  103. })
  104. else:
  105. f.update({
  106. 'width': int_or_none(a.get('width')),
  107. 'height': int_or_none(a.get('height')),
  108. 'vcodec': a.get('codec'),
  109. })
  110. if a.get('container') == 'm3u8' or aext == 'm3u8':
  111. ts_f = f.copy()
  112. ts_f.update({
  113. 'ext': 'ts',
  114. 'format_id': f['format_id'].replace('hls-', 'ts-'),
  115. 'url': f['url'].replace('.bin', '.ts'),
  116. })
  117. formats.append(ts_f)
  118. f.update({
  119. 'ext': 'mp4',
  120. 'protocol': 'm3u8_native',
  121. })
  122. else:
  123. f.update({
  124. 'container': a.get('container'),
  125. 'ext': aext,
  126. 'filesize': int_or_none(a.get('size')),
  127. })
  128. formats.append(f)
  129. self._sort_formats(formats)
  130. subtitles = {}
  131. for caption in data.get('captions', []):
  132. language = caption.get('language')
  133. if not language:
  134. continue
  135. subtitles[language] = [{
  136. 'url': self._EMBED_BASE_URL + 'captions/' + video_id + '.vtt?language=' + language,
  137. }]
  138. return {
  139. 'id': video_id,
  140. 'title': title,
  141. 'description': data.get('seoDescription'),
  142. 'formats': formats,
  143. 'thumbnails': thumbnails,
  144. 'duration': float_or_none(data.get('duration')),
  145. 'timestamp': int_or_none(data.get('createdAt')),
  146. 'subtitles': subtitles,
  147. }