]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/zingmp3.py
[cleanup, docs] Misc cleanup
[yt-dlp.git] / yt_dlp / extractor / zingmp3.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import hashlib
5 import hmac
6 import urllib.parse
7
8 from .common import InfoExtractor
9 from ..utils import (
10 int_or_none,
11 traverse_obj,
12 HEADRequest,
13 )
14
15
16 class ZingMp3BaseIE(InfoExtractor):
17 _VALID_URL_TMPL = r'https?://(?:mp3\.zing|zingmp3)\.vn/(?P<type>(?:%s))/[^/]+/(?P<id>\w+)(?:\.html|\?)'
18 _GEO_COUNTRIES = ['VN']
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'
32
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')
42
43 formats = []
44 for k, v in (source or {}).items():
45 if not v:
46 continue
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 })
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 })
72 if not formats:
73 if not fatal:
74 return
75 msg = item.get('msg')
76 if msg == 'Sorry, this content is not available in your country.':
77 self.raise_geo_restricted(countries=self._GEO_COUNTRIES, metadata_available=True)
78 self.raise_no_formats(msg, expected=True)
79 self._sort_formats(formats)
80
81 lyric = item.get('lyric')
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
91
92 album = item.get('album') or {}
93
94 return {
95 'id': item_id,
96 'title': title,
97 'formats': formats,
98 'thumbnail': traverse_obj(item, 'thumbnail', 'thumbnailM'),
99 'subtitles': subtitles,
100 'duration': int_or_none(item.get('duration')),
101 'track': title,
102 'artist': traverse_obj(item, 'artistsNames', 'artists_names'),
103 'album': traverse_obj(album, 'name', 'title'),
104 'album_artist': traverse_obj(album, 'artistsNames', 'artists_names'),
105 }
106
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
111 def _real_extract(self, url):
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)}'
127
128
129 class ZingMp3IE(ZingMp3BaseIE):
130 _VALID_URL = ZingMp3BaseIE._VALID_URL_TMPL % 'bai-hat|video-clip|embed'
131 _TESTS = [{
132 'url': 'https://mp3.zing.vn/bai-hat/Xa-Mai-Xa-Bao-Thy/ZWZB9WAB.html',
133 'md5': 'ead7ae13693b3205cbc89536a077daed',
134 'info_dict': {
135 'id': 'ZWZB9WAB',
136 'title': 'Xa Mãi Xa',
137 'ext': 'mp3',
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',
149 },
150 }, {
151 'url': 'https://zingmp3.vn/video-clip/Suong-Hoa-Dua-Loi-K-ICM-RYO/ZO8ZF7C7.html',
152 'md5': 'c7f23d971ac1a4f675456ed13c9b9612',
153 'info_dict': {
154 'id': 'ZO8ZF7C7',
155 'title': 'Sương Hoa Đưa Lối',
156 'ext': 'mp4',
157 'thumbnail': r're:^https?://.+\.jpg',
158 'duration': 207,
159 'track': 'Sương Hoa Đưa Lối',
160 'artist': 'K-ICM, RYO',
161 'album': 'Sương Hoa Đưa Lối (Single)',
162 'album_artist': 'K-ICM, RYO',
163 },
164 }, {
165 'url': 'https://zingmp3.vn/embed/song/ZWZEI76B?start=false',
166 'only_matching': True,
167 }, {
168 'url': 'https://zingmp3.vn/bai-hat/Xa-Mai-Xa-Bao-Thy/ZWZB9WAB.html',
169 'only_matching': True,
170 }]
171 IE_NAME = 'zingmp3'
172 IE_DESC = 'zingmp3.vn'
173
174 def _process_data(self, data, song_id, type_url):
175 return self._extract_item(data, song_id, type_url, True)
176
177
178 class ZingMp3AlbumIE(ZingMp3BaseIE):
179 _VALID_URL = ZingMp3BaseIE._VALID_URL_TMPL % 'album|playlist'
180 _TESTS = [{
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',
185 'title': 'Lâu Đài Tình Ái',
186 },
187 'playlist_count': 9,
188 }, {
189 'url': 'http://mp3.zing.vn/playlist/Duong-Hong-Loan-apollobee/IWCAACCB.html',
190 'only_matching': True,
191 }, {
192 'url': 'https://zingmp3.vn/album/Lau-Dai-Tinh-Ai-Bang-Kieu-Minh-Tuyet/ZWZBWDAF.html',
193 'only_matching': True,
194 }]
195 IE_NAME = 'zingmp3:album'
196
197 def _process_data(self, data, song_id, type_url):
198 def entries():
199 for item in traverse_obj(data, ('song', 'items')) or []:
200 entry = self._extract_item(item, song_id, type_url, False)
201 if entry:
202 yield entry
203
204 return self.playlist_result(entries(), traverse_obj(data, 'id', 'encodeId'),
205 traverse_obj(data, 'name', 'title'))