]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/gettr.py
[cleanup] Misc fixes
[yt-dlp.git] / yt_dlp / extractor / gettr.py
CommitLineData
85187609 1# coding: utf-8
2from __future__ import unicode_literals
3
4from .common import InfoExtractor
5from ..utils import (
82b51767 6 bool_or_none,
85187609 7 ExtractorError,
8 dict_get,
9 float_or_none,
10 int_or_none,
85187609 11 str_or_none,
b90dbe6c 12 traverse_obj,
85187609 13 try_get,
14 url_or_none,
15 urljoin,
16)
17
18
82b51767 19class GettrBaseIE(InfoExtractor):
20 _BASE_REGEX = r'https?://(www\.)?gettr\.com/'
85187609 21 _MEDIA_BASE_URL = 'https://media.gettr.com/'
22
82b51767 23 def _call_api(self, path, video_id, *args, **kwargs):
24 return self._download_json(urljoin('https://api.gettr.com/u/', path), video_id, *args, **kwargs)['result']
25
26
27class GettrIE(GettrBaseIE):
28 _VALID_URL = GettrBaseIE._BASE_REGEX + r'post/(?P<id>[a-z0-9]+)'
29
85187609 30 _TESTS = [{
31 'url': 'https://www.gettr.com/post/pcf6uv838f',
32 'info_dict': {
33 'id': 'pcf6uv838f',
34 'title': 'md5:9086a646bbd06c41c4fe8e52b3c93454',
35 'description': 'md5:be0577f1e4caadc06de4a002da2bf287',
36 'ext': 'mp4',
37 'uploader': 'EpochTV',
38 'uploader_id': 'epochtv',
b90dbe6c 39 'upload_date': '20210927',
85187609 40 'thumbnail': r're:^https?://.+/out\.jpg',
b90dbe6c 41 'timestamp': 1632782451.058,
85187609 42 'duration': 58.5585,
971c4847 43 'tags': ['hornofafrica', 'explorations'],
85187609 44 }
45 }, {
46 'url': 'https://gettr.com/post/p4iahp',
47 'info_dict': {
48 'id': 'p4iahp',
49 'title': 'md5:b03c07883db6fbc1aab88877a6c3b149',
50 'description': 'md5:741b7419d991c403196ed2ea7749a39d',
51 'ext': 'mp4',
52 'uploader': 'Neues Forum Freiheit',
53 'uploader_id': 'nf_freiheit',
b90dbe6c 54 'upload_date': '20210718',
85187609 55 'thumbnail': r're:^https?://.+/out\.jpg',
b90dbe6c 56 'timestamp': 1626594455.017,
85187609 57 'duration': 23,
971c4847 58 'tags': 'count:12',
85187609 59 }
b90dbe6c 60 }, {
61 # quote post
62 'url': 'https://gettr.com/post/pxn5b743a9',
63 'only_matching': True,
64 }, {
65 # quote with video
66 'url': 'https://gettr.com/post/pxtiiz5ca2',
67 'only_matching': True,
68 }, {
69 # streaming embed
70 'url': 'https://gettr.com/post/pxlu8p3b13',
71 'only_matching': True,
72 }, {
73 # youtube embed
74 'url': 'https://gettr.com/post/pv6wp9e24c',
75 'only_matching': True,
76 'add_ie': ['Youtube'],
85187609 77 }]
78
79 def _real_extract(self, url):
80 post_id = self._match_id(url)
81 webpage = self._download_webpage(url, post_id)
82b51767 82 api_data = self._call_api('post/%s?incl="poststats|userinfo"' % post_id, post_id)
85187609 83
82b51767 84 post_data = api_data.get('data')
b90dbe6c 85 user_data = try_get(api_data, lambda x: x['aux']['uinf'][post_data['uid']], dict) or {}
85187609 86
b90dbe6c 87 vid = post_data.get('vid')
88 ovid = post_data.get('ovid')
89
90 if post_data.get('p_type') == 'stream':
91 return self.url_result(f'https://gettr.com/streaming/{post_id}', ie='GettrStreaming', video_id=post_id)
92
93 if not (ovid or vid):
94 embed_url = url_or_none(post_data.get('prevsrc'))
95 shared_post_id = traverse_obj(api_data, ('aux', 'shrdpst', '_id'), ('data', 'rpstIds', 0), expected_type=str)
96
97 if embed_url:
98 return self.url_result(embed_url)
99 elif shared_post_id:
100 return self.url_result(f'https://gettr.com/post/{shared_post_id}', ie='Gettr', video_id=shared_post_id)
101 else:
102 raise ExtractorError('There\'s no video in this post.')
85187609 103
104 title = description = str_or_none(
105 post_data.get('txt') or self._og_search_description(webpage))
106
107 uploader = str_or_none(
108 user_data.get('nickname')
b90dbe6c 109 or self._search_regex(r'^(.+?) on GETTR', self._og_search_title(webpage, default=''), 'uploader', fatal=False))
110
85187609 111 if uploader:
112 title = '%s - %s' % (uploader, title)
113
b90dbe6c 114 formats, subtitles = self._extract_m3u8_formats_and_subtitles(
85187609 115 urljoin(self._MEDIA_BASE_URL, vid), post_id, 'mp4',
b90dbe6c 116 entry_protocol='m3u8_native', m3u8_id='hls', fatal=False) if vid else ([], {})
85187609 117
118 if ovid:
119 formats.append({
120 'url': urljoin(self._MEDIA_BASE_URL, ovid),
121 'format_id': 'ovid',
122 'ext': 'mp4',
123 'width': int_or_none(post_data.get('vid_wid')),
124 'height': int_or_none(post_data.get('vid_hgt')),
85187609 125 })
126
127 self._sort_formats(formats)
128
129 return {
130 'id': post_id,
131 'title': title,
132 'description': description,
b90dbe6c 133 'formats': formats,
134 'subtitles': subtitles,
135 'uploader': uploader,
85187609 136 'uploader_id': str_or_none(
137 dict_get(user_data, ['_id', 'username'])
138 or post_data.get('uid')),
b90dbe6c 139 'thumbnail': url_or_none(
140 urljoin(self._MEDIA_BASE_URL, post_data.get('main'))
141 or self._html_search_meta(['og:image', 'image'], webpage, 'thumbnail', fatal=False)),
142 'timestamp': float_or_none(dict_get(post_data, ['cdate', 'udate']), scale=1000),
85187609 143 'duration': float_or_none(post_data.get('vid_dur')),
144 'tags': post_data.get('htgs'),
145 }
82b51767 146
147
148class GettrStreamingIE(GettrBaseIE):
149 _VALID_URL = GettrBaseIE._BASE_REGEX + r'streaming/(?P<id>[a-z0-9]+)'
150
151 _TESTS = [{
152 'url': 'https://gettr.com/streaming/psoiulc122',
153 'info_dict': {
154 'id': 'psoiulc122',
155 'ext': 'mp4',
156 'description': 'md5:56bca4b8f48f1743d9fd03d49c723017',
157 'view_count': int,
158 'uploader': 'Corona Investigative Committee',
159 'uploader_id': 'coronacommittee',
160 'duration': 5180.184,
161 'thumbnail': r're:^https?://.+',
162 'title': 'Day 1: Opening Session of the Grand Jury Proceeding',
163 'timestamp': 1644080997.164,
164 'upload_date': '20220205',
165 }
166 }, {
167 'url': 'https://gettr.com/streaming/psfmeefcc1',
168 'info_dict': {
169 'id': 'psfmeefcc1',
170 'ext': 'mp4',
171 'title': 'Session 90: "The Virus Of Power"',
172 'view_count': int,
173 'uploader_id': 'coronacommittee',
174 'description': 'md5:98986acdf656aa836bf36f9c9704c65b',
175 'uploader': 'Corona Investigative Committee',
176 'thumbnail': r're:^https?://.+',
177 'duration': 21872.507,
178 'timestamp': 1643976662.858,
179 'upload_date': '20220204',
180 }
181 }]
182
183 def _real_extract(self, url):
184 video_id = self._match_id(url)
185 video_info = self._call_api('live/join/%s' % video_id, video_id, data={})
186
187 live_info = video_info['broadcast']
188 live_url = url_or_none(live_info.get('url'))
189
190 formats, subtitles = self._extract_m3u8_formats_and_subtitles(
191 live_url, video_id, ext='mp4',
192 entry_protocol='m3u8_native', m3u8_id='hls', fatal=False) if live_url else ([], {})
193
194 thumbnails = [{
195 'url': urljoin(self._MEDIA_BASE_URL, thumbnail),
b90dbe6c 196 } for thumbnail in try_get(video_info, lambda x: x['postData']['imgs'], list) or []]
82b51767 197
198 self._sort_formats(formats)
199
200 return {
201 'id': video_id,
b90dbe6c 202 'title': try_get(video_info, lambda x: x['postData']['ttl'], str),
203 'description': try_get(video_info, lambda x: x['postData']['dsc'], str),
82b51767 204 'formats': formats,
205 'subtitles': subtitles,
206 'thumbnails': thumbnails,
b90dbe6c 207 'uploader': try_get(video_info, lambda x: x['liveHostInfo']['nickname'], str),
208 'uploader_id': try_get(video_info, lambda x: x['liveHostInfo']['_id'], str),
82b51767 209 'view_count': int_or_none(live_info.get('viewsCount')),
210 'timestamp': float_or_none(live_info.get('startAt'), scale=1000),
211 'duration': float_or_none(live_info.get('duration'), scale=1000),
212 'is_live': bool_or_none(live_info.get('isLive')),
213 }