gab.py 5.6 KB

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