]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/gettr.py
[youtube:comments] Add more options for limiting number of comments extracted (#1626)
[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 (
6 ExtractorError,
7 dict_get,
8 float_or_none,
9 int_or_none,
10 remove_end,
11 str_or_none,
12 try_get,
13 url_or_none,
14 urljoin,
15)
16
17
18class GettrIE(InfoExtractor):
19 _VALID_URL = r'https?://(www\.)?gettr\.com/post/(?P<id>[a-z0-9]+)'
20 _MEDIA_BASE_URL = 'https://media.gettr.com/'
21
22 _TESTS = [{
23 'url': 'https://www.gettr.com/post/pcf6uv838f',
24 'info_dict': {
25 'id': 'pcf6uv838f',
26 'title': 'md5:9086a646bbd06c41c4fe8e52b3c93454',
27 'description': 'md5:be0577f1e4caadc06de4a002da2bf287',
28 'ext': 'mp4',
29 'uploader': 'EpochTV',
30 'uploader_id': 'epochtv',
31 'thumbnail': r're:^https?://.+/out\.jpg',
32 'timestamp': 1632782451058,
33 'duration': 58.5585,
34 }
35 }, {
36 'url': 'https://gettr.com/post/p4iahp',
37 'info_dict': {
38 'id': 'p4iahp',
39 'title': 'md5:b03c07883db6fbc1aab88877a6c3b149',
40 'description': 'md5:741b7419d991c403196ed2ea7749a39d',
41 'ext': 'mp4',
42 'uploader': 'Neues Forum Freiheit',
43 'uploader_id': 'nf_freiheit',
44 'thumbnail': r're:^https?://.+/out\.jpg',
45 'timestamp': 1626594455017,
46 'duration': 23,
47 }
48 }]
49
50 def _real_extract(self, url):
51 post_id = self._match_id(url)
52 webpage = self._download_webpage(url, post_id)
53
54 api_data = self._download_json(
55 'https://api.gettr.com/u/post/%s?incl="poststats|userinfo"' % post_id, post_id)
56
57 post_data = try_get(api_data, lambda x: x['result']['data'])
58 user_data = try_get(api_data, lambda x: x['result']['aux']['uinf'][post_data['uid']]) or {}
59
60 if post_data.get('nfound'):
61 raise ExtractorError(post_data.get('txt'), expected=True)
62
63 title = description = str_or_none(
64 post_data.get('txt') or self._og_search_description(webpage))
65
66 uploader = str_or_none(
67 user_data.get('nickname')
68 or remove_end(self._og_search_title(webpage), ' on GETTR'))
69 if uploader:
70 title = '%s - %s' % (uploader, title)
71
72 if not dict_get(post_data, ['vid', 'ovid']):
73 raise ExtractorError('There\'s no video in this post.')
74
75 vid = post_data.get('vid')
76 ovid = post_data.get('ovid')
77
78 formats = self._extract_m3u8_formats(
79 urljoin(self._MEDIA_BASE_URL, vid), post_id, 'mp4',
80 entry_protocol='m3u8_native', m3u8_id='hls') if vid else []
81
82 if ovid:
83 formats.append({
84 'url': urljoin(self._MEDIA_BASE_URL, ovid),
85 'format_id': 'ovid',
86 'ext': 'mp4',
87 'width': int_or_none(post_data.get('vid_wid')),
88 'height': int_or_none(post_data.get('vid_hgt')),
89 'source_preference': 1,
90 'quality': 1,
91 })
92
93 self._sort_formats(formats)
94
95 return {
96 'id': post_id,
97 'title': title,
98 'description': description,
99 'thumbnail': url_or_none(
100 urljoin(self._MEDIA_BASE_URL, post_data.get('main'))
101 or self._og_search_thumbnail(webpage)),
102 'timestamp': int_or_none(post_data.get('cdate')),
103 'uploader_id': str_or_none(
104 dict_get(user_data, ['_id', 'username'])
105 or post_data.get('uid')),
106 'uploader': uploader,
107 'formats': formats,
108 'duration': float_or_none(post_data.get('vid_dur')),
109 'tags': post_data.get('htgs'),
110 }