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