boosty.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import json
  2. import urllib.parse
  3. from .common import InfoExtractor
  4. from .youtube import YoutubeIE
  5. from ..utils import (
  6. ExtractorError,
  7. bug_reports_message,
  8. int_or_none,
  9. qualities,
  10. str_or_none,
  11. url_or_none,
  12. )
  13. from ..utils.traversal import traverse_obj
  14. class BoostyIE(InfoExtractor):
  15. _VALID_URL = r'https?://(?:www\.)?boosty\.to/(?P<user>[^/#?]+)/posts/(?P<post_id>[^/#?]+)'
  16. _TESTS = [{
  17. # single ok_video
  18. 'url': 'https://boosty.to/kuplinov/posts/e55d050c-e3bb-4873-a7db-ac7a49b40c38',
  19. 'info_dict': {
  20. 'id': 'd7473824-352e-48e2-ae53-d4aa39459968',
  21. 'title': 'phasma_3',
  22. 'channel': 'Kuplinov',
  23. 'channel_id': '7958701',
  24. 'timestamp': 1655031975,
  25. 'upload_date': '20220612',
  26. 'release_timestamp': 1655049000,
  27. 'release_date': '20220612',
  28. 'modified_timestamp': 1668680993,
  29. 'modified_date': '20221117',
  30. 'tags': ['куплинов', 'phasmophobia'],
  31. 'like_count': int,
  32. 'ext': 'mp4',
  33. 'duration': 105,
  34. 'view_count': int,
  35. 'thumbnail': r're:^https://i\.mycdn\.me/videoPreview\?',
  36. },
  37. }, {
  38. # multiple ok_video
  39. 'url': 'https://boosty.to/maddyson/posts/0c652798-3b35-471f-8b48-a76a0b28736f',
  40. 'info_dict': {
  41. 'id': '0c652798-3b35-471f-8b48-a76a0b28736f',
  42. 'title': 'то что не пропустил юта6',
  43. 'channel': 'Илья Давыдов',
  44. 'channel_id': '6808257',
  45. 'timestamp': 1694017040,
  46. 'upload_date': '20230906',
  47. 'release_timestamp': 1694017040,
  48. 'release_date': '20230906',
  49. 'modified_timestamp': 1694071178,
  50. 'modified_date': '20230907',
  51. 'like_count': int,
  52. },
  53. 'playlist_count': 3,
  54. 'playlist': [{
  55. 'info_dict': {
  56. 'id': 'cc325a9f-a563-41c6-bf47-516c1b506c9a',
  57. 'title': 'то что не пропустил юта6',
  58. 'channel': 'Илья Давыдов',
  59. 'channel_id': '6808257',
  60. 'timestamp': 1694017040,
  61. 'upload_date': '20230906',
  62. 'release_timestamp': 1694017040,
  63. 'release_date': '20230906',
  64. 'modified_timestamp': 1694071178,
  65. 'modified_date': '20230907',
  66. 'like_count': int,
  67. 'ext': 'mp4',
  68. 'duration': 31204,
  69. 'view_count': int,
  70. 'thumbnail': r're:^https://i\.mycdn\.me/videoPreview\?',
  71. },
  72. }, {
  73. 'info_dict': {
  74. 'id': 'd07b0a72-9493-4512-b54e-55ce468fd4b7',
  75. 'title': 'то что не пропустил юта6',
  76. 'channel': 'Илья Давыдов',
  77. 'channel_id': '6808257',
  78. 'timestamp': 1694017040,
  79. 'upload_date': '20230906',
  80. 'release_timestamp': 1694017040,
  81. 'release_date': '20230906',
  82. 'modified_timestamp': 1694071178,
  83. 'modified_date': '20230907',
  84. 'like_count': int,
  85. 'ext': 'mp4',
  86. 'duration': 25704,
  87. 'view_count': int,
  88. 'thumbnail': r're:^https://i\.mycdn\.me/videoPreview\?',
  89. },
  90. }, {
  91. 'info_dict': {
  92. 'id': '4a3bba32-78c8-422a-9432-2791aff60b42',
  93. 'title': 'то что не пропустил юта6',
  94. 'channel': 'Илья Давыдов',
  95. 'channel_id': '6808257',
  96. 'timestamp': 1694017040,
  97. 'upload_date': '20230906',
  98. 'release_timestamp': 1694017040,
  99. 'release_date': '20230906',
  100. 'modified_timestamp': 1694071178,
  101. 'modified_date': '20230907',
  102. 'like_count': int,
  103. 'ext': 'mp4',
  104. 'duration': 31867,
  105. 'view_count': int,
  106. 'thumbnail': r're:^https://i\.mycdn\.me/videoPreview\?',
  107. },
  108. }],
  109. }, {
  110. # single external video (youtube)
  111. 'url': 'https://boosty.to/denischuzhoy/posts/6094a487-bcec-4cf8-a453-43313b463c38',
  112. 'info_dict': {
  113. 'id': 'EXelTnve5lY',
  114. 'title': 'Послание Президента Федеральному Собранию | Класс народа',
  115. 'upload_date': '20210425',
  116. 'channel': 'Денис Чужой',
  117. 'tags': 'count:10',
  118. 'like_count': int,
  119. 'ext': 'mp4',
  120. 'duration': 816,
  121. 'view_count': int,
  122. 'thumbnail': r're:^https://i\.ytimg\.com/',
  123. 'age_limit': 0,
  124. 'availability': 'public',
  125. 'categories': list,
  126. 'channel_follower_count': int,
  127. 'channel_id': 'UCCzVNbWZfYpBfyofCCUD_0w',
  128. 'channel_is_verified': bool,
  129. 'channel_url': r're:^https://www\.youtube\.com/',
  130. 'comment_count': int,
  131. 'description': str,
  132. 'heatmap': 'count:100',
  133. 'live_status': str,
  134. 'playable_in_embed': bool,
  135. 'uploader': str,
  136. 'uploader_id': str,
  137. 'uploader_url': r're:^https://www\.youtube\.com/',
  138. },
  139. }]
  140. _MP4_TYPES = ('tiny', 'lowest', 'low', 'medium', 'high', 'full_hd', 'quad_hd', 'ultra_hd')
  141. def _extract_formats(self, player_urls, video_id):
  142. formats = []
  143. quality = qualities(self._MP4_TYPES)
  144. for player_url in traverse_obj(player_urls, lambda _, v: url_or_none(v['url'])):
  145. url = player_url['url']
  146. format_type = player_url.get('type')
  147. if format_type in ('hls', 'hls_live', 'live_ondemand_hls', 'live_playback_hls'):
  148. formats.extend(self._extract_m3u8_formats(url, video_id, m3u8_id='hls', fatal=False))
  149. elif format_type in ('dash', 'dash_live', 'live_playback_dash'):
  150. formats.extend(self._extract_mpd_formats(url, video_id, mpd_id='dash', fatal=False))
  151. elif format_type in self._MP4_TYPES:
  152. formats.append({
  153. 'url': url,
  154. 'ext': 'mp4',
  155. 'format_id': format_type,
  156. 'quality': quality(format_type),
  157. })
  158. else:
  159. self.report_warning(f'Unknown format type: {format_type!r}')
  160. return formats
  161. def _real_extract(self, url):
  162. user, post_id = self._match_valid_url(url).group('user', 'post_id')
  163. auth_headers = {}
  164. auth_cookie = self._get_cookies('https://boosty.to/').get('auth')
  165. if auth_cookie is not None:
  166. try:
  167. auth_data = json.loads(urllib.parse.unquote(auth_cookie.value))
  168. auth_headers['Authorization'] = f'Bearer {auth_data["accessToken"]}'
  169. except (json.JSONDecodeError, KeyError):
  170. self.report_warning(f'Failed to extract token from auth cookie{bug_reports_message()}')
  171. post = self._download_json(
  172. f'https://api.boosty.to/v1/blog/{user}/post/{post_id}', post_id,
  173. note='Downloading post data', errnote='Unable to download post data', headers=auth_headers)
  174. post_title = post.get('title')
  175. if not post_title:
  176. self.report_warning('Unable to extract post title. Falling back to parsing html page')
  177. webpage = self._download_webpage(url, video_id=post_id)
  178. post_title = self._og_search_title(webpage, default=None) or self._html_extract_title(webpage)
  179. common_metadata = {
  180. 'title': post_title,
  181. **traverse_obj(post, {
  182. 'channel': ('user', 'name', {str}),
  183. 'channel_id': ('user', 'id', {str_or_none}),
  184. 'timestamp': ('createdAt', {int_or_none}),
  185. 'release_timestamp': ('publishTime', {int_or_none}),
  186. 'modified_timestamp': ('updatedAt', {int_or_none}),
  187. 'tags': ('tags', ..., 'title', {str}),
  188. 'like_count': ('count', 'likes', {int_or_none}),
  189. }),
  190. }
  191. entries = []
  192. for item in traverse_obj(post, ('data', ..., {dict})):
  193. item_type = item.get('type')
  194. if item_type == 'video' and url_or_none(item.get('url')):
  195. entries.append(self.url_result(item['url'], YoutubeIE))
  196. elif item_type == 'ok_video':
  197. video_id = item.get('id') or post_id
  198. entries.append({
  199. 'id': video_id,
  200. 'formats': self._extract_formats(item.get('playerUrls'), video_id),
  201. **common_metadata,
  202. **traverse_obj(item, {
  203. 'title': ('title', {str}),
  204. 'duration': ('duration', {int_or_none}),
  205. 'view_count': ('viewsCounter', {int_or_none}),
  206. 'thumbnail': (('previewUrl', 'defaultPreview'), {url_or_none}),
  207. }, get_all=False)})
  208. if not entries and not post.get('hasAccess'):
  209. self.raise_login_required('This post requires a subscription', metadata_available=True)
  210. elif not entries:
  211. raise ExtractorError('No videos found', expected=True)
  212. if len(entries) == 1:
  213. return entries[0]
  214. return self.playlist_result(entries, post_id, post_title, **common_metadata)