banbye.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import math
  2. import urllib.parse
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. InAdvancePagedList,
  6. format_field,
  7. traverse_obj,
  8. unified_timestamp,
  9. )
  10. class BanByeBaseIE(InfoExtractor):
  11. _API_BASE = 'https://api.banbye.com'
  12. _CDN_BASE = 'https://cdn.banbye.com'
  13. _VIDEO_BASE = 'https://banbye.com/watch'
  14. @staticmethod
  15. def _extract_playlist_id(url, param='playlist'):
  16. return urllib.parse.parse_qs(
  17. urllib.parse.urlparse(url).query).get(param, [None])[0]
  18. def _extract_playlist(self, playlist_id):
  19. data = self._download_json(f'{self._API_BASE}/playlists/{playlist_id}', playlist_id)
  20. return self.playlist_result([
  21. self.url_result(f'{self._VIDEO_BASE}/{video_id}', BanByeIE)
  22. for video_id in data['videoIds']], playlist_id, data.get('name'))
  23. class BanByeIE(BanByeBaseIE):
  24. _VALID_URL = r'https?://(?:www\.)?banbye\.com/(?:en/)?watch/(?P<id>[\w-]+)'
  25. _TESTS = [{
  26. 'url': 'https://banbye.com/watch/v_ytfmvkVYLE8T',
  27. 'md5': '2f4ea15c5ca259a73d909b2cfd558eb5',
  28. 'info_dict': {
  29. 'id': 'v_ytfmvkVYLE8T',
  30. 'ext': 'mp4',
  31. 'title': 'md5:5ec098f88a0d796f987648de6322ba0f',
  32. 'description': 'md5:4d94836e73396bc18ef1fa0f43e5a63a',
  33. 'uploader': 'wRealu24',
  34. 'channel_id': 'ch_wrealu24',
  35. 'channel_url': 'https://banbye.com/channel/ch_wrealu24',
  36. 'timestamp': 1647604800,
  37. 'upload_date': '20220318',
  38. 'duration': 1931,
  39. 'thumbnail': r're:https?://.*\.webp',
  40. 'tags': 'count:5',
  41. 'like_count': int,
  42. 'dislike_count': int,
  43. 'view_count': int,
  44. 'comment_count': int,
  45. },
  46. }, {
  47. 'url': 'https://banbye.com/watch/v_2JjQtqjKUE_F?playlistId=p_Ld82N6gBw_OJ',
  48. 'info_dict': {
  49. 'title': 'Krzysztof Karoń',
  50. 'id': 'p_Ld82N6gBw_OJ',
  51. },
  52. 'playlist_mincount': 9,
  53. }, {
  54. 'url': 'https://banbye.com/watch/v_kb6_o1Kyq-CD',
  55. 'info_dict': {
  56. 'id': 'v_kb6_o1Kyq-CD',
  57. 'ext': 'mp4',
  58. 'title': 'Co tak naprawdę dzieje się we Francji?! Czy Warszawa a potem cała Polska będzie drugim Paryżem?!🤔🇵🇱',
  59. 'description': 'md5:82be4c0e13eae8ea1ca8b9f2e07226a8',
  60. 'uploader': 'Marcin Rola - MOIM ZDANIEM!🇵🇱',
  61. 'channel_id': 'ch_QgWnHvDG2fo5',
  62. 'channel_url': 'https://banbye.com/channel/ch_QgWnHvDG2fo5',
  63. 'duration': 597,
  64. 'timestamp': 1688642656,
  65. 'upload_date': '20230706',
  66. 'thumbnail': 'https://cdn.banbye.com/video/v_kb6_o1Kyq-CD/96.webp',
  67. 'tags': ['Paryż', 'Francja', 'Polska', 'Imigranci', 'Morawiecki', 'Tusk'],
  68. 'like_count': int,
  69. 'dislike_count': int,
  70. 'view_count': int,
  71. 'comment_count': int,
  72. },
  73. }]
  74. def _real_extract(self, url):
  75. video_id = self._match_id(url)
  76. playlist_id = self._extract_playlist_id(url, 'playlistId')
  77. if self._yes_playlist(playlist_id, video_id):
  78. return self._extract_playlist(playlist_id)
  79. data = self._download_json(f'{self._API_BASE}/videos/{video_id}', video_id)
  80. thumbnails = [{
  81. 'id': f'{quality}p',
  82. 'url': f'{self._CDN_BASE}/video/{video_id}/{quality}.webp',
  83. } for quality in [48, 96, 144, 240, 512, 1080]]
  84. formats = [{
  85. 'format_id': f'http-{quality}p',
  86. 'quality': quality,
  87. 'url': f'{self._CDN_BASE}/video/{video_id}/{quality}.mp4',
  88. } for quality in data['quality']]
  89. return {
  90. 'id': video_id,
  91. 'title': data.get('title'),
  92. 'description': data.get('desc'),
  93. 'uploader': traverse_obj(data, ('channel', 'name')),
  94. 'channel_id': data.get('channelId'),
  95. 'channel_url': format_field(data, 'channelId', 'https://banbye.com/channel/%s'),
  96. 'timestamp': unified_timestamp(data.get('publishedAt')),
  97. 'duration': data.get('duration'),
  98. 'tags': data.get('tags'),
  99. 'formats': formats,
  100. 'thumbnails': thumbnails,
  101. 'like_count': data.get('likes'),
  102. 'dislike_count': data.get('dislikes'),
  103. 'view_count': data.get('views'),
  104. 'comment_count': data.get('commentCount'),
  105. }
  106. class BanByeChannelIE(BanByeBaseIE):
  107. _VALID_URL = r'https?://(?:www\.)?banbye\.com/(?:en/)?channel/(?P<id>\w+)'
  108. _TESTS = [{
  109. 'url': 'https://banbye.com/channel/ch_wrealu24',
  110. 'info_dict': {
  111. 'title': 'wRealu24',
  112. 'id': 'ch_wrealu24',
  113. 'description': 'md5:da54e48416b74dfdde20a04867c0c2f6',
  114. },
  115. 'playlist_mincount': 791,
  116. }, {
  117. 'url': 'https://banbye.com/channel/ch_wrealu24?playlist=p_Ld82N6gBw_OJ',
  118. 'info_dict': {
  119. 'title': 'Krzysztof Karoń',
  120. 'id': 'p_Ld82N6gBw_OJ',
  121. },
  122. 'playlist_count': 9,
  123. }]
  124. _PAGE_SIZE = 100
  125. def _real_extract(self, url):
  126. channel_id = self._match_id(url)
  127. playlist_id = self._extract_playlist_id(url)
  128. if playlist_id:
  129. return self._extract_playlist(playlist_id)
  130. def page_func(page_num):
  131. data = self._download_json(f'{self._API_BASE}/videos', channel_id, query={
  132. 'channelId': channel_id,
  133. 'sort': 'new',
  134. 'limit': self._PAGE_SIZE,
  135. 'offset': page_num * self._PAGE_SIZE,
  136. }, note=f'Downloading page {page_num + 1}')
  137. return [
  138. self.url_result(f"{self._VIDEO_BASE}/{video['_id']}", BanByeIE)
  139. for video in data['items']
  140. ]
  141. channel_data = self._download_json(f'{self._API_BASE}/channels/{channel_id}', channel_id)
  142. entries = InAdvancePagedList(
  143. page_func,
  144. math.ceil(channel_data['videoCount'] / self._PAGE_SIZE),
  145. self._PAGE_SIZE)
  146. return self.playlist_result(
  147. entries, channel_id, channel_data.get('name'), channel_data.get('description'))