banbye.py 5.2 KB

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