wistia.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. from __future__ import unicode_literals
  2. from .common import InfoExtractor
  3. from ..utils import (
  4. ExtractorError,
  5. int_or_none,
  6. )
  7. class WistiaIE(InfoExtractor):
  8. _VALID_URL = r'(?:wistia:|https?://(?:fast\.)?wistia\.net/embed/iframe/)(?P<id>[a-z0-9]+)'
  9. _API_URL = 'http://fast.wistia.com/embed/medias/%s.json'
  10. _IFRAME_URL = 'http://fast.wistia.net/embed/iframe/%s'
  11. _TESTS = [{
  12. 'url': 'http://fast.wistia.net/embed/iframe/sh7fpupwlt',
  13. 'md5': 'cafeb56ec0c53c18c97405eecb3133df',
  14. 'info_dict': {
  15. 'id': 'sh7fpupwlt',
  16. 'ext': 'mov',
  17. 'title': 'Being Resourceful',
  18. 'description': 'a Clients From Hell Video Series video from worldwidewebhosting',
  19. 'upload_date': '20131204',
  20. 'timestamp': 1386185018,
  21. 'duration': 117,
  22. },
  23. }, {
  24. 'url': 'wistia:sh7fpupwlt',
  25. 'only_matching': True,
  26. }, {
  27. # with hls video
  28. 'url': 'wistia:807fafadvk',
  29. 'only_matching': True,
  30. }]
  31. def _real_extract(self, url):
  32. video_id = self._match_id(url)
  33. data_json = self._download_json(
  34. self._API_URL % video_id, video_id,
  35. # Some videos require this.
  36. headers={
  37. 'Referer': url if url.startswith('http') else self._IFRAME_URL % video_id,
  38. })
  39. if data_json.get('error'):
  40. raise ExtractorError(
  41. 'Error while getting the playlist', expected=True)
  42. data = data_json['media']
  43. title = data['name']
  44. formats = []
  45. thumbnails = []
  46. for a in data['assets']:
  47. aurl = a.get('url')
  48. if not aurl:
  49. continue
  50. astatus = a.get('status')
  51. atype = a.get('type')
  52. if (astatus is not None and astatus != 2) or atype in ('preview', 'storyboard'):
  53. continue
  54. elif atype in ('still', 'still_image'):
  55. thumbnails.append({
  56. 'url': aurl,
  57. 'width': int_or_none(a.get('width')),
  58. 'height': int_or_none(a.get('height')),
  59. })
  60. else:
  61. aext = a.get('ext')
  62. is_m3u8 = a.get('container') == 'm3u8' or aext == 'm3u8'
  63. formats.append({
  64. 'format_id': atype,
  65. 'url': aurl,
  66. 'tbr': int_or_none(a.get('bitrate')),
  67. 'vbr': int_or_none(a.get('opt_vbitrate')),
  68. 'width': int_or_none(a.get('width')),
  69. 'height': int_or_none(a.get('height')),
  70. 'filesize': int_or_none(a.get('size')),
  71. 'vcodec': a.get('codec'),
  72. 'container': a.get('container'),
  73. 'ext': 'mp4' if is_m3u8 else aext,
  74. 'protocol': 'm3u8' if is_m3u8 else None,
  75. 'preference': 1 if atype == 'original' else None,
  76. })
  77. self._sort_formats(formats)
  78. return {
  79. 'id': video_id,
  80. 'title': title,
  81. 'description': data.get('seoDescription'),
  82. 'formats': formats,
  83. 'thumbnails': thumbnails,
  84. 'duration': int_or_none(data.get('duration')),
  85. 'timestamp': int_or_none(data.get('createdAt')),
  86. }