]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/zingmp3.py
[cleanup] Upgrade syntax
[yt-dlp.git] / yt_dlp / extractor / zingmp3.py
1 import hashlib
2 import hmac
3 import urllib.parse
4
5 from .common import InfoExtractor
6 from ..utils import (
7 int_or_none,
8 traverse_obj,
9 )
10
11
12 class ZingMp3BaseIE(InfoExtractor):
13 _VALID_URL_TMPL = r'https?://(?:mp3\.zing|zingmp3)\.vn/(?P<type>(?:%s))/[^/]+/(?P<id>\w+)(?:\.html|\?)'
14 _GEO_COUNTRIES = ['VN']
15 _DOMAIN = 'https://zingmp3.vn'
16 _SLUG_API = {
17 'bai-hat': '/api/v2/page/get/song',
18 'embed': '/api/v2/page/get/song',
19 'video-clip': '/api/v2/page/get/video',
20 'playlist': '/api/v2/page/get/playlist',
21 'album': '/api/v2/page/get/playlist',
22 'lyric': '/api/v2/lyric/get/lyric',
23 'song_streaming': '/api/v2/song/get/streaming',
24 }
25
26 _API_KEY = '88265e23d4284f25963e6eedac8fbfa3'
27 _SECRET_KEY = b'2aa2d1c561e809b267f3638c4a307aab'
28
29 def _extract_item(self, item, song_id, type_url, fatal):
30 item_id = item.get('encodeId') or song_id
31 title = item.get('title') or item.get('alias')
32
33 if type_url == 'video-clip':
34 source = item.get('streaming')
35 else:
36 api = self.get_api_with_signature(name_api=self._SLUG_API.get('song_streaming'), param={'id': item_id})
37 source = self._download_json(api, video_id=item_id).get('data')
38
39 formats = []
40 for k, v in (source or {}).items():
41 if not v:
42 continue
43 if k in ('mp4', 'hls'):
44 for res, video_url in v.items():
45 if not video_url:
46 continue
47 if k == 'hls':
48 formats.extend(self._extract_m3u8_formats(
49 video_url, item_id, 'mp4',
50 'm3u8_native', m3u8_id=k, fatal=False))
51 elif k == 'mp4':
52 formats.append({
53 'format_id': 'mp4-' + res,
54 'url': video_url,
55 'height': int_or_none(self._search_regex(
56 r'^(\d+)p', res, 'resolution', default=None)),
57 })
58 continue
59 elif v == 'VIP':
60 continue
61 formats.append({
62 'ext': 'mp3',
63 'format_id': k,
64 'tbr': int_or_none(k),
65 'url': self._proto_relative_url(v),
66 'vcodec': 'none',
67 })
68 if not formats:
69 if not fatal:
70 return
71 msg = item.get('msg')
72 if msg == 'Sorry, this content is not available in your country.':
73 self.raise_geo_restricted(countries=self._GEO_COUNTRIES, metadata_available=True)
74 self.raise_no_formats(msg, expected=True)
75 self._sort_formats(formats)
76
77 lyric = item.get('lyric')
78 if not lyric:
79 api = self.get_api_with_signature(name_api=self._SLUG_API.get("lyric"), param={'id': item_id})
80 info_lyric = self._download_json(api, video_id=item_id)
81 lyric = traverse_obj(info_lyric, ('data', 'file'))
82 subtitles = {
83 'origin': [{
84 'url': lyric,
85 }],
86 } if lyric else None
87
88 album = item.get('album') or {}
89
90 return {
91 'id': item_id,
92 'title': title,
93 'formats': formats,
94 'thumbnail': traverse_obj(item, 'thumbnail', 'thumbnailM'),
95 'subtitles': subtitles,
96 'duration': int_or_none(item.get('duration')),
97 'track': title,
98 'artist': traverse_obj(item, 'artistsNames', 'artists_names'),
99 'album': traverse_obj(album, 'name', 'title'),
100 'album_artist': traverse_obj(album, 'artistsNames', 'artists_names'),
101 }
102
103 def _real_initialize(self):
104 if not self.get_param('cookiefile') and not self.get_param('cookiesfrombrowser'):
105 self._request_webpage(self.get_api_with_signature(name_api=self._SLUG_API['bai-hat'], param={'id': ''}),
106 None, note='Updating cookies')
107
108 def _real_extract(self, url):
109 song_id, type_url = self._match_valid_url(url).group('id', 'type')
110 api = self.get_api_with_signature(name_api=self._SLUG_API[type_url], param={'id': song_id})
111 return self._process_data(self._download_json(api, song_id)['data'], song_id, type_url)
112
113 def get_api_with_signature(self, name_api, param):
114 param.update({'ctime': '1'})
115 sha256 = hashlib.sha256(''.join(f'{i}={param[i]}' for i in sorted(param)).encode('utf-8')).hexdigest()
116 data = {
117 'apiKey': self._API_KEY,
118 'sig': hmac.new(self._SECRET_KEY, f'{name_api}{sha256}'.encode('utf-8'), hashlib.sha512).hexdigest(),
119 **param,
120 }
121 return f'{self._DOMAIN}{name_api}?{urllib.parse.urlencode(data)}'
122
123
124 class ZingMp3IE(ZingMp3BaseIE):
125 _VALID_URL = ZingMp3BaseIE._VALID_URL_TMPL % 'bai-hat|video-clip|embed'
126 _TESTS = [{
127 'url': 'https://mp3.zing.vn/bai-hat/Xa-Mai-Xa-Bao-Thy/ZWZB9WAB.html',
128 'md5': 'ead7ae13693b3205cbc89536a077daed',
129 'info_dict': {
130 'id': 'ZWZB9WAB',
131 'title': 'Xa Mãi Xa',
132 'ext': 'mp3',
133 'thumbnail': r're:^https?://.+\.jpg',
134 'subtitles': {
135 'origin': [{
136 'ext': 'lrc',
137 }]
138 },
139 'duration': 255,
140 'track': 'Xa Mãi Xa',
141 'artist': 'Bảo Thy',
142 'album': 'Special Album',
143 'album_artist': 'Bảo Thy',
144 },
145 }, {
146 'url': 'https://zingmp3.vn/video-clip/Suong-Hoa-Dua-Loi-K-ICM-RYO/ZO8ZF7C7.html',
147 'md5': 'c7f23d971ac1a4f675456ed13c9b9612',
148 'info_dict': {
149 'id': 'ZO8ZF7C7',
150 'title': 'Sương Hoa Đưa Lối',
151 'ext': 'mp4',
152 'thumbnail': r're:^https?://.+\.jpg',
153 'duration': 207,
154 'track': 'Sương Hoa Đưa Lối',
155 'artist': 'K-ICM, RYO',
156 'album': 'Sương Hoa Đưa Lối (Single)',
157 'album_artist': 'K-ICM, RYO',
158 },
159 }, {
160 'url': 'https://zingmp3.vn/bai-hat/Nguoi-Yeu-Toi-Lanh-Lung-Sat-Da-Mr-Siro/ZZ6IW7OU.html',
161 'md5': '3e9f7a9bd0d965573dbff8d7c68b629d',
162 'info_dict': {
163 'id': 'ZZ6IW7OU',
164 'title': 'Người Yêu Tôi Lạnh Lùng Sắt Đá',
165 'ext': 'mp3',
166 'thumbnail': r're:^https?://.+\.jpg',
167 'duration': 303,
168 'track': 'Người Yêu Tôi Lạnh Lùng Sắt Đá',
169 'artist': 'Mr. Siro',
170 'album': 'Người Yêu Tôi Lạnh Lùng Sắt Đá (Single)',
171 'album_artist': 'Mr. Siro',
172 },
173 }, {
174 'url': 'https://zingmp3.vn/embed/song/ZWZEI76B?start=false',
175 'only_matching': True,
176 }, {
177 'url': 'https://zingmp3.vn/bai-hat/Xa-Mai-Xa-Bao-Thy/ZWZB9WAB.html',
178 'only_matching': True,
179 }]
180 IE_NAME = 'zingmp3'
181 IE_DESC = 'zingmp3.vn'
182
183 def _process_data(self, data, song_id, type_url):
184 return self._extract_item(data, song_id, type_url, True)
185
186
187 class ZingMp3AlbumIE(ZingMp3BaseIE):
188 _VALID_URL = ZingMp3BaseIE._VALID_URL_TMPL % 'album|playlist'
189 _TESTS = [{
190 'url': 'http://mp3.zing.vn/album/Lau-Dai-Tinh-Ai-Bang-Kieu-Minh-Tuyet/ZWZBWDAF.html',
191 'info_dict': {
192 '_type': 'playlist',
193 'id': 'ZWZBWDAF',
194 'title': 'Lâu Đài Tình Ái',
195 },
196 'playlist_count': 9,
197 }, {
198 'url': 'https://zingmp3.vn/album/Nhung-Bai-Hat-Hay-Nhat-Cua-Mr-Siro-Mr-Siro/ZWZAEZZD.html',
199 'info_dict': {
200 '_type': 'playlist',
201 'id': 'ZWZAEZZD',
202 'title': 'Những Bài Hát Hay Nhất Của Mr. Siro',
203 },
204 'playlist_count': 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 _process_data(self, data, song_id, type_url):
215 def entries():
216 for item in traverse_obj(data, ('song', 'items')) or []:
217 entry = self._extract_item(item, song_id, type_url, False)
218 if entry:
219 yield entry
220
221 return self.playlist_result(entries(), traverse_obj(data, 'id', 'encodeId'),
222 traverse_obj(data, 'name', 'title'))