gab.py 5.6 KB

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