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