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