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