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.

120 lines
4.1 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. int_or_none,
  6. urlencode_postdata,
  7. compat_str,
  8. ExtractorError,
  9. )
  10. class CuriosityStreamBaseIE(InfoExtractor):
  11. _NETRC_MACHINE = 'curiositystream'
  12. _auth_token = None
  13. _API_BASE_URL = 'https://api.curiositystream.com/v1/'
  14. def _handle_errors(self, result):
  15. error = result.get('error', {}).get('message')
  16. if error:
  17. if isinstance(error, dict):
  18. error = ', '.join(error.values())
  19. raise ExtractorError(
  20. '%s said: %s' % (self.IE_NAME, error), expected=True)
  21. def _call_api(self, path, video_id):
  22. headers = {}
  23. if self._auth_token:
  24. headers['X-Auth-Token'] = self._auth_token
  25. result = self._download_json(
  26. self._API_BASE_URL + path, video_id, headers=headers)
  27. self._handle_errors(result)
  28. return result['data']
  29. def _real_initialize(self):
  30. (email, password) = self._get_login_info()
  31. if email is None:
  32. return
  33. result = self._download_json(
  34. self._API_BASE_URL + 'login', None, data=urlencode_postdata({
  35. 'email': email,
  36. 'password': password,
  37. }))
  38. self._handle_errors(result)
  39. self._auth_token = result['message']['auth_token']
  40. def _extract_media_info(self, media):
  41. video_id = compat_str(media['id'])
  42. limelight_media_id = media['limelight_media_id']
  43. title = media['title']
  44. subtitles = {}
  45. for closed_caption in media.get('closed_captions', []):
  46. sub_url = closed_caption.get('file')
  47. if not sub_url:
  48. continue
  49. lang = closed_caption.get('code') or closed_caption.get('language') or 'en'
  50. subtitles.setdefault(lang, []).append({
  51. 'url': sub_url,
  52. })
  53. return {
  54. '_type': 'url_transparent',
  55. 'id': video_id,
  56. 'url': 'limelight:media:' + limelight_media_id,
  57. 'title': title,
  58. 'description': media.get('description'),
  59. 'thumbnail': media.get('image_large') or media.get('image_medium') or media.get('image_small'),
  60. 'duration': int_or_none(media.get('duration')),
  61. 'tags': media.get('tags'),
  62. 'subtitles': subtitles,
  63. 'ie_key': 'LimelightMedia',
  64. }
  65. class CuriosityStreamIE(CuriosityStreamBaseIE):
  66. IE_NAME = 'curiositystream'
  67. _VALID_URL = r'https?://app\.curiositystream\.com/video/(?P<id>\d+)'
  68. _TEST = {
  69. 'url': 'https://app.curiositystream.com/video/2',
  70. 'md5': 'a0074c190e6cddaf86900b28d3e9ee7a',
  71. 'info_dict': {
  72. 'id': '2',
  73. 'ext': 'mp4',
  74. 'title': 'How Did You Develop The Internet?',
  75. 'description': 'Vint Cerf, Google\'s Chief Internet Evangelist, describes how he and Bob Kahn created the internet.',
  76. 'timestamp': 1448388615,
  77. 'upload_date': '20151124',
  78. }
  79. }
  80. def _real_extract(self, url):
  81. video_id = self._match_id(url)
  82. media = self._call_api('media/' + video_id, video_id)
  83. return self._extract_media_info(media)
  84. class CuriosityStreamCollectionIE(CuriosityStreamBaseIE):
  85. IE_NAME = 'curiositystream:collection'
  86. _VALID_URL = r'https?://app\.curiositystream\.com/collection/(?P<id>\d+)'
  87. _TEST = {
  88. 'url': 'https://app.curiositystream.com/collection/2',
  89. 'info_dict': {
  90. 'id': '2',
  91. 'title': 'Curious Minds: The Internet',
  92. 'description': 'How is the internet shaping our lives in the 21st Century?',
  93. },
  94. 'playlist_mincount': 17,
  95. }
  96. def _real_extract(self, url):
  97. collection_id = self._match_id(url)
  98. collection = self._call_api(
  99. 'collections/' + collection_id, collection_id)
  100. entries = []
  101. for media in collection.get('media', []):
  102. entries.append(self._extract_media_info(media))
  103. return self.playlist_result(
  104. entries, collection_id,
  105. collection.get('title'), collection.get('description'))