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