7 from .common
import InfoExtractor
16 from ..utils
.traversal
import traverse_obj
19 class ZingMp3BaseIE(InfoExtractor
):
20 _VALID_URL_TMPL
= r
'https?://(?:mp3\.zing|zingmp3)\.vn/(?P<type>(?:%s))/[^/?#]+/(?P<id>\w+)(?:\.html|\?)'
21 _GEO_COUNTRIES
= ['VN']
22 _DOMAIN
= 'https://zingmp3.vn'
26 'bai-hat': '/api/v2/page/get/song',
27 'embed': '/api/v2/page/get/song',
28 'video-clip': '/api/v2/page/get/video',
29 'lyric': '/api/v2/lyric/get/lyric',
30 'song-streaming': '/api/v2/song/get/streaming',
31 'liveradio': '/api/v2/livestream/get/info',
32 'eps': '/api/v2/page/get/podcast-episode',
33 'episode-streaming': '/api/v2/podcast/episode/get/streaming',
35 'playlist': '/api/v2/page/get/playlist',
36 'album': '/api/v2/page/get/playlist',
37 'pgr': '/api/v2/page/get/podcast-program',
38 'pgr-list': '/api/v2/podcast/episode/get/list',
39 'cgr': '/api/v2/page/get/podcast-category',
40 'cgr-list': '/api/v2/podcast/program/get/list-by-cate',
41 'cgrs': '/api/v2/page/get/podcast-categories',
43 'zing-chart': '/api/v2/page/get/chart-home',
44 'zing-chart-tuan': '/api/v2/page/get/week-chart',
45 'moi-phat-hanh': '/api/v2/page/get/newrelease-chart',
46 'the-loai-video': '/api/v2/video/get/list',
48 'info-artist': '/api/v2/page/get/artist',
49 'user-list-song': '/api/v2/song/get/list',
50 'user-list-video': '/api/v2/video/get/list',
51 'hub': '/api/v2/page/get/hub-detail',
52 'new-release': '/api/v2/chart/get/new-release',
53 'top100': '/api/v2/page/get/top-100',
54 'podcast-new': '/api/v2/podcast/program/get/list-by-type',
55 'top-podcast': '/api/v2/podcast/program/get/top-episode',
58 def _api_url(self
, url_type
, params
):
59 api_slug
= self
._API
_SLUGS
[url_type
]
60 params
.update({'ctime': '1'})
61 sha256
= hashlib
.sha256(
62 ''.join(f
'{k}={v}' for k
, v
in sorted(params
.items())).encode()).hexdigest()
65 'apiKey': 'X5BM3w8N7MKozC0B85o4KMlzLZKhV00y',
66 'sig': hmac
.new(b
'acOrvUS15XRW2o9JksiK1KgQ6Vbds8ZW',
67 f
'{api_slug}{sha256}'.encode(), hashlib
.sha512
).hexdigest(),
69 return f
'{self._DOMAIN}{api_slug}?{urllib.parse.urlencode(data)}'
71 def _call_api(self
, url_type
, params
, display_id
=None, **kwargs
):
72 resp
= self
._download
_json
(
73 self
._api
_url
(url_type
, params
), display_id
or params
.get('id'),
74 note
=f
'Downloading {url_type} JSON metadata', **kwargs
)
75 return (resp
or {}).get('data') or {}
77 def _real_initialize(self
):
78 if not self
._cookies
_passed
:
79 self
._request
_webpage
(
80 self
._api
_url
('bai-hat', {'id': ''}), None, note
='Updating cookies')
82 def _parse_items(self
, items
):
83 for url
in traverse_obj(items
, (..., 'link')) or []:
84 yield self
.url_result(urljoin(self
._DOMAIN
, url
))
86 def _fetch_page(self
, id_
, url_type
, page
):
87 raise NotImplementedError('This method must be implemented by subclasses')
89 def _paged_list(self
, _id
, url_type
):
91 for page
in itertools
.count(1):
92 data
= self
._fetch
_page
(_id
, url_type
, page
)
93 entries
= list(self
._parse
_items
(data
.get('items')))
96 if not data
.get('hasMore') or try_call(lambda: count
> data
['total']):
100 class ZingMp3IE(ZingMp3BaseIE
):
101 _VALID_URL
= ZingMp3BaseIE
._VALID
_URL
_TMPL
% 'bai-hat|video-clip|embed|eps'
103 IE_DESC
= 'zingmp3.vn'
105 'url': 'https://mp3.zing.vn/bai-hat/Xa-Mai-Xa-Bao-Thy/ZWZB9WAB.html',
106 'md5': 'ead7ae13693b3205cbc89536a077daed',
109 'title': 'Xa Mãi Xa',
111 'thumbnail': r
're:^https?://.+\.jpg',
118 'track': 'Xa Mãi Xa',
120 'album': 'Special Album',
121 'album_artist': 'Bảo Thy',
124 'url': 'https://zingmp3.vn/video-clip/Suong-Hoa-Dua-Loi-K-ICM-RYO/ZO8ZF7C7.html',
125 'md5': '92c6e7a019f06b4682a6c35ae5785fab',
128 'title': 'Sương Hoa Đưa Lối',
130 'thumbnail': r
're:^https?://.+\.jpg',
132 'track': 'Sương Hoa Đưa Lối',
133 'artist': 'K-ICM, RYO',
134 'album': 'Sương Hoa Đưa Lối (Single)',
135 'album_artist': 'K-ICM, RYO',
138 'url': 'https://zingmp3.vn/bai-hat/Nguoi-Yeu-Toi-Lanh-Lung-Sat-Da-Mr-Siro/ZZ6IW7OU.html',
139 'md5': '3e9f7a9bd0d965573dbff8d7c68b629d',
142 'title': 'Người Yêu Tôi Lạnh Lùng Sắt Đá',
144 'thumbnail': r
're:^https?://.+\.jpg',
146 'track': 'Người Yêu Tôi Lạnh Lùng Sắt Đá',
147 'artist': 'Mr. Siro',
148 'album': 'Người Yêu Tôi Lạnh Lùng Sắt Đá (Single)',
149 'album_artist': 'Mr. Siro',
152 'url': 'https://zingmp3.vn/eps/Cham-x-Ban-Noi-Goi-La-Nha/ZZD9ACWI.html',
153 'md5': 'd52f9f63e2631e004e4f15188eedcf80',
156 'title': 'Chạm x Bạn - Nơi Gọi Là Nhà',
159 'thumbnail': r
're:^https?://.+\.jpg',
160 'track': 'Chạm x Bạn - Nơi Gọi Là Nhà',
162 'album': 'Top Podcast',
163 'album_artist': 'On Air',
166 'url': 'https://zingmp3.vn/embed/song/ZWZEI76B?start=false',
167 'only_matching': True,
169 'url': 'https://zingmp3.vn/bai-hat/Xa-Mai-Xa-Bao-Thy/ZWZB9WAB.html',
170 'only_matching': True,
173 def _real_extract(self
, url
):
174 song_id
, url_type
= self
._match
_valid
_url
(url
).group('id', 'type')
175 item
= self
._call
_api
(url_type
, {'id': song_id
})
177 item_id
= item
.get('encodeId') or song_id
178 if url_type
== 'video-clip':
179 source
= item
.get('streaming')
180 source
['mp4'] = self
._download
_json
(
181 'http://api.mp3.zing.vn/api/mobile/video/getvideoinfo', item_id
,
182 query
={'requestdata': json
.dumps({'id': item_id
})},
183 note
='Downloading mp4 JSON metadata').get('source')
184 elif url_type
== 'eps':
185 source
= self
._call
_api
('episode-streaming', {'id': item_id
})
187 source
= self
._call
_api
('song-streaming', {'id': item_id
})
190 for k
, v
in (source
or {}).items():
191 if not v
or v
== 'VIP':
193 if k
not in ('mp4', 'hls'):
197 'tbr': int_or_none(k
),
198 'url': self
._proto
_relative
_url
(v
),
202 for res
, video_url
in v
.items():
206 formats
.extend(self
._extract
_m
3u8_formats
(video_url
, item_id
, 'mp4', m3u8_id
=k
, fatal
=False))
209 'format_id': f
'mp4-{res}',
211 'height': int_or_none(res
),
215 if item
.get('msg') == 'Sorry, this content is not available in your country.':
216 self
.raise_geo_restricted(countries
=self
._GEO
_COUNTRIES
, metadata_available
=True)
218 self
.raise_no_formats('The song is only for VIP accounts.')
220 lyric
= item
.get('lyric') or self
._call
_api
('lyric', {'id': item_id
}, fatal
=False).get('file')
224 'title': traverse_obj(item
, 'title', 'alias'),
225 'thumbnail': traverse_obj(item
, 'thumbnail', 'thumbnailM'),
226 'duration': int_or_none(item
.get('duration')),
227 'track': traverse_obj(item
, 'title', 'alias'),
228 'artist': traverse_obj(item
, 'artistsNames', 'artists_names', ('artists', 0, 'name')),
229 'album': traverse_obj(item
, ('album', ('name', 'title')), ('genres', 0, 'name'), get_all
=False),
230 'album_artist': traverse_obj(item
, ('album', ('artistsNames', 'artists_names')),
231 ('artists', 0, 'name'), get_all
=False),
233 'subtitles': {'origin': [{'url': lyric
}]} if lyric
else None,
237 class ZingMp3AlbumIE(ZingMp3BaseIE
):
238 _VALID_URL
= ZingMp3BaseIE
._VALID
_URL
_TMPL
% 'album|playlist'
240 'url': 'https://zingmp3.vn/album/Ca-Phe-Quan-Quen-Hoang-Dung-My-Anh-Da-LAB-Thinh-Suy/ZOC7WUZC.html',
243 'title': 'Cà Phê Quán Quen',
245 'playlist_mincount': 10,
247 'url': 'https://zingmp3.vn/album/Nhung-Bai-Hat-Hay-Nhat-Cua-Mr-Siro-Mr-Siro/ZWZAEZZD.html',
250 'title': 'Những Bài Hát Hay Nhất Của Mr. Siro',
252 'playlist_mincount': 20,
254 'url': 'http://mp3.zing.vn/playlist/Duong-Hong-Loan-apollobee/IWCAACCB.html',
255 'only_matching': True,
257 'url': 'https://zingmp3.vn/album/Lau-Dai-Tinh-Ai-Bang-Kieu-Minh-Tuyet/ZWZBWDAF.html',
258 'only_matching': True,
260 IE_NAME
= 'zingmp3:album'
262 def _real_extract(self
, url
):
263 song_id
, url_type
= self
._match
_valid
_url
(url
).group('id', 'type')
264 data
= self
._call
_api
(url_type
, {'id': song_id
})
265 return self
.playlist_result(
266 self
._parse
_items
(traverse_obj(data
, ('song', 'items'))),
267 traverse_obj(data
, 'id', 'encodeId'), traverse_obj(data
, 'name', 'title'))
270 class ZingMp3ChartHomeIE(ZingMp3BaseIE
):
271 _VALID_URL
= r
'https?://(?:mp3\.zing|zingmp3)\.vn/(?P<id>(?:zing-chart|moi-phat-hanh|top100|podcast-discover))/?(?:[#?]|$)'
273 'url': 'https://zingmp3.vn/zing-chart',
277 'playlist_mincount': 100,
279 'url': 'https://zingmp3.vn/moi-phat-hanh',
281 'id': 'moi-phat-hanh',
283 'playlist_mincount': 100,
285 'url': 'https://zingmp3.vn/top100',
289 'playlist_mincount': 50,
291 'url': 'https://zingmp3.vn/podcast-discover',
293 'id': 'podcast-discover',
295 'playlist_mincount': 4,
297 IE_NAME
= 'zingmp3:chart-home'
299 def _real_extract(self
, url
):
300 url_type
= self
._match
_id
(url
)
301 params
= {'id': url_type
}
302 if url_type
== 'podcast-discover':
303 params
['type'] = 'discover'
304 data
= self
._call
_api
(url_type
, params
)
306 if url_type
== 'top100':
307 items
.extend(traverse_obj(data
, (..., 'items', ..., {dict}
)))
308 elif url_type
== 'zing-chart':
309 items
.extend(traverse_obj(data
, ('RTChart', 'items', ..., {dict}
)))
311 items
.extend(traverse_obj(data
, ('items', ..., {dict}
)))
312 return self
.playlist_result(self
._parse
_items
(items
), url_type
)
315 class ZingMp3WeekChartIE(ZingMp3BaseIE
):
316 _VALID_URL
= ZingMp3BaseIE
._VALID
_URL
_TMPL
% 'zing-chart-tuan'
317 IE_NAME
= 'zingmp3:week-chart'
319 'url': 'https://zingmp3.vn/zing-chart-tuan/Bai-hat-Viet-Nam/IWZ9Z08I.html',
322 'title': 'zing-chart-vn',
324 'playlist_mincount': 10,
326 'url': 'https://zingmp3.vn/zing-chart-tuan/Bai-hat-US-UK/IWZ9Z0BW.html',
329 'title': 'zing-chart-us',
331 'playlist_mincount': 10,
333 'url': 'https://zingmp3.vn/zing-chart-tuan/Bai-hat-KPop/IWZ9Z0BO.html',
336 'title': 'zing-chart-korea',
338 'playlist_mincount': 10,
341 def _real_extract(self
, url
):
342 song_id
, url_type
= self
._match
_valid
_url
(url
).group('id', 'type')
343 data
= self
._call
_api
(url_type
, {'id': song_id
})
344 return self
.playlist_result(
345 self
._parse
_items
(data
['items']), song_id
, f
'zing-chart-{data.get("country", "")}')
348 class ZingMp3ChartMusicVideoIE(ZingMp3BaseIE
):
349 _VALID_URL
= r
'https?://(?:mp3\.zing|zingmp3)\.vn/(?P<type>the-loai-video)/(?P<regions>[^/]+)/(?P<id>[^\.]+)'
350 IE_NAME
= 'zingmp3:chart-music-video'
352 'url': 'https://zingmp3.vn/the-loai-video/Viet-Nam/IWZ9Z08I.html',
355 'title': 'the-loai-video_Viet-Nam',
357 'playlist_mincount': 400,
359 'url': 'https://zingmp3.vn/the-loai-video/Au-My/IWZ9Z08O.html',
362 'title': 'the-loai-video_Au-My',
364 'playlist_mincount': 40,
366 'url': 'https://zingmp3.vn/the-loai-video/Han-Quoc/IWZ9Z08W.html',
369 'title': 'the-loai-video_Han-Quoc',
371 'playlist_mincount': 30,
373 'url': 'https://zingmp3.vn/the-loai-video/Khong-Loi/IWZ9Z086.html',
376 'title': 'the-loai-video_Khong-Loi',
378 'playlist_mincount': 1,
381 def _fetch_page(self
, song_id
, url_type
, page
):
382 return self
._call
_api
(url_type
, {
386 'count': self
._PER
_PAGE
,
389 def _real_extract(self
, url
):
390 song_id
, regions
, url_type
= self
._match
_valid
_url
(url
).group('id', 'regions', 'type')
391 return self
.playlist_result(self
._paged
_list
(song_id
, url_type
), song_id
, f
'{url_type}_{regions}')
394 class ZingMp3UserIE(ZingMp3BaseIE
):
395 _VALID_URL
= r
'https?://(?:mp3\.zing|zingmp3)\.vn/(?P<user>[^/]+)/(?P<type>bai-hat|single|album|video|song)/?(?:[?#]|$)'
396 IE_NAME
= 'zingmp3:user'
398 'url': 'https://zingmp3.vn/Mr-Siro/bai-hat',
401 'title': 'Mr. Siro - bai-hat',
402 'description': 'md5:5bdcf45e955dc1b8d7f518f322ffef36',
404 'playlist_mincount': 91,
406 'url': 'https://zingmp3.vn/Mr-Siro/album',
409 'title': 'Mr. Siro - album',
410 'description': 'md5:5bdcf45e955dc1b8d7f518f322ffef36',
412 'playlist_mincount': 3,
414 'url': 'https://zingmp3.vn/Mr-Siro/single',
417 'title': 'Mr. Siro - single',
418 'description': 'md5:5bdcf45e955dc1b8d7f518f322ffef36',
420 'playlist_mincount': 20,
422 'url': 'https://zingmp3.vn/Mr-Siro/video',
425 'title': 'Mr. Siro - video',
426 'description': 'md5:5bdcf45e955dc1b8d7f518f322ffef36',
428 'playlist_mincount': 15,
430 'url': 'https://zingmp3.vn/new-release/song',
432 'id': 'new-release-song',
434 'playlist_mincount': 50,
436 'url': 'https://zingmp3.vn/new-release/album',
438 'id': 'new-release-album',
440 'playlist_mincount': 20,
443 def _fetch_page(self
, user_id
, url_type
, page
):
444 url_type
= 'user-list-song' if url_type
== 'bai-hat' else 'user-list-video'
445 return self
._call
_api
(url_type
, {
449 'count': self
._PER
_PAGE
,
452 def _real_extract(self
, url
):
453 alias
, url_type
= self
._match
_valid
_url
(url
).group('user', 'type')
457 user_info
= self
._call
_api
('info-artist', {}, alias
, query
={'alias': alias
})
459 # Handle for new-release
460 if alias
== 'new-release' and url_type
in ('song', 'album'):
461 _id
= f
'{alias}-{url_type}'
462 return self
.playlist_result(self
._parse
_items
(
463 self
._call
_api
('new-release', params
={'type': url_type
}, display_id
=_id
)), _id
)
465 # Handle for user/artist
466 if url_type
in ('bai-hat', 'video'):
467 entries
= self
._paged
_list
(user_info
['id'], url_type
)
469 section_id
= 'aAlbum' if url_type
== 'album' else 'aSingle'
470 entries
= self
._parse
_items
(traverse_obj(user_info
, (
471 'sections', lambda _
, v
: v
['sectionId'] == section_id
, 'items', ...)))
472 return self
.playlist_result(
473 entries
, user_info
['id'], join_nonempty(user_info
.get('name'), url_type
, delim
=' - '),
474 user_info
.get('biography'))
477 class ZingMp3HubIE(ZingMp3BaseIE
):
478 IE_NAME
= 'zingmp3:hub'
479 _VALID_URL
= r
'https?://(?:mp3\.zing|zingmp3)\.vn/(?P<type>hub)/(?P<regions>[^/]+)/(?P<id>[^\.]+)'
481 'url': 'https://zingmp3.vn/hub/Nhac-Moi/IWZ9Z0CA.html',
484 'title': 'BXH Nhạc Mới',
485 'description': 'md5:1cc31b68a6f746427b07b2756c22a558',
487 'playlist_mincount': 20,
489 'url': 'https://zingmp3.vn/hub/Nhac-Viet/IWZ9Z087.html',
492 'title': 'Nhạc Việt',
493 'description': 'md5:acc976c8bdde64d5c6ee4a92c39f7a77',
495 'playlist_mincount': 30,
498 def _real_extract(self
, url
):
499 song_id
, regions
, url_type
= self
._match
_valid
_url
(url
).group('id', 'regions', 'type')
500 hub_detail
= self
._call
_api
(url_type
, {'id': song_id
})
501 entries
= self
._parse
_items
(traverse_obj(hub_detail
, (
502 'sections', lambda _
, v
: v
['sectionId'] == 'hub', 'items', ...)))
503 return self
.playlist_result(
504 entries
, song_id
, hub_detail
.get('title'), hub_detail
.get('description'))
507 class ZingMp3LiveRadioIE(ZingMp3BaseIE
):
508 IE_NAME
= 'zingmp3:liveradio'
509 _VALID_URL
= r
'https?://(?:mp3\.zing|zingmp3)\.vn/(?P<type>(?:liveradio))/(?P<id>\w+)(?:\.html|\?)'
511 'url': 'https://zingmp3.vn/liveradio/IWZ979UB.html',
514 'title': r
're:^V\-POP',
515 'description': 'md5:aa857f8a91dc9ce69e862a809e4bdc10',
518 'thumbnail': r
're:^https?://.*\.jpg',
520 'live_status': 'is_live',
523 'skip_download': True,
526 'url': 'https://zingmp3.vn/liveradio/IWZ97CWB.html',
529 'title': r
're:^Live\s247',
530 'description': 'md5:d41d8cd98f00b204e9800998ecf8427e',
533 'thumbnail': r
're:^https?://.*\.jpg',
535 'live_status': 'is_live',
538 'skip_download': True,
542 def _real_extract(self
, url
):
543 url_type
, live_radio_id
= self
._match
_valid
_url
(url
).group('type', 'id')
544 info
= self
._call
_api
(url_type
, {'id': live_radio_id
})
545 manifest_url
= info
.get('streaming')
547 raise ExtractorError('This radio is offline.', expected
=True)
548 fmts
, subtitles
= self
._extract
_m
3u8_formats
_and
_subtitles
(manifest_url
, live_radio_id
, fatal
=False)
553 'subtitles': subtitles
,
554 **traverse_obj(info
, {
556 'thumbnail': (('thumbnail', 'thumbnailM', 'thumbnailV', 'thumbnailH'), {url_or_none}
),
557 'view_count': ('activeUsers', {int_or_none}
),
558 'like_count': ('totalReaction', {int_or_none}
),
559 'description': 'description',
564 class ZingMp3PodcastEpisodeIE(ZingMp3BaseIE
):
565 IE_NAME
= 'zingmp3:podcast-episode'
566 _VALID_URL
= ZingMp3BaseIE
._VALID
_URL
_TMPL
% 'pgr|cgr'
568 'url': 'https://zingmp3.vn/pgr/Nhac-Moi-Moi-Ngay/68Z9W66B.html',
571 'title': 'Nhạc Mới Mỗi Ngày',
572 'description': 'md5:2875dfa951f8e5356742f1610cf20691',
574 'playlist_mincount': 20,
576 'url': 'https://zingmp3.vn/cgr/Am-nhac/IWZ980AO.html',
581 'playlist_mincount': 2,
584 def _fetch_page(self
, eps_id
, url_type
, page
):
585 return self
._call
_api
(url_type
, {
588 'count': self
._PER
_PAGE
,
591 def _real_extract(self
, url
):
592 podcast_id
, url_type
= self
._match
_valid
_url
(url
).group('id', 'type')
593 podcast_info
= self
._call
_api
(url_type
, {'id': podcast_id
})
594 entries
= self
._paged
_list
(podcast_id
, 'pgr-list' if url_type
== 'pgr' else 'cgr-list')
595 return self
.playlist_result(
596 entries
, podcast_id
, podcast_info
.get('title'), podcast_info
.get('description'))
599 class ZingMp3PodcastIE(ZingMp3BaseIE
):
600 IE_NAME
= 'zingmp3:podcast'
601 _VALID_URL
= r
'https?://(?:mp3\.zing|zingmp3)\.vn/(?P<id>(?:cgr|top-podcast|podcast-new))/?(?:[#?]|$)'
603 'url': 'https://zingmp3.vn/cgr',
607 'playlist_mincount': 5,
609 'url': 'https://zingmp3.vn/top-podcast',
613 'playlist_mincount': 7,
615 'url': 'https://zingmp3.vn/podcast-new',
619 'playlist_mincount': 4,
622 def _real_extract(self
, url
):
623 url_type
= self
._match
_id
(url
)
624 params
= {'id': url_type
}
625 if url_type
== 'podcast-new':
626 params
['type'] = 'new'
627 items
= self
._call
_api
('cgrs' if url_type
== 'cgr' else url_type
, params
)['items']
628 return self
.playlist_result(self
._parse
_items
(items
), url_type
)