logo

youtube-dl

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

wistia.py (7230B)


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