]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/gab.py
[extractor] Deprecate `_sort_formats`
[yt-dlp.git] / yt_dlp / extractor / gab.py
CommitLineData
2acf2ce5
A
1import re
2
3from .common import InfoExtractor
4from ..utils import (
5 clean_html,
1ee316a3 6 int_or_none,
7 parse_codecs,
8 parse_duration,
2acf2ce5 9 str_to_int,
1ee316a3 10 unified_timestamp
2acf2ce5
A
11)
12
13
14class GabTVIE(InfoExtractor):
73f035e1 15 _VALID_URL = r'https?://tv\.gab\.com/channel/[^/]+/view/(?P<id>[a-z0-9-]+)'
2acf2ce5
A
16 _TESTS = [{
17 'url': 'https://tv.gab.com/channel/wurzelroot/view/why-was-america-in-afghanistan-61217eacea5665de450d0488',
18 'info_dict': {
19 'id': '61217eacea5665de450d0488',
20 'ext': 'mp4',
21 'title': 'WHY WAS AMERICA IN AFGHANISTAN - AMERICA FIRST AGAINST AMERICAN OLIGARCHY',
22 'description': None,
23 'uploader': 'Wurzelroot',
24 'uploader_id': '608fb0a85738fd1974984f7d',
25 'thumbnail': 'https://tv.gab.com/image/61217eacea5665de450d0488',
26 }
27 }]
28
29 def _real_extract(self, url):
30 id = self._match_id(url).split('-')[-1]
31 webpage = self._download_webpage(url, id)
32 channel_id = self._search_regex(r'data-channel-id=\"(?P<channel_id>[^\"]+)', webpage, 'channel_id')
33 channel_name = self._search_regex(r'data-channel-name=\"(?P<channel_id>[^\"]+)', webpage, 'channel_name')
34 title = self._search_regex(r'data-episode-title=\"(?P<channel_id>[^\"]+)', webpage, 'title')
35 view_key = self._search_regex(r'data-view-key=\"(?P<channel_id>[^\"]+)', webpage, 'view_key')
1ee316a3 36 description = clean_html(
37 self._html_search_regex(self._meta_regex('description'), webpage, 'description', group='content')) or None
38 available_resolutions = re.findall(r'<a\ data-episode-id=\"%s\"\ data-resolution=\"(?P<resolution>[^\"]+)' % id,
39 webpage)
2acf2ce5
A
40
41 formats = []
42 for resolution in available_resolutions:
43 frmt = {
44 'url': f'https://tv.gab.com/media/{id}?viewKey={view_key}&r={resolution}',
45 'format_id': resolution,
46 'vcodec': 'h264',
47 'acodec': 'aac',
48 'ext': 'mp4'
49 }
50 if 'audio-' in resolution:
51 frmt['abr'] = str_to_int(resolution.replace('audio-', ''))
52 frmt['height'] = 144
53 frmt['quality'] = -10
54 else:
55 frmt['height'] = str_to_int(resolution.replace('p', ''))
56 formats.append(frmt)
2acf2ce5
A
57
58 return {
59 'id': id,
60 'title': title,
61 'formats': formats,
62 'description': description,
63 'uploader': channel_name,
64 'uploader_id': channel_id,
65 'thumbnail': f'https://tv.gab.com/image/{id}',
66 }
1ee316a3 67
68
69class GabIE(InfoExtractor):
70 _VALID_URL = r'https?://(?:www\.)?gab\.com/[^/]+/posts/(?P<id>\d+)'
71 _TESTS = [{
72 'url': 'https://gab.com/SomeBitchIKnow/posts/107163961867310434',
73 'md5': '8ca34fb00f1e1033b5c5988d79ec531d',
74 'info_dict': {
75 'id': '107163961867310434-0',
76 'ext': 'mp4',
77 'title': 'L on Gab',
78 'uploader_id': '946600',
79 'uploader': 'SomeBitchIKnow',
80 'description': 'md5:204055fafd5e1a519f5d6db953567ca3',
81 'timestamp': 1635192289,
82 'upload_date': '20211025',
83 }
84 }, {
85 'url': 'https://gab.com/TheLonelyProud/posts/107045884469287653',
86 'md5': 'f9cefcfdff6418e392611a828d47839d',
87 'info_dict': {
88 'id': '107045884469287653-0',
89 'ext': 'mp4',
90 'title': 'Jody Sadowski on Gab',
91 'uploader_id': '1390705',
92 'timestamp': 1633390571,
93 'upload_date': '20211004',
94 'uploader': 'TheLonelyProud',
95 }
96 }]
97
98 def _real_extract(self, url):
99 post_id = self._match_id(url)
100 json_data = self._download_json(f'https://gab.com/api/v1/statuses/{post_id}', post_id)
101
102 entries = []
103 for idx, media in enumerate(json_data['media_attachments']):
104 if media.get('type') not in ('video', 'gifv'):
105 continue
106 metadata = media['meta']
107 format_metadata = {
108 'acodec': parse_codecs(metadata.get('audio_encode')).get('acodec'),
109 'asr': int_or_none((metadata.get('audio_bitrate') or '').split(' ')[0]),
110 'fps': metadata.get('fps'),
111 }
112
113 formats = [{
114 'url': url,
115 'width': f.get('width'),
116 'height': f.get('height'),
117 'tbr': int_or_none(f.get('bitrate'), scale=1000),
118 **format_metadata,
119 } for url, f in ((media.get('url'), metadata.get('original') or {}),
120 (media.get('source_mp4'), metadata.get('playable') or {})) if url]
121
1ee316a3 122 author = json_data.get('account') or {}
123 entries.append({
124 'id': f'{post_id}-{idx}',
125 'title': f'{json_data["account"]["display_name"]} on Gab',
126 'timestamp': unified_timestamp(json_data.get('created_at')),
127 'formats': formats,
128 'description': clean_html(json_data.get('content')),
129 'duration': metadata.get('duration') or parse_duration(metadata.get('length')),
130 'like_count': json_data.get('favourites_count'),
131 'comment_count': json_data.get('replies_count'),
132 'repost_count': json_data.get('reblogs_count'),
133 'uploader': author.get('username'),
134 'uploader_id': author.get('id'),
135 'uploader_url': author.get('url'),
136 })
137
138 if len(entries) > 1:
139 return self.playlist_result(entries, post_id)
140
141 return entries[0]