]>
Commit | Line | Data |
---|---|---|
54044dec | 1 | import functools |
ecca4519 HTL |
2 | import hashlib |
3 | import hmac | |
54044dec | 4 | import json |
ecca4519 HTL |
5 | import urllib.parse |
6 | ||
c66bdc48 | 7 | from .common import InfoExtractor |
3d47ee0a | 8 | from ..utils import ( |
54044dec | 9 | OnDemandPagedList, |
3d47ee0a | 10 | int_or_none, |
ecca4519 | 11 | traverse_obj, |
54044dec | 12 | urljoin, |
3d47ee0a | 13 | ) |
c66bdc48 DHS |
14 | |
15 | ||
1418a043 | 16 | class ZingMp3BaseIE(InfoExtractor): |
6b70527f | 17 | _VALID_URL_TMPL = r'https?://(?:mp3\.zing|zingmp3)\.vn/(?P<type>(?:%s))/[^/?#]+/(?P<id>\w+)(?:\.html|\?)' |
1418a043 | 18 | _GEO_COUNTRIES = ['VN'] |
ecca4519 | 19 | _DOMAIN = 'https://zingmp3.vn' |
6b70527f | 20 | _PER_PAGE = 50 |
21 | _API_SLUGS = { | |
22 | # Audio/video | |
ecca4519 HTL |
23 | 'bai-hat': '/api/v2/page/get/song', |
24 | 'embed': '/api/v2/page/get/song', | |
25 | 'video-clip': '/api/v2/page/get/video', | |
ecca4519 | 26 | 'lyric': '/api/v2/lyric/get/lyric', |
6b70527f | 27 | 'song-streaming': '/api/v2/song/get/streaming', |
28 | # Playlist | |
54044dec HTL |
29 | 'playlist': '/api/v2/page/get/playlist', |
30 | 'album': '/api/v2/page/get/playlist', | |
6b70527f | 31 | # Chart |
54044dec HTL |
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', | |
6b70527f | 36 | # User |
54044dec HTL |
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', | |
ecca4519 | 40 | } |
1418a043 | 41 | |
6b70527f | 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(), | |
c66bdc48 | 52 | } |
6b70527f | 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 {} | |
c66bdc48 | 60 | |
ecca4519 | 61 | def _real_initialize(self): |
24146491 | 62 | if not self._cookies_passed: |
6b70527f | 63 | self._request_webpage( |
64 | self._api_url('bai-hat', {'id': ''}), None, note='Updating cookies') | |
c66bdc48 | 65 | |
6b70527f | 66 | def _parse_items(self, items): |
67 | for url in traverse_obj(items, (..., 'link')) or []: | |
68 | yield self.url_result(urljoin(self._DOMAIN, url)) | |
54044dec | 69 | |
c66bdc48 | 70 | |
1418a043 | 71 | class ZingMp3IE(ZingMp3BaseIE): |
ecca4519 | 72 | _VALID_URL = ZingMp3BaseIE._VALID_URL_TMPL % 'bai-hat|video-clip|embed' |
6b70527f | 73 | IE_NAME = 'zingmp3' |
74 | IE_DESC = 'zingmp3.vn' | |
c66bdc48 | 75 | _TESTS = [{ |
ecca4519 | 76 | 'url': 'https://mp3.zing.vn/bai-hat/Xa-Mai-Xa-Bao-Thy/ZWZB9WAB.html', |
4ffc3103 | 77 | 'md5': 'ead7ae13693b3205cbc89536a077daed', |
c66bdc48 DHS |
78 | 'info_dict': { |
79 | 'id': 'ZWZB9WAB', | |
4ffc3103 | 80 | 'title': 'Xa Mãi Xa', |
c66bdc48 | 81 | 'ext': 'mp3', |
1418a043 | 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', | |
c66bdc48 | 93 | }, |
3d47ee0a | 94 | }, { |
ecca4519 | 95 | 'url': 'https://zingmp3.vn/video-clip/Suong-Hoa-Dua-Loi-K-ICM-RYO/ZO8ZF7C7.html', |
6b70527f | 96 | 'md5': '3c2081e79471a2f4a3edd90b70b185ea', |
3d47ee0a | 97 | 'info_dict': { |
1418a043 | 98 | 'id': 'ZO8ZF7C7', |
99 | 'title': 'Sương Hoa Đưa Lối', | |
3d47ee0a | 100 | 'ext': 'mp4', |
1418a043 | 101 | 'thumbnail': r're:^https?://.+\.jpg', |
102 | 'duration': 207, | |
103 | 'track': 'Sương Hoa Đưa Lối', | |
104 | 'artist': 'K-ICM, RYO', | |
08d30158 | 105 | 'album': 'Sương Hoa Đưa Lối (Single)', |
106 | 'album_artist': 'K-ICM, RYO', | |
3d47ee0a | 107 | }, |
63b2f88b HTL |
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 | }, | |
ecca4519 HTL |
122 | }, { |
123 | 'url': 'https://zingmp3.vn/embed/song/ZWZEI76B?start=false', | |
124 | 'only_matching': True, | |
3d47ee0a | 125 | }, { |
1418a043 | 126 | 'url': 'https://zingmp3.vn/bai-hat/Xa-Mai-Xa-Bao-Thy/ZWZB9WAB.html', |
127 | 'only_matching': True, | |
128 | }] | |
1418a043 | 129 | |
6b70527f | 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) | |
6b70527f | 171 | |
172 | lyric = item.get('lyric') or self._call_api('lyric', {'id': item_id}, fatal=False).get('file') | |
173 | ||
174 | return { | |
175 | 'id': item_id, | |
176 | 'title': traverse_obj(item, 'title', 'alias'), | |
177 | 'thumbnail': traverse_obj(item, 'thumbnail', 'thumbnailM'), | |
178 | 'duration': int_or_none(item.get('duration')), | |
179 | 'track': traverse_obj(item, 'title', 'alias'), | |
180 | 'artist': traverse_obj(item, 'artistsNames', 'artists_names'), | |
181 | 'album': traverse_obj(item, ('album', ('name', 'title')), get_all=False), | |
182 | 'album_artist': traverse_obj(item, ('album', ('artistsNames', 'artists_names')), get_all=False), | |
183 | 'formats': formats, | |
184 | 'subtitles': {'origin': [{'url': lyric}]} if lyric else None, | |
185 | } | |
1418a043 | 186 | |
187 | ||
188 | class ZingMp3AlbumIE(ZingMp3BaseIE): | |
189 | _VALID_URL = ZingMp3BaseIE._VALID_URL_TMPL % 'album|playlist' | |
190 | _TESTS = [{ | |
43abd799 S |
191 | 'url': 'http://mp3.zing.vn/album/Lau-Dai-Tinh-Ai-Bang-Kieu-Minh-Tuyet/ZWZBWDAF.html', |
192 | 'info_dict': { | |
43abd799 | 193 | 'id': 'ZWZBWDAF', |
1418a043 | 194 | 'title': 'Lâu Đài Tình Ái', |
c66bdc48 | 195 | }, |
54044dec | 196 | 'playlist_mincount': 9, |
63b2f88b HTL |
197 | }, { |
198 | 'url': 'https://zingmp3.vn/album/Nhung-Bai-Hat-Hay-Nhat-Cua-Mr-Siro-Mr-Siro/ZWZAEZZD.html', | |
199 | 'info_dict': { | |
63b2f88b HTL |
200 | 'id': 'ZWZAEZZD', |
201 | 'title': 'Những Bài Hát Hay Nhất Của Mr. Siro', | |
202 | }, | |
54044dec | 203 | 'playlist_mincount': 49, |
43abd799 S |
204 | }, { |
205 | 'url': 'http://mp3.zing.vn/playlist/Duong-Hong-Loan-apollobee/IWCAACCB.html', | |
206 | 'only_matching': True, | |
1418a043 | 207 | }, { |
208 | 'url': 'https://zingmp3.vn/album/Lau-Dai-Tinh-Ai-Bang-Kieu-Minh-Tuyet/ZWZBWDAF.html', | |
209 | 'only_matching': True, | |
43abd799 | 210 | }] |
1418a043 | 211 | IE_NAME = 'zingmp3:album' |
212 | ||
6b70527f | 213 | def _real_extract(self, url): |
214 | song_id, url_type = self._match_valid_url(url).group('id', 'type') | |
215 | data = self._call_api(url_type, {'id': song_id}) | |
216 | return self.playlist_result( | |
217 | self._parse_items(traverse_obj(data, ('song', 'items'))), | |
218 | traverse_obj(data, 'id', 'encodeId'), traverse_obj(data, 'name', 'title')) | |
54044dec HTL |
219 | |
220 | ||
221 | class ZingMp3ChartHomeIE(ZingMp3BaseIE): | |
222 | _VALID_URL = r'https?://(?:mp3\.zing|zingmp3)\.vn/(?P<id>(?:zing-chart|moi-phat-hanh))/?(?:[#?]|$)' | |
223 | _TESTS = [{ | |
224 | 'url': 'https://zingmp3.vn/zing-chart', | |
225 | 'info_dict': { | |
226 | 'id': 'zing-chart', | |
54044dec HTL |
227 | }, |
228 | 'playlist_mincount': 100, | |
229 | }, { | |
230 | 'url': 'https://zingmp3.vn/moi-phat-hanh', | |
231 | 'info_dict': { | |
232 | 'id': 'moi-phat-hanh', | |
54044dec HTL |
233 | }, |
234 | 'playlist_mincount': 100, | |
235 | }] | |
236 | IE_NAME = 'zingmp3:chart-home' | |
237 | ||
238 | def _real_extract(self, url): | |
6b70527f | 239 | url_type = self._match_id(url) |
240 | data = self._call_api(url_type, {'id': url_type}) | |
241 | items = traverse_obj(data, ('RTChart', 'items') if url_type == 'zing-chart' else 'items') | |
242 | return self.playlist_result(self._parse_items(items), url_type) | |
54044dec HTL |
243 | |
244 | ||
245 | class ZingMp3WeekChartIE(ZingMp3BaseIE): | |
6b70527f | 246 | _VALID_URL = ZingMp3BaseIE._VALID_URL_TMPL % 'zing-chart-tuan' |
54044dec HTL |
247 | IE_NAME = 'zingmp3:week-chart' |
248 | _TESTS = [{ | |
249 | 'url': 'https://zingmp3.vn/zing-chart-tuan/Bai-hat-Viet-Nam/IWZ9Z08I.html', | |
250 | 'info_dict': { | |
251 | 'id': 'IWZ9Z08I', | |
252 | 'title': 'zing-chart-vn', | |
253 | }, | |
254 | 'playlist_mincount': 10, | |
255 | }, { | |
256 | 'url': 'https://zingmp3.vn/zing-chart-tuan/Bai-hat-US-UK/IWZ9Z0BW.html', | |
257 | 'info_dict': { | |
258 | 'id': 'IWZ9Z0BW', | |
259 | 'title': 'zing-chart-us', | |
260 | }, | |
261 | 'playlist_mincount': 10, | |
262 | }, { | |
263 | 'url': 'https://zingmp3.vn/zing-chart-tuan/Bai-hat-KPop/IWZ9Z0BO.html', | |
264 | 'info_dict': { | |
265 | 'id': 'IWZ9Z0BO', | |
266 | 'title': 'zing-chart-korea', | |
267 | }, | |
268 | 'playlist_mincount': 10, | |
269 | }] | |
270 | ||
6b70527f | 271 | def _real_extract(self, url): |
272 | song_id, url_type = self._match_valid_url(url).group('id', 'type') | |
273 | data = self._call_api(url_type, {'id': song_id}) | |
274 | return self.playlist_result( | |
275 | self._parse_items(data['items']), song_id, f'zing-chart-{data.get("country", "")}') | |
54044dec HTL |
276 | |
277 | ||
278 | class ZingMp3ChartMusicVideoIE(ZingMp3BaseIE): | |
279 | _VALID_URL = r'https?://(?:mp3\.zing|zingmp3)\.vn/(?P<type>the-loai-video)/(?P<regions>[^/]+)/(?P<id>[^\.]+)' | |
280 | IE_NAME = 'zingmp3:chart-music-video' | |
281 | _TESTS = [{ | |
282 | 'url': 'https://zingmp3.vn/the-loai-video/Viet-Nam/IWZ9Z08I.html', | |
283 | 'info_dict': { | |
284 | 'id': 'IWZ9Z08I', | |
285 | 'title': 'the-loai-video_Viet-Nam', | |
286 | }, | |
287 | 'playlist_mincount': 400, | |
288 | }, { | |
289 | 'url': 'https://zingmp3.vn/the-loai-video/Au-My/IWZ9Z08O.html', | |
290 | 'info_dict': { | |
291 | 'id': 'IWZ9Z08O', | |
292 | 'title': 'the-loai-video_Au-My', | |
293 | }, | |
294 | 'playlist_mincount': 40, | |
295 | }, { | |
296 | 'url': 'https://zingmp3.vn/the-loai-video/Han-Quoc/IWZ9Z08W.html', | |
297 | 'info_dict': { | |
298 | 'id': 'IWZ9Z08W', | |
299 | 'title': 'the-loai-video_Han-Quoc', | |
300 | }, | |
301 | 'playlist_mincount': 30, | |
302 | }, { | |
303 | 'url': 'https://zingmp3.vn/the-loai-video/Khong-Loi/IWZ9Z086.html', | |
304 | 'info_dict': { | |
305 | 'id': 'IWZ9Z086', | |
306 | 'title': 'the-loai-video_Khong-Loi', | |
307 | }, | |
308 | 'playlist_mincount': 10, | |
309 | }] | |
310 | ||
6b70527f | 311 | def _fetch_page(self, song_id, url_type, page): |
312 | return self._parse_items(self._call_api(url_type, { | |
54044dec HTL |
313 | 'id': song_id, |
314 | 'type': 'genre', | |
6b70527f | 315 | 'page': page + 1, |
54044dec | 316 | 'count': self._PER_PAGE |
6b70527f | 317 | }).get('items')) |
54044dec HTL |
318 | |
319 | def _real_extract(self, url): | |
6b70527f | 320 | song_id, regions, url_type = self._match_valid_url(url).group('id', 'regions', 'type') |
321 | return self.playlist_result( | |
322 | OnDemandPagedList(functools.partial(self._fetch_page, song_id, url_type), self._PER_PAGE), | |
323 | song_id, f'{url_type}_{regions}') | |
54044dec HTL |
324 | |
325 | ||
326 | class ZingMp3UserIE(ZingMp3BaseIE): | |
6b70527f | 327 | _VALID_URL = r'https?://(?:mp3\.zing|zingmp3)\.vn/(?P<user>[^/]+)/(?P<type>bai-hat|single|album|video)/?(?:[?#]|$)' |
54044dec HTL |
328 | IE_NAME = 'zingmp3:user' |
329 | _TESTS = [{ | |
330 | 'url': 'https://zingmp3.vn/Mr-Siro/bai-hat', | |
331 | 'info_dict': { | |
332 | 'id': 'IWZ98609', | |
333 | 'title': 'Mr. Siro - bai-hat', | |
334 | 'description': 'md5:85ab29bd7b21725c12bf76fd1d6922e5', | |
335 | }, | |
336 | 'playlist_mincount': 91, | |
337 | }, { | |
338 | 'url': 'https://zingmp3.vn/Mr-Siro/album', | |
339 | 'info_dict': { | |
340 | 'id': 'IWZ98609', | |
341 | 'title': 'Mr. Siro - album', | |
342 | 'description': 'md5:85ab29bd7b21725c12bf76fd1d6922e5', | |
343 | }, | |
344 | 'playlist_mincount': 3, | |
345 | }, { | |
346 | 'url': 'https://zingmp3.vn/Mr-Siro/single', | |
347 | 'info_dict': { | |
348 | 'id': 'IWZ98609', | |
349 | 'title': 'Mr. Siro - single', | |
350 | 'description': 'md5:85ab29bd7b21725c12bf76fd1d6922e5', | |
351 | }, | |
352 | 'playlist_mincount': 20, | |
353 | }, { | |
354 | 'url': 'https://zingmp3.vn/Mr-Siro/video', | |
355 | 'info_dict': { | |
356 | 'id': 'IWZ98609', | |
357 | 'title': 'Mr. Siro - video', | |
358 | 'description': 'md5:85ab29bd7b21725c12bf76fd1d6922e5', | |
359 | }, | |
360 | 'playlist_mincount': 15, | |
361 | }] | |
362 | ||
6b70527f | 363 | def _fetch_page(self, user_id, url_type, page): |
364 | url_type = 'user-list-song' if url_type == 'bai-hat' else 'user-list-video' | |
365 | return self._parse_items(self._call_api(url_type, { | |
54044dec HTL |
366 | 'id': user_id, |
367 | 'type': 'artist', | |
6b70527f | 368 | 'page': page + 1, |
54044dec | 369 | 'count': self._PER_PAGE |
6b70527f | 370 | }, query={'sort': 'new', 'sectionId': 'aSong'}).get('items')) |
54044dec HTL |
371 | |
372 | def _real_extract(self, url): | |
6b70527f | 373 | user_alias, url_type = self._match_valid_url(url).group('user', 'type') |
374 | if not url_type: | |
375 | url_type = 'bai-hat' | |
376 | ||
377 | user_info = self._call_api('info-artist', {}, user_alias, query={'alias': user_alias}) | |
378 | if url_type in ('bai-hat', 'video'): | |
379 | entries = OnDemandPagedList( | |
380 | functools.partial(self._fetch_page, user_info['id'], url_type), self._PER_PAGE) | |
54044dec | 381 | else: |
6b70527f | 382 | entries = self._parse_items(traverse_obj(user_info, ( |
383 | 'sections', lambda _, v: v['link'] == f'/{user_alias}/{url_type}', 'items', ...))) | |
384 | return self.playlist_result( | |
385 | entries, user_info['id'], f'{user_info.get("name")} - {url_type}', user_info.get('biography')) |