]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/banbye.py
[misc] Add `hatch`, `ruff`, `pre-commit` and improve dev docs (#7409)
[yt-dlp.git] / yt_dlp / extractor / banbye.py
CommitLineData
5b4bb715 1import math
2
3from .common import InfoExtractor
4from ..compat import (
5b4bb715 5 compat_parse_qs,
e897bd82 6 compat_urllib_parse_urlparse,
5b4bb715 7)
8from ..utils import (
5b4bb715 9 InAdvancePagedList,
e897bd82 10 format_field,
5b4bb715 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):
b634ba74 34 _VALID_URL = r'https?://(?:www\.)?banbye\.com/(?:en/)?watch/(?P<id>[\w-]+)'
5b4bb715 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 },
578a82e4
KG
62 'playlist_mincount': 9,
63 }, {
64 'url': 'https://banbye.com/watch/v_kb6_o1Kyq-CD',
65 'info_dict': {
66 'id': 'v_kb6_o1Kyq-CD',
67 'ext': 'mp4',
68 'title': 'Co tak naprawdę dzieje się we Francji?! Czy Warszawa a potem cała Polska będzie drugim Paryżem?!🤔🇵🇱',
69 'description': 'md5:82be4c0e13eae8ea1ca8b9f2e07226a8',
70 'uploader': 'Marcin Rola - MOIM ZDANIEM!🇵🇱',
71 'channel_id': 'ch_QgWnHvDG2fo5',
72 'channel_url': 'https://banbye.com/channel/ch_QgWnHvDG2fo5',
73 'duration': 597,
74 'timestamp': 1688642656,
75 'upload_date': '20230706',
76 'thumbnail': 'https://cdn.banbye.com/video/v_kb6_o1Kyq-CD/96.webp',
77 'tags': ['Paryż', 'Francja', 'Polska', 'Imigranci', 'Morawiecki', 'Tusk'],
78 'like_count': int,
79 'dislike_count': int,
80 'view_count': int,
81 'comment_count': int,
82 },
5b4bb715 83 }]
84
85 def _real_extract(self, url):
86 video_id = self._match_id(url)
87 playlist_id = self._extract_playlist_id(url, 'playlistId')
88
89 if self._yes_playlist(playlist_id, video_id):
90 return self._extract_playlist(playlist_id)
91
92 data = self._download_json(f'{self._API_BASE}/videos/{video_id}', video_id)
93 thumbnails = [{
94 'id': f'{quality}p',
95 'url': f'{self._CDN_BASE}/video/{video_id}/{quality}.webp',
96 } for quality in [48, 96, 144, 240, 512, 1080]]
97 formats = [{
98 'format_id': f'http-{quality}p',
99 'quality': quality,
100 'url': f'{self._CDN_BASE}/video/{video_id}/{quality}.mp4',
101 } for quality in data['quality']]
102
5b4bb715 103 return {
104 'id': video_id,
105 'title': data.get('title'),
106 'description': data.get('desc'),
107 'uploader': traverse_obj(data, ('channel', 'name')),
108 'channel_id': data.get('channelId'),
109 'channel_url': format_field(data, 'channelId', 'https://banbye.com/channel/%s'),
110 'timestamp': unified_timestamp(data.get('publishedAt')),
111 'duration': data.get('duration'),
112 'tags': data.get('tags'),
113 'formats': formats,
114 'thumbnails': thumbnails,
115 'like_count': data.get('likes'),
116 'dislike_count': data.get('dislikes'),
117 'view_count': data.get('views'),
118 'comment_count': data.get('commentCount'),
119 }
120
121
122class BanByeChannelIE(BanByeBaseIE):
b634ba74 123 _VALID_URL = r'https?://(?:www\.)?banbye\.com/(?:en/)?channel/(?P<id>\w+)'
5b4bb715 124 _TESTS = [{
125 'url': 'https://banbye.com/channel/ch_wrealu24',
126 'info_dict': {
127 'title': 'wRealu24',
128 'id': 'ch_wrealu24',
129 'description': 'md5:da54e48416b74dfdde20a04867c0c2f6',
130 },
131 'playlist_mincount': 791,
132 }, {
133 'url': 'https://banbye.com/channel/ch_wrealu24?playlist=p_Ld82N6gBw_OJ',
134 'info_dict': {
135 'title': 'Krzysztof Karoń',
136 'id': 'p_Ld82N6gBw_OJ',
137 },
138 'playlist_count': 9,
139 }]
140 _PAGE_SIZE = 100
141
142 def _real_extract(self, url):
143 channel_id = self._match_id(url)
144 playlist_id = self._extract_playlist_id(url)
145
146 if playlist_id:
147 return self._extract_playlist(playlist_id)
148
149 def page_func(page_num):
150 data = self._download_json(f'{self._API_BASE}/videos', channel_id, query={
151 'channelId': channel_id,
152 'sort': 'new',
153 'limit': self._PAGE_SIZE,
154 'offset': page_num * self._PAGE_SIZE,
f9fb3ce8 155 }, note=f'Downloading page {page_num + 1}')
5b4bb715 156 return [
157 self.url_result(f"{self._VIDEO_BASE}/{video['_id']}", BanByeIE)
158 for video in data['items']
159 ]
160
161 channel_data = self._download_json(f'{self._API_BASE}/channels/{channel_id}', channel_id)
162 entries = InAdvancePagedList(
163 page_func,
164 math.ceil(channel_data['videoCount'] / self._PAGE_SIZE),
165 self._PAGE_SIZE)
166
167 return self.playlist_result(
168 entries, channel_id, channel_data.get('name'), channel_data.get('description'))