]>
Commit | Line | Data |
---|---|---|
ecca4519 HTL |
1 | import hashlib |
2 | import hmac | |
3 | import urllib.parse | |
4 | ||
c66bdc48 | 5 | from .common import InfoExtractor |
3d47ee0a | 6 | from ..utils import ( |
3d47ee0a | 7 | int_or_none, |
ecca4519 | 8 | traverse_obj, |
3d47ee0a | 9 | ) |
c66bdc48 DHS |
10 | |
11 | ||
1418a043 | 12 | class ZingMp3BaseIE(InfoExtractor): |
ecca4519 | 13 | _VALID_URL_TMPL = r'https?://(?:mp3\.zing|zingmp3)\.vn/(?P<type>(?:%s))/[^/]+/(?P<id>\w+)(?:\.html|\?)' |
1418a043 | 14 | _GEO_COUNTRIES = ['VN'] |
ecca4519 HTL |
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' | |
c66bdc48 | 28 | |
ecca4519 HTL |
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') | |
51156528 | 38 | |
3d47ee0a | 39 | formats = [] |
ecca4519 | 40 | for k, v in (source or {}).items(): |
1418a043 | 41 | if not v: |
3d47ee0a | 42 | continue |
1418a043 | 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 | }) | |
ecca4519 HTL |
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 | }) | |
1418a043 | 68 | if not formats: |
69 | if not fatal: | |
70 | return | |
ecca4519 | 71 | msg = item.get('msg') |
1418a043 | 72 | if msg == 'Sorry, this content is not available in your country.': |
b7da73eb | 73 | self.raise_geo_restricted(countries=self._GEO_COUNTRIES, metadata_available=True) |
74 | self.raise_no_formats(msg, expected=True) | |
1418a043 | 75 | self._sort_formats(formats) |
76 | ||
1418a043 | 77 | lyric = item.get('lyric') |
ecca4519 HTL |
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 | |
3d47ee0a | 87 | |
1418a043 | 88 | album = item.get('album') or {} |
c66bdc48 DHS |
89 | |
90 | return { | |
1418a043 | 91 | 'id': item_id, |
92 | 'title': title, | |
3d47ee0a | 93 | 'formats': formats, |
ecca4519 | 94 | 'thumbnail': traverse_obj(item, 'thumbnail', 'thumbnailM'), |
1418a043 | 95 | 'subtitles': subtitles, |
96 | 'duration': int_or_none(item.get('duration')), | |
97 | 'track': title, | |
ecca4519 HTL |
98 | 'artist': traverse_obj(item, 'artistsNames', 'artists_names'), |
99 | 'album': traverse_obj(album, 'name', 'title'), | |
100 | 'album_artist': traverse_obj(album, 'artistsNames', 'artists_names'), | |
c66bdc48 DHS |
101 | } |
102 | ||
ecca4519 HTL |
103 | def _real_initialize(self): |
104 | if not self.get_param('cookiefile') and not self.get_param('cookiesfrombrowser'): | |
63b2f88b HTL |
105 | self._request_webpage(self.get_api_with_signature(name_api=self._SLUG_API['bai-hat'], param={'id': ''}), |
106 | None, note='Updating cookies') | |
ecca4519 | 107 | |
1418a043 | 108 | def _real_extract(self, url): |
ecca4519 | 109 | song_id, type_url = self._match_valid_url(url).group('id', 'type') |
ecca4519 | 110 | api = self.get_api_with_signature(name_api=self._SLUG_API[type_url], param={'id': song_id}) |
ecca4519 HTL |
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): | |
63b2f88b HTL |
114 | param.update({'ctime': '1'}) |
115 | sha256 = hashlib.sha256(''.join(f'{i}={param[i]}' for i in sorted(param)).encode('utf-8')).hexdigest() | |
ecca4519 HTL |
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)}' | |
c66bdc48 DHS |
122 | |
123 | ||
1418a043 | 124 | class ZingMp3IE(ZingMp3BaseIE): |
ecca4519 | 125 | _VALID_URL = ZingMp3BaseIE._VALID_URL_TMPL % 'bai-hat|video-clip|embed' |
c66bdc48 | 126 | _TESTS = [{ |
ecca4519 | 127 | 'url': 'https://mp3.zing.vn/bai-hat/Xa-Mai-Xa-Bao-Thy/ZWZB9WAB.html', |
4ffc3103 | 128 | 'md5': 'ead7ae13693b3205cbc89536a077daed', |
c66bdc48 DHS |
129 | 'info_dict': { |
130 | 'id': 'ZWZB9WAB', | |
4ffc3103 | 131 | 'title': 'Xa Mãi Xa', |
c66bdc48 | 132 | 'ext': 'mp3', |
1418a043 | 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', | |
c66bdc48 | 144 | }, |
3d47ee0a | 145 | }, { |
ecca4519 | 146 | 'url': 'https://zingmp3.vn/video-clip/Suong-Hoa-Dua-Loi-K-ICM-RYO/ZO8ZF7C7.html', |
08d30158 | 147 | 'md5': 'c7f23d971ac1a4f675456ed13c9b9612', |
3d47ee0a | 148 | 'info_dict': { |
1418a043 | 149 | 'id': 'ZO8ZF7C7', |
150 | 'title': 'Sương Hoa Đưa Lối', | |
3d47ee0a | 151 | 'ext': 'mp4', |
1418a043 | 152 | 'thumbnail': r're:^https?://.+\.jpg', |
153 | 'duration': 207, | |
154 | 'track': 'Sương Hoa Đưa Lối', | |
155 | 'artist': 'K-ICM, RYO', | |
08d30158 | 156 | 'album': 'Sương Hoa Đưa Lối (Single)', |
157 | 'album_artist': 'K-ICM, RYO', | |
3d47ee0a | 158 | }, |
63b2f88b HTL |
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 | }, | |
ecca4519 HTL |
173 | }, { |
174 | 'url': 'https://zingmp3.vn/embed/song/ZWZEI76B?start=false', | |
175 | 'only_matching': True, | |
3d47ee0a | 176 | }, { |
1418a043 | 177 | 'url': 'https://zingmp3.vn/bai-hat/Xa-Mai-Xa-Bao-Thy/ZWZB9WAB.html', |
178 | 'only_matching': True, | |
179 | }] | |
180 | IE_NAME = 'zingmp3' | |
ecca4519 | 181 | IE_DESC = 'zingmp3.vn' |
1418a043 | 182 | |
ecca4519 HTL |
183 | def _process_data(self, data, song_id, type_url): |
184 | return self._extract_item(data, song_id, type_url, True) | |
1418a043 | 185 | |
186 | ||
187 | class ZingMp3AlbumIE(ZingMp3BaseIE): | |
188 | _VALID_URL = ZingMp3BaseIE._VALID_URL_TMPL % 'album|playlist' | |
189 | _TESTS = [{ | |
43abd799 S |
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', | |
1418a043 | 194 | 'title': 'Lâu Đài Tình Ái', |
c66bdc48 | 195 | }, |
ecca4519 | 196 | 'playlist_count': 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': { | |
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, | |
43abd799 S |
205 | }, { |
206 | 'url': 'http://mp3.zing.vn/playlist/Duong-Hong-Loan-apollobee/IWCAACCB.html', | |
207 | 'only_matching': True, | |
1418a043 | 208 | }, { |
209 | 'url': 'https://zingmp3.vn/album/Lau-Dai-Tinh-Ai-Bang-Kieu-Minh-Tuyet/ZWZBWDAF.html', | |
210 | 'only_matching': True, | |
43abd799 | 211 | }] |
1418a043 | 212 | IE_NAME = 'zingmp3:album' |
213 | ||
ecca4519 | 214 | def _process_data(self, data, song_id, type_url): |
1418a043 | 215 | def entries(): |
ecca4519 HTL |
216 | for item in traverse_obj(data, ('song', 'items')) or []: |
217 | entry = self._extract_item(item, song_id, type_url, False) | |
1418a043 | 218 | if entry: |
219 | yield entry | |
ecca4519 HTL |
220 | |
221 | return self.playlist_result(entries(), traverse_obj(data, 'id', 'encodeId'), | |
222 | traverse_obj(data, 'name', 'title')) |