7 from .common
import InfoExtractor
8 from ..utils
import int_or_none
, traverse_obj
, try_call
, urljoin
11 class ZingMp3BaseIE(InfoExtractor
):
12 _VALID_URL_TMPL
= r
'https?://(?:mp3\.zing|zingmp3)\.vn/(?P<type>(?:%s))/[^/?#]+/(?P<id>\w+)(?:\.html|\?)'
13 _GEO_COUNTRIES
= ['VN']
14 _DOMAIN
= 'https://zingmp3.vn'
18 'bai-hat': '/api/v2/page/get/song',
19 'embed': '/api/v2/page/get/song',
20 'video-clip': '/api/v2/page/get/video',
21 'lyric': '/api/v2/lyric/get/lyric',
22 'song-streaming': '/api/v2/song/get/streaming',
24 'playlist': '/api/v2/page/get/playlist',
25 'album': '/api/v2/page/get/playlist',
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',
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',
35 'hub': '/api/v2/page/get/hub-detail',
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()
45 'apiKey': 'X5BM3w8N7MKozC0B85o4KMlzLZKhV00y',
46 'sig': hmac
.new(b
'acOrvUS15XRW2o9JksiK1KgQ6Vbds8ZW',
47 f
'{api_slug}{sha256}'.encode(), hashlib
.sha512
).hexdigest(),
49 return f
'{self._DOMAIN}{api_slug}?{urllib.parse.urlencode(data)}'
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 {}
57 def _real_initialize(self
):
58 if not self
._cookies
_passed
:
59 self
._request
_webpage
(
60 self
._api
_url
('bai-hat', {'id': ''}
), None, note
='Updating cookies')
62 def _parse_items(self
, items
):
63 for url
in traverse_obj(items
, (..., 'link')) or []:
64 yield self
.url_result(urljoin(self
._DOMAIN
, url
))
66 def _fetch_page(self
, id_
, url_type
, page
):
67 raise NotImplementedError('This method must be implemented by subclasses')
69 def _paged_list(self
, _id
, url_type
):
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')))
76 if not data
.get('hasMore') or try_call(lambda: count
> data
['total']):
80 class ZingMp3IE(ZingMp3BaseIE
):
81 _VALID_URL
= ZingMp3BaseIE
._VALID
_URL
_TMPL
% 'bai-hat|video-clip|embed'
83 IE_DESC
= 'zingmp3.vn'
85 'url': 'https://mp3.zing.vn/bai-hat/Xa-Mai-Xa-Bao-Thy/ZWZB9WAB.html',
86 'md5': 'ead7ae13693b3205cbc89536a077daed',
91 'thumbnail': r
're:^https?://.+\.jpg',
100 'album': 'Special Album',
101 'album_artist': 'Bảo Thy',
104 'url': 'https://zingmp3.vn/video-clip/Suong-Hoa-Dua-Loi-K-ICM-RYO/ZO8ZF7C7.html',
105 'md5': '3c2081e79471a2f4a3edd90b70b185ea',
108 'title': 'Sương Hoa Đưa Lối',
110 'thumbnail': r
're:^https?://.+\.jpg',
112 'track': 'Sương Hoa Đưa Lối',
113 'artist': 'K-ICM, RYO',
114 'album': 'Sương Hoa Đưa Lối (Single)',
115 'album_artist': 'K-ICM, RYO',
118 'url': 'https://zingmp3.vn/bai-hat/Nguoi-Yeu-Toi-Lanh-Lung-Sat-Da-Mr-Siro/ZZ6IW7OU.html',
119 'md5': '3e9f7a9bd0d965573dbff8d7c68b629d',
122 'title': 'Người Yêu Tôi Lạnh Lùng Sắt Đá',
124 'thumbnail': r
're:^https?://.+\.jpg',
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',
132 'url': 'https://zingmp3.vn/embed/song/ZWZEI76B?start=false',
133 'only_matching': True,
135 'url': 'https://zingmp3.vn/bai-hat/Xa-Mai-Xa-Bao-Thy/ZWZB9WAB.html',
136 'only_matching': True,
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}
)
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')
151 source
= self
._call
_api
('song-streaming', {'id': item_id}
)
154 for k
, v
in (source
or {}).items():
155 if not v
or v
== 'VIP':
157 if k
not in ('mp4', 'hls'):
161 'tbr': int_or_none(k
),
162 'url': self
._proto
_relative
_url
(v
),
166 for res
, video_url
in v
.items():
170 formats
.extend(self
._extract
_m
3u8_formats
(video_url
, item_id
, 'mp4', m3u8_id
=k
, fatal
=False))
173 'format_id': f
'mp4-{res}',
175 'height': int_or_none(res
),
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)
182 self
.raise_no_formats('The song is only for VIP accounts.')
184 lyric
= item
.get('lyric') or self
._call
_api
('lyric', {'id': item_id}
, fatal
=False).get('file')
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),
196 'subtitles': {'origin': [{'url': lyric}
]} if lyric
else None,
200 class ZingMp3AlbumIE(ZingMp3BaseIE
):
201 _VALID_URL
= ZingMp3BaseIE
._VALID
_URL
_TMPL
% 'album|playlist'
203 'url': 'http://mp3.zing.vn/album/Lau-Dai-Tinh-Ai-Bang-Kieu-Minh-Tuyet/ZWZBWDAF.html',
206 'title': 'Lâu Đài Tình Ái',
208 'playlist_mincount': 9,
210 'url': 'https://zingmp3.vn/album/Nhung-Bai-Hat-Hay-Nhat-Cua-Mr-Siro-Mr-Siro/ZWZAEZZD.html',
213 'title': 'Những Bài Hát Hay Nhất Của Mr. Siro',
215 'playlist_mincount': 20,
217 'url': 'http://mp3.zing.vn/playlist/Duong-Hong-Loan-apollobee/IWCAACCB.html',
218 'only_matching': True,
220 'url': 'https://zingmp3.vn/album/Lau-Dai-Tinh-Ai-Bang-Kieu-Minh-Tuyet/ZWZBWDAF.html',
221 'only_matching': True,
223 IE_NAME
= 'zingmp3:album'
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'))
233 class ZingMp3ChartHomeIE(ZingMp3BaseIE
):
234 _VALID_URL
= r
'https?://(?:mp3\.zing|zingmp3)\.vn/(?P<id>(?:zing-chart|moi-phat-hanh))/?(?:[#?]|$)'
236 'url': 'https://zingmp3.vn/zing-chart',
240 'playlist_mincount': 100,
242 'url': 'https://zingmp3.vn/moi-phat-hanh',
244 'id': 'moi-phat-hanh',
246 'playlist_mincount': 100,
248 IE_NAME
= 'zingmp3:chart-home'
250 def _real_extract(self
, url
):
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
)
257 class ZingMp3WeekChartIE(ZingMp3BaseIE
):
258 _VALID_URL
= ZingMp3BaseIE
._VALID
_URL
_TMPL
% 'zing-chart-tuan'
259 IE_NAME
= 'zingmp3:week-chart'
261 'url': 'https://zingmp3.vn/zing-chart-tuan/Bai-hat-Viet-Nam/IWZ9Z08I.html',
264 'title': 'zing-chart-vn',
266 'playlist_mincount': 10,
268 'url': 'https://zingmp3.vn/zing-chart-tuan/Bai-hat-US-UK/IWZ9Z0BW.html',
271 'title': 'zing-chart-us',
273 'playlist_mincount': 10,
275 'url': 'https://zingmp3.vn/zing-chart-tuan/Bai-hat-KPop/IWZ9Z0BO.html',
278 'title': 'zing-chart-korea',
280 'playlist_mincount': 10,
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", "")}')
290 class 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'
294 'url': 'https://zingmp3.vn/the-loai-video/Viet-Nam/IWZ9Z08I.html',
297 'title': 'the-loai-video_Viet-Nam',
299 'playlist_mincount': 400,
301 'url': 'https://zingmp3.vn/the-loai-video/Au-My/IWZ9Z08O.html',
304 'title': 'the-loai-video_Au-My',
306 'playlist_mincount': 40,
308 'url': 'https://zingmp3.vn/the-loai-video/Han-Quoc/IWZ9Z08W.html',
311 'title': 'the-loai-video_Han-Quoc',
313 'playlist_mincount': 30,
315 'url': 'https://zingmp3.vn/the-loai-video/Khong-Loi/IWZ9Z086.html',
318 'title': 'the-loai-video_Khong-Loi',
320 'playlist_mincount': 1,
323 def _fetch_page(self
, song_id
, url_type
, page
):
324 return self
._call
_api
(url_type
, {
328 'count': self
._PER
_PAGE
331 def _real_extract(self
, url
):
332 song_id
, regions
, url_type
= self
._match
_valid
_url
(url
).group('id', 'regions', 'type')
333 return self
.playlist_result(self
._paged
_list
(song_id
, url_type
), song_id
, f
'{url_type}_{regions}')
336 class ZingMp3UserIE(ZingMp3BaseIE
):
337 _VALID_URL
= r
'https?://(?:mp3\.zing|zingmp3)\.vn/(?P<user>[^/]+)/(?P<type>bai-hat|single|album|video)/?(?:[?#]|$)'
338 IE_NAME
= 'zingmp3:user'
340 'url': 'https://zingmp3.vn/Mr-Siro/bai-hat',
343 'title': 'Mr. Siro - bai-hat',
344 'description': 'md5:5bdcf45e955dc1b8d7f518f322ffef36',
346 'playlist_mincount': 91,
348 'url': 'https://zingmp3.vn/Mr-Siro/album',
351 'title': 'Mr. Siro - album',
352 'description': 'md5:5bdcf45e955dc1b8d7f518f322ffef36',
354 'playlist_mincount': 3,
356 'url': 'https://zingmp3.vn/Mr-Siro/single',
359 'title': 'Mr. Siro - single',
360 'description': 'md5:5bdcf45e955dc1b8d7f518f322ffef36',
362 'playlist_mincount': 20,
364 'url': 'https://zingmp3.vn/Mr-Siro/video',
367 'title': 'Mr. Siro - video',
368 'description': 'md5:5bdcf45e955dc1b8d7f518f322ffef36',
370 'playlist_mincount': 15,
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'
375 return self
._call
_api
(url_type
, {
379 'count': self
._PER
_PAGE
382 def _real_extract(self
, url
):
383 user_alias
, url_type
= self
._match
_valid
_url
(url
).group('user', 'type')
387 user_info
= self
._call
_api
('info-artist', {}, user_alias, query={'alias': user_alias}
)
388 if url_type
in ('bai-hat', 'video'):
389 entries
= self
._paged
_list
(user_info
['id'], url_type
)
391 entries
= self
._parse
_items
(traverse_obj(user_info
, (
393 lambda _
, v
: v
['sectionId'] == 'aAlbum' if url_type
== 'album' else v
['sectionId'] == 'aSingle',
395 return self
.playlist_result(
396 entries
, user_info
['id'], f
'{user_info.get("name")} - {url_type}', user_info
.get('biography'))
399 class ZingMp3HubIE(ZingMp3BaseIE
):
400 IE_NAME
= 'zingmp3:hub'
401 _VALID_URL
= r
'https?://(?:mp3\.zing|zingmp3)\.vn/(?P<type>hub)/(?P<regions>[^/]+)/(?P<id>[^\.]+)'
403 'url': 'https://zingmp3.vn/hub/Nhac-Moi/IWZ9Z0CA.html',
407 'description': 'md5:1cc31b68a6f746427b07b2756c22a558',
409 'playlist_mincount': 20,
411 'url': 'https://zingmp3.vn/hub/Nhac-Viet/IWZ9Z087.html',
414 'title': 'Nhạc Việt',
415 'description': 'md5:acc976c8bdde64d5c6ee4a92c39f7a77',
417 'playlist_mincount': 30,
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'))