minds.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. from .common import InfoExtractor
  2. from ..compat import compat_str
  3. from ..utils import (
  4. clean_html,
  5. format_field,
  6. int_or_none,
  7. str_or_none,
  8. strip_or_none,
  9. )
  10. class MindsBaseIE(InfoExtractor):
  11. _VALID_URL_BASE = r'https?://(?:www\.)?minds\.com/'
  12. def _call_api(self, path, video_id, resource, query=None):
  13. api_url = 'https://www.minds.com/api/' + path
  14. token = self._get_cookies(api_url).get('XSRF-TOKEN')
  15. return self._download_json(
  16. api_url, video_id, 'Downloading %s JSON metadata' % resource, headers={
  17. 'Referer': 'https://www.minds.com/',
  18. 'X-XSRF-TOKEN': token.value if token else '',
  19. }, query=query)
  20. class MindsIE(MindsBaseIE):
  21. IE_NAME = 'minds'
  22. _VALID_URL = MindsBaseIE._VALID_URL_BASE + r'(?:media|newsfeed|archive/view)/(?P<id>[0-9]+)'
  23. _TESTS = [{
  24. 'url': 'https://www.minds.com/media/100000000000086822',
  25. 'md5': '215a658184a419764852239d4970b045',
  26. 'info_dict': {
  27. 'id': '100000000000086822',
  28. 'ext': 'mp4',
  29. 'title': 'Minds intro sequence',
  30. 'thumbnail': r're:https?://.+\.png',
  31. 'uploader_id': 'ottman',
  32. 'upload_date': '20130524',
  33. 'timestamp': 1369404826,
  34. 'uploader': 'Bill Ottman',
  35. 'view_count': int,
  36. 'like_count': int,
  37. 'dislike_count': int,
  38. 'tags': ['animation'],
  39. 'comment_count': int,
  40. 'license': 'attribution-cc',
  41. },
  42. }, {
  43. # entity.type == 'activity' and empty title
  44. 'url': 'https://www.minds.com/newsfeed/798025111988506624',
  45. 'md5': 'b2733a74af78d7fd3f541c4cbbaa5950',
  46. 'info_dict': {
  47. 'id': '798022190320226304',
  48. 'ext': 'mp4',
  49. 'title': '798022190320226304',
  50. 'uploader': 'ColinFlaherty',
  51. 'upload_date': '20180111',
  52. 'timestamp': 1515639316,
  53. 'uploader_id': 'ColinFlaherty',
  54. },
  55. }, {
  56. 'url': 'https://www.minds.com/archive/view/715172106794442752',
  57. 'only_matching': True,
  58. }, {
  59. # youtube perma_url
  60. 'url': 'https://www.minds.com/newsfeed/1197131838022602752',
  61. 'only_matching': True,
  62. }]
  63. def _real_extract(self, url):
  64. entity_id = self._match_id(url)
  65. entity = self._call_api(
  66. 'v1/entities/entity/' + entity_id, entity_id, 'entity')['entity']
  67. if entity.get('type') == 'activity':
  68. if entity.get('custom_type') == 'video':
  69. video_id = entity['entity_guid']
  70. else:
  71. return self.url_result(entity['perma_url'])
  72. else:
  73. assert entity['subtype'] == 'video'
  74. video_id = entity_id
  75. # 1080p and webm formats available only on the sources array
  76. video = self._call_api(
  77. 'v2/media/video/' + video_id, video_id, 'video')
  78. formats = []
  79. for source in (video.get('sources') or []):
  80. src = source.get('src')
  81. if not src:
  82. continue
  83. formats.append({
  84. 'format_id': source.get('label'),
  85. 'height': int_or_none(source.get('size')),
  86. 'url': src,
  87. })
  88. entity = video.get('entity') or entity
  89. owner = entity.get('ownerObj') or {}
  90. uploader_id = owner.get('username')
  91. tags = entity.get('tags')
  92. if tags and isinstance(tags, compat_str):
  93. tags = [tags]
  94. thumbnail = None
  95. poster = video.get('poster') or entity.get('thumbnail_src')
  96. if poster:
  97. urlh = self._request_webpage(poster, video_id, fatal=False)
  98. if urlh:
  99. thumbnail = urlh.geturl()
  100. return {
  101. 'id': video_id,
  102. 'title': entity.get('title') or video_id,
  103. 'formats': formats,
  104. 'description': clean_html(entity.get('description')) or None,
  105. 'license': str_or_none(entity.get('license')),
  106. 'timestamp': int_or_none(entity.get('time_created')),
  107. 'uploader': strip_or_none(owner.get('name')),
  108. 'uploader_id': uploader_id,
  109. 'uploader_url': format_field(uploader_id, None, 'https://www.minds.com/%s'),
  110. 'view_count': int_or_none(entity.get('play:count')),
  111. 'like_count': int_or_none(entity.get('thumbs:up:count')),
  112. 'dislike_count': int_or_none(entity.get('thumbs:down:count')),
  113. 'tags': tags,
  114. 'comment_count': int_or_none(entity.get('comments:count')),
  115. 'thumbnail': thumbnail,
  116. }
  117. class MindsFeedBaseIE(MindsBaseIE):
  118. _PAGE_SIZE = 150
  119. def _entries(self, feed_id):
  120. query = {'limit': self._PAGE_SIZE, 'sync': 1}
  121. i = 1
  122. while True:
  123. data = self._call_api(
  124. 'v2/feeds/container/%s/videos' % feed_id,
  125. feed_id, 'page %s' % i, query)
  126. entities = data.get('entities') or []
  127. for entity in entities:
  128. guid = entity.get('guid')
  129. if not guid:
  130. continue
  131. yield self.url_result(
  132. 'https://www.minds.com/newsfeed/' + guid,
  133. MindsIE.ie_key(), guid)
  134. query['from_timestamp'] = data['load-next']
  135. if not (query['from_timestamp'] and len(entities) == self._PAGE_SIZE):
  136. break
  137. i += 1
  138. def _real_extract(self, url):
  139. feed_id = self._match_id(url)
  140. feed = self._call_api(
  141. 'v1/%s/%s' % (self._FEED_PATH, feed_id),
  142. feed_id, self._FEED_TYPE)[self._FEED_TYPE]
  143. return self.playlist_result(
  144. self._entries(feed['guid']), feed_id,
  145. strip_or_none(feed.get('name')),
  146. feed.get('briefdescription'))
  147. class MindsChannelIE(MindsFeedBaseIE):
  148. _FEED_TYPE = 'channel'
  149. IE_NAME = 'minds:' + _FEED_TYPE
  150. _VALID_URL = MindsBaseIE._VALID_URL_BASE + r'(?!(?:newsfeed|media|api|archive|groups)/)(?P<id>[^/?&#]+)'
  151. _FEED_PATH = 'channel'
  152. _TEST = {
  153. 'url': 'https://www.minds.com/ottman',
  154. 'info_dict': {
  155. 'id': 'ottman',
  156. 'title': 'Bill Ottman',
  157. 'description': 'Co-creator & CEO @minds',
  158. },
  159. 'playlist_mincount': 54,
  160. }
  161. class MindsGroupIE(MindsFeedBaseIE):
  162. _FEED_TYPE = 'group'
  163. IE_NAME = 'minds:' + _FEED_TYPE
  164. _VALID_URL = MindsBaseIE._VALID_URL_BASE + r'groups/profile/(?P<id>[0-9]+)'
  165. _FEED_PATH = 'groups/group'
  166. _TEST = {
  167. 'url': 'https://www.minds.com/groups/profile/785582576369672204/feed/videos',
  168. 'info_dict': {
  169. 'id': '785582576369672204',
  170. 'title': 'Cooking Videos',
  171. },
  172. 'playlist_mincount': 1,
  173. }