]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/banbye.py
[extractor] Deprecate `_sort_formats`
[yt-dlp.git] / yt_dlp / extractor / banbye.py
1 import math
2
3 from .common import InfoExtractor
4 from ..compat import (
5 compat_urllib_parse_urlparse,
6 compat_parse_qs,
7 )
8 from ..utils import (
9 format_field,
10 InAdvancePagedList,
11 traverse_obj,
12 unified_timestamp,
13 )
14
15
16 class 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
33 class 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 return {
84 'id': video_id,
85 'title': data.get('title'),
86 'description': data.get('desc'),
87 'uploader': traverse_obj(data, ('channel', 'name')),
88 'channel_id': data.get('channelId'),
89 'channel_url': format_field(data, 'channelId', 'https://banbye.com/channel/%s'),
90 'timestamp': unified_timestamp(data.get('publishedAt')),
91 'duration': data.get('duration'),
92 'tags': data.get('tags'),
93 'formats': formats,
94 'thumbnails': thumbnails,
95 'like_count': data.get('likes'),
96 'dislike_count': data.get('dislikes'),
97 'view_count': data.get('views'),
98 'comment_count': data.get('commentCount'),
99 }
100
101
102 class BanByeChannelIE(BanByeBaseIE):
103 _VALID_URL = r'https?://(?:www\.)?banbye.com/(?:en/)?channel/(?P<id>\w+)'
104 _TESTS = [{
105 'url': 'https://banbye.com/channel/ch_wrealu24',
106 'info_dict': {
107 'title': 'wRealu24',
108 'id': 'ch_wrealu24',
109 'description': 'md5:da54e48416b74dfdde20a04867c0c2f6',
110 },
111 'playlist_mincount': 791,
112 }, {
113 'url': 'https://banbye.com/channel/ch_wrealu24?playlist=p_Ld82N6gBw_OJ',
114 'info_dict': {
115 'title': 'Krzysztof Karoń',
116 'id': 'p_Ld82N6gBw_OJ',
117 },
118 'playlist_count': 9,
119 }]
120 _PAGE_SIZE = 100
121
122 def _real_extract(self, url):
123 channel_id = self._match_id(url)
124 playlist_id = self._extract_playlist_id(url)
125
126 if playlist_id:
127 return self._extract_playlist(playlist_id)
128
129 def page_func(page_num):
130 data = self._download_json(f'{self._API_BASE}/videos', channel_id, query={
131 'channelId': channel_id,
132 'sort': 'new',
133 'limit': self._PAGE_SIZE,
134 'offset': page_num * self._PAGE_SIZE,
135 }, note=f'Downloading page {page_num+1}')
136 return [
137 self.url_result(f"{self._VIDEO_BASE}/{video['_id']}", BanByeIE)
138 for video in data['items']
139 ]
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
147 return self.playlist_result(
148 entries, channel_id, channel_data.get('name'), channel_data.get('description'))