]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/zingmp3.py
[cleanup] Misc cleanup
[yt-dlp.git] / yt_dlp / extractor / zingmp3.py
1 import functools
2 import hashlib
3 import hmac
4 import json
5 import urllib.parse
6
7 from .common import InfoExtractor
8 from ..utils import (
9 OnDemandPagedList,
10 int_or_none,
11 traverse_obj,
12 urljoin,
13 )
14
15
16 class ZingMp3BaseIE(InfoExtractor):
17 _VALID_URL_TMPL = r'https?://(?:mp3\.zing|zingmp3)\.vn/(?P<type>(?:%s))/[^/?#]+/(?P<id>\w+)(?:\.html|\?)'
18 _GEO_COUNTRIES = ['VN']
19 _DOMAIN = 'https://zingmp3.vn'
20 _PER_PAGE = 50
21 _API_SLUGS = {
22 # Audio/video
23 'bai-hat': '/api/v2/page/get/song',
24 'embed': '/api/v2/page/get/song',
25 'video-clip': '/api/v2/page/get/video',
26 'lyric': '/api/v2/lyric/get/lyric',
27 'song-streaming': '/api/v2/song/get/streaming',
28 # Playlist
29 'playlist': '/api/v2/page/get/playlist',
30 'album': '/api/v2/page/get/playlist',
31 # Chart
32 'zing-chart': '/api/v2/page/get/chart-home',
33 'zing-chart-tuan': '/api/v2/page/get/week-chart',
34 'moi-phat-hanh': '/api/v2/page/get/newrelease-chart',
35 'the-loai-video': '/api/v2/video/get/list',
36 # User
37 'info-artist': '/api/v2/page/get/artist',
38 'user-list-song': '/api/v2/song/get/list',
39 'user-list-video': '/api/v2/video/get/list',
40 }
41
42 def _api_url(self, url_type, params):
43 api_slug = self._API_SLUGS[url_type]
44 params.update({'ctime': '1'})
45 sha256 = hashlib.sha256(
46 ''.join(f'{k}={v}' for k, v in sorted(params.items())).encode()).hexdigest()
47 data = {
48 **params,
49 'apiKey': '88265e23d4284f25963e6eedac8fbfa3',
50 'sig': hmac.new(
51 b'2aa2d1c561e809b267f3638c4a307aab', f'{api_slug}{sha256}'.encode(), hashlib.sha512).hexdigest(),
52 }
53 return f'{self._DOMAIN}{api_slug}?{urllib.parse.urlencode(data)}'
54
55 def _call_api(self, url_type, params, display_id=None, **kwargs):
56 resp = self._download_json(
57 self._api_url(url_type, params), display_id or params.get('id'),
58 note=f'Downloading {url_type} JSON metadata', **kwargs)
59 return (resp or {}).get('data') or {}
60
61 def _real_initialize(self):
62 if not self._cookies_passed:
63 self._request_webpage(
64 self._api_url('bai-hat', {'id': ''}), None, note='Updating cookies')
65
66 def _parse_items(self, items):
67 for url in traverse_obj(items, (..., 'link')) or []:
68 yield self.url_result(urljoin(self._DOMAIN, url))
69
70
71 class ZingMp3IE(ZingMp3BaseIE):
72 _VALID_URL = ZingMp3BaseIE._VALID_URL_TMPL % 'bai-hat|video-clip|embed'
73 IE_NAME = 'zingmp3'
74 IE_DESC = 'zingmp3.vn'
75 _TESTS = [{
76 'url': 'https://mp3.zing.vn/bai-hat/Xa-Mai-Xa-Bao-Thy/ZWZB9WAB.html',
77 'md5': 'ead7ae13693b3205cbc89536a077daed',
78 'info_dict': {
79 'id': 'ZWZB9WAB',
80 'title': 'Xa Mãi Xa',
81 'ext': 'mp3',
82 'thumbnail': r're:^https?://.+\.jpg',
83 'subtitles': {
84 'origin': [{
85 'ext': 'lrc',
86 }]
87 },
88 'duration': 255,
89 'track': 'Xa Mãi Xa',
90 'artist': 'Bảo Thy',
91 'album': 'Special Album',
92 'album_artist': 'Bảo Thy',
93 },
94 }, {
95 'url': 'https://zingmp3.vn/video-clip/Suong-Hoa-Dua-Loi-K-ICM-RYO/ZO8ZF7C7.html',
96 'md5': '3c2081e79471a2f4a3edd90b70b185ea',
97 'info_dict': {
98 'id': 'ZO8ZF7C7',
99 'title': 'Sương Hoa Đưa Lối',
100 'ext': 'mp4',
101 'thumbnail': r're:^https?://.+\.jpg',
102 'duration': 207,
103 'track': 'Sương Hoa Đưa Lối',
104 'artist': 'K-ICM, RYO',
105 'album': 'Sương Hoa Đưa Lối (Single)',
106 'album_artist': 'K-ICM, RYO',
107 },
108 }, {
109 'url': 'https://zingmp3.vn/bai-hat/Nguoi-Yeu-Toi-Lanh-Lung-Sat-Da-Mr-Siro/ZZ6IW7OU.html',
110 'md5': '3e9f7a9bd0d965573dbff8d7c68b629d',
111 'info_dict': {
112 'id': 'ZZ6IW7OU',
113 'title': 'Người Yêu Tôi Lạnh Lùng Sắt Đá',
114 'ext': 'mp3',
115 'thumbnail': r're:^https?://.+\.jpg',
116 'duration': 303,
117 'track': 'Người Yêu Tôi Lạnh Lùng Sắt Đá',
118 'artist': 'Mr. Siro',
119 'album': 'Người Yêu Tôi Lạnh Lùng Sắt Đá (Single)',
120 'album_artist': 'Mr. Siro',
121 },
122 }, {
123 'url': 'https://zingmp3.vn/embed/song/ZWZEI76B?start=false',
124 'only_matching': True,
125 }, {
126 'url': 'https://zingmp3.vn/bai-hat/Xa-Mai-Xa-Bao-Thy/ZWZB9WAB.html',
127 'only_matching': True,
128 }]
129
130 def _real_extract(self, url):
131 song_id, url_type = self._match_valid_url(url).group('id', 'type')
132 item = self._call_api(url_type, {'id': song_id})
133
134 item_id = item.get('encodeId') or song_id
135 if url_type == 'video-clip':
136 source = item.get('streaming')
137 source['mp4'] = self._download_json(
138 'http://api.mp3.zing.vn/api/mobile/video/getvideoinfo', item_id,
139 query={'requestdata': json.dumps({'id': item_id})},
140 note='Downloading mp4 JSON metadata').get('source')
141 else:
142 source = self._call_api('song-streaming', {'id': item_id})
143
144 formats = []
145 for k, v in (source or {}).items():
146 if not v or v == 'VIP':
147 continue
148 if k not in ('mp4', 'hls'):
149 formats.append({
150 'ext': 'mp3',
151 'format_id': k,
152 'tbr': int_or_none(k),
153 'url': self._proto_relative_url(v),
154 'vcodec': 'none',
155 })
156 continue
157 for res, video_url in v.items():
158 if not video_url:
159 continue
160 if k == 'hls':
161 formats.extend(self._extract_m3u8_formats(video_url, item_id, 'mp4', m3u8_id=k, fatal=False))
162 continue
163 formats.append({
164 'format_id': f'mp4-{res}',
165 'url': video_url,
166 'height': int_or_none(res),
167 })
168
169 if not formats and item.get('msg') == 'Sorry, this content is not available in your country.':
170 self.raise_geo_restricted(countries=self._GEO_COUNTRIES, metadata_available=True)
171 self._sort_formats(formats)
172
173 lyric = item.get('lyric') or self._call_api('lyric', {'id': item_id}, fatal=False).get('file')
174
175 return {
176 'id': item_id,
177 'title': traverse_obj(item, 'title', 'alias'),
178 'thumbnail': traverse_obj(item, 'thumbnail', 'thumbnailM'),
179 'duration': int_or_none(item.get('duration')),
180 'track': traverse_obj(item, 'title', 'alias'),
181 'artist': traverse_obj(item, 'artistsNames', 'artists_names'),
182 'album': traverse_obj(item, ('album', ('name', 'title')), get_all=False),
183 'album_artist': traverse_obj(item, ('album', ('artistsNames', 'artists_names')), get_all=False),
184 'formats': formats,
185 'subtitles': {'origin': [{'url': lyric}]} if lyric else None,
186 }
187
188
189 class ZingMp3AlbumIE(ZingMp3BaseIE):
190 _VALID_URL = ZingMp3BaseIE._VALID_URL_TMPL % 'album|playlist'
191 _TESTS = [{
192 'url': 'http://mp3.zing.vn/album/Lau-Dai-Tinh-Ai-Bang-Kieu-Minh-Tuyet/ZWZBWDAF.html',
193 'info_dict': {
194 'id': 'ZWZBWDAF',
195 'title': 'Lâu Đài Tình Ái',
196 },
197 'playlist_mincount': 9,
198 }, {
199 'url': 'https://zingmp3.vn/album/Nhung-Bai-Hat-Hay-Nhat-Cua-Mr-Siro-Mr-Siro/ZWZAEZZD.html',
200 'info_dict': {
201 'id': 'ZWZAEZZD',
202 'title': 'Những Bài Hát Hay Nhất Của Mr. Siro',
203 },
204 'playlist_mincount': 49,
205 }, {
206 'url': 'http://mp3.zing.vn/playlist/Duong-Hong-Loan-apollobee/IWCAACCB.html',
207 'only_matching': True,
208 }, {
209 'url': 'https://zingmp3.vn/album/Lau-Dai-Tinh-Ai-Bang-Kieu-Minh-Tuyet/ZWZBWDAF.html',
210 'only_matching': True,
211 }]
212 IE_NAME = 'zingmp3:album'
213
214 def _real_extract(self, url):
215 song_id, url_type = self._match_valid_url(url).group('id', 'type')
216 data = self._call_api(url_type, {'id': song_id})
217 return self.playlist_result(
218 self._parse_items(traverse_obj(data, ('song', 'items'))),
219 traverse_obj(data, 'id', 'encodeId'), traverse_obj(data, 'name', 'title'))
220
221
222 class ZingMp3ChartHomeIE(ZingMp3BaseIE):
223 _VALID_URL = r'https?://(?:mp3\.zing|zingmp3)\.vn/(?P<id>(?:zing-chart|moi-phat-hanh))/?(?:[#?]|$)'
224 _TESTS = [{
225 'url': 'https://zingmp3.vn/zing-chart',
226 'info_dict': {
227 'id': 'zing-chart',
228 },
229 'playlist_mincount': 100,
230 }, {
231 'url': 'https://zingmp3.vn/moi-phat-hanh',
232 'info_dict': {
233 'id': 'moi-phat-hanh',
234 },
235 'playlist_mincount': 100,
236 }]
237 IE_NAME = 'zingmp3:chart-home'
238
239 def _real_extract(self, url):
240 url_type = self._match_id(url)
241 data = self._call_api(url_type, {'id': url_type})
242 items = traverse_obj(data, ('RTChart', 'items') if url_type == 'zing-chart' else 'items')
243 return self.playlist_result(self._parse_items(items), url_type)
244
245
246 class ZingMp3WeekChartIE(ZingMp3BaseIE):
247 _VALID_URL = ZingMp3BaseIE._VALID_URL_TMPL % 'zing-chart-tuan'
248 IE_NAME = 'zingmp3:week-chart'
249 _TESTS = [{
250 'url': 'https://zingmp3.vn/zing-chart-tuan/Bai-hat-Viet-Nam/IWZ9Z08I.html',
251 'info_dict': {
252 'id': 'IWZ9Z08I',
253 'title': 'zing-chart-vn',
254 },
255 'playlist_mincount': 10,
256 }, {
257 'url': 'https://zingmp3.vn/zing-chart-tuan/Bai-hat-US-UK/IWZ9Z0BW.html',
258 'info_dict': {
259 'id': 'IWZ9Z0BW',
260 'title': 'zing-chart-us',
261 },
262 'playlist_mincount': 10,
263 }, {
264 'url': 'https://zingmp3.vn/zing-chart-tuan/Bai-hat-KPop/IWZ9Z0BO.html',
265 'info_dict': {
266 'id': 'IWZ9Z0BO',
267 'title': 'zing-chart-korea',
268 },
269 'playlist_mincount': 10,
270 }]
271
272 def _real_extract(self, url):
273 song_id, url_type = self._match_valid_url(url).group('id', 'type')
274 data = self._call_api(url_type, {'id': song_id})
275 return self.playlist_result(
276 self._parse_items(data['items']), song_id, f'zing-chart-{data.get("country", "")}')
277
278
279 class ZingMp3ChartMusicVideoIE(ZingMp3BaseIE):
280 _VALID_URL = r'https?://(?:mp3\.zing|zingmp3)\.vn/(?P<type>the-loai-video)/(?P<regions>[^/]+)/(?P<id>[^\.]+)'
281 IE_NAME = 'zingmp3:chart-music-video'
282 _TESTS = [{
283 'url': 'https://zingmp3.vn/the-loai-video/Viet-Nam/IWZ9Z08I.html',
284 'info_dict': {
285 'id': 'IWZ9Z08I',
286 'title': 'the-loai-video_Viet-Nam',
287 },
288 'playlist_mincount': 400,
289 }, {
290 'url': 'https://zingmp3.vn/the-loai-video/Au-My/IWZ9Z08O.html',
291 'info_dict': {
292 'id': 'IWZ9Z08O',
293 'title': 'the-loai-video_Au-My',
294 },
295 'playlist_mincount': 40,
296 }, {
297 'url': 'https://zingmp3.vn/the-loai-video/Han-Quoc/IWZ9Z08W.html',
298 'info_dict': {
299 'id': 'IWZ9Z08W',
300 'title': 'the-loai-video_Han-Quoc',
301 },
302 'playlist_mincount': 30,
303 }, {
304 'url': 'https://zingmp3.vn/the-loai-video/Khong-Loi/IWZ9Z086.html',
305 'info_dict': {
306 'id': 'IWZ9Z086',
307 'title': 'the-loai-video_Khong-Loi',
308 },
309 'playlist_mincount': 10,
310 }]
311
312 def _fetch_page(self, song_id, url_type, page):
313 return self._parse_items(self._call_api(url_type, {
314 'id': song_id,
315 'type': 'genre',
316 'page': page + 1,
317 'count': self._PER_PAGE
318 }).get('items'))
319
320 def _real_extract(self, url):
321 song_id, regions, url_type = self._match_valid_url(url).group('id', 'regions', 'type')
322 return self.playlist_result(
323 OnDemandPagedList(functools.partial(self._fetch_page, song_id, url_type), self._PER_PAGE),
324 song_id, f'{url_type}_{regions}')
325
326
327 class ZingMp3UserIE(ZingMp3BaseIE):
328 _VALID_URL = r'https?://(?:mp3\.zing|zingmp3)\.vn/(?P<user>[^/]+)/(?P<type>bai-hat|single|album|video)/?(?:[?#]|$)'
329 IE_NAME = 'zingmp3:user'
330 _TESTS = [{
331 'url': 'https://zingmp3.vn/Mr-Siro/bai-hat',
332 'info_dict': {
333 'id': 'IWZ98609',
334 'title': 'Mr. Siro - bai-hat',
335 'description': 'md5:85ab29bd7b21725c12bf76fd1d6922e5',
336 },
337 'playlist_mincount': 91,
338 }, {
339 'url': 'https://zingmp3.vn/Mr-Siro/album',
340 'info_dict': {
341 'id': 'IWZ98609',
342 'title': 'Mr. Siro - album',
343 'description': 'md5:85ab29bd7b21725c12bf76fd1d6922e5',
344 },
345 'playlist_mincount': 3,
346 }, {
347 'url': 'https://zingmp3.vn/Mr-Siro/single',
348 'info_dict': {
349 'id': 'IWZ98609',
350 'title': 'Mr. Siro - single',
351 'description': 'md5:85ab29bd7b21725c12bf76fd1d6922e5',
352 },
353 'playlist_mincount': 20,
354 }, {
355 'url': 'https://zingmp3.vn/Mr-Siro/video',
356 'info_dict': {
357 'id': 'IWZ98609',
358 'title': 'Mr. Siro - video',
359 'description': 'md5:85ab29bd7b21725c12bf76fd1d6922e5',
360 },
361 'playlist_mincount': 15,
362 }]
363
364 def _fetch_page(self, user_id, url_type, page):
365 url_type = 'user-list-song' if url_type == 'bai-hat' else 'user-list-video'
366 return self._parse_items(self._call_api(url_type, {
367 'id': user_id,
368 'type': 'artist',
369 'page': page + 1,
370 'count': self._PER_PAGE
371 }, query={'sort': 'new', 'sectionId': 'aSong'}).get('items'))
372
373 def _real_extract(self, url):
374 user_alias, url_type = self._match_valid_url(url).group('user', 'type')
375 if not url_type:
376 url_type = 'bai-hat'
377
378 user_info = self._call_api('info-artist', {}, user_alias, query={'alias': user_alias})
379 if url_type in ('bai-hat', 'video'):
380 entries = OnDemandPagedList(
381 functools.partial(self._fetch_page, user_info['id'], url_type), self._PER_PAGE)
382 else:
383 entries = self._parse_items(traverse_obj(user_info, (
384 'sections', lambda _, v: v['link'] == f'/{user_alias}/{url_type}', 'items', ...)))
385 return self.playlist_result(
386 entries, user_info['id'], f'{user_info.get("name")} - {url_type}', user_info.get('biography'))