[zingmp3] Fix extractor (#2889)

Authored by: hatienl0i261299
This commit is contained in:
Ha Tien Loi 2022-03-04 18:22:45 +07:00 committed by GitHub
parent 761fba6d22
commit ecca4519b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 89 additions and 46 deletions

View File

@ -1,22 +1,47 @@
# coding: utf-8 # coding: utf-8
from __future__ import unicode_literals from __future__ import unicode_literals
import hashlib
import hmac
import urllib.parse
from .common import InfoExtractor from .common import InfoExtractor
from ..utils import ( from ..utils import (
int_or_none, int_or_none,
traverse_obj,
HEADRequest,
) )
class ZingMp3BaseIE(InfoExtractor): class ZingMp3BaseIE(InfoExtractor):
_VALID_URL_TMPL = r'https?://(?:mp3\.zing|zingmp3)\.vn/(?:%s)/[^/]+/(?P<id>\w+)\.html' _VALID_URL_TMPL = r'https?://(?:mp3\.zing|zingmp3)\.vn/(?P<type>(?:%s))/[^/]+/(?P<id>\w+)(?:\.html|\?)'
_GEO_COUNTRIES = ['VN'] _GEO_COUNTRIES = ['VN']
_DOMAIN = 'https://zingmp3.vn'
_SLUG_API = {
'bai-hat': '/api/v2/page/get/song',
'embed': '/api/v2/page/get/song',
'video-clip': '/api/v2/page/get/video',
'playlist': '/api/v2/page/get/playlist',
'album': '/api/v2/page/get/playlist',
'lyric': '/api/v2/lyric/get/lyric',
'song_streaming': '/api/v2/song/get/streaming',
}
def _extract_item(self, item, fatal): _API_KEY = '88265e23d4284f25963e6eedac8fbfa3'
item_id = item['id'] _SECRET_KEY = b'2aa2d1c561e809b267f3638c4a307aab'
title = item.get('name') or item['title']
def _extract_item(self, item, song_id, type_url, fatal):
item_id = item.get('encodeId') or song_id
title = item.get('title') or item.get('alias')
if type_url == 'video-clip':
source = item.get('streaming')
else:
api = self.get_api_with_signature(name_api=self._SLUG_API.get('song_streaming'), param={'id': item_id})
source = self._download_json(api, video_id=item_id).get('data')
formats = [] formats = []
for k, v in (item.get('source') or {}).items(): for k, v in (source or {}).items():
if not v: if not v:
continue continue
if k in ('mp4', 'hls'): if k in ('mp4', 'hls'):
@ -34,31 +59,35 @@ class ZingMp3BaseIE(InfoExtractor):
'height': int_or_none(self._search_regex( 'height': int_or_none(self._search_regex(
r'^(\d+)p', res, 'resolution', default=None)), r'^(\d+)p', res, 'resolution', default=None)),
}) })
else: continue
formats.append({ elif v == 'VIP':
'ext': 'mp3', continue
'format_id': k, formats.append({
'tbr': int_or_none(k), 'ext': 'mp3',
'url': self._proto_relative_url(v), 'format_id': k,
'vcodec': 'none', 'tbr': int_or_none(k),
}) 'url': self._proto_relative_url(v),
'vcodec': 'none',
})
if not formats: if not formats:
if not fatal: if not fatal:
return return
msg = item['msg'] msg = item.get('msg')
if msg == 'Sorry, this content is not available in your country.': if msg == 'Sorry, this content is not available in your country.':
self.raise_geo_restricted(countries=self._GEO_COUNTRIES, metadata_available=True) self.raise_geo_restricted(countries=self._GEO_COUNTRIES, metadata_available=True)
self.raise_no_formats(msg, expected=True) self.raise_no_formats(msg, expected=True)
self._sort_formats(formats) self._sort_formats(formats)
subtitles = None
lyric = item.get('lyric') lyric = item.get('lyric')
if lyric: if not lyric:
subtitles = { api = self.get_api_with_signature(name_api=self._SLUG_API.get("lyric"), param={'id': item_id})
'origin': [{ info_lyric = self._download_json(api, video_id=item_id)
'url': lyric, lyric = traverse_obj(info_lyric, ('data', 'file'))
}], subtitles = {
} 'origin': [{
'url': lyric,
}],
} if lyric else None
album = item.get('album') or {} album = item.get('album') or {}
@ -66,30 +95,41 @@ class ZingMp3BaseIE(InfoExtractor):
'id': item_id, 'id': item_id,
'title': title, 'title': title,
'formats': formats, 'formats': formats,
'thumbnail': item.get('thumbnail'), 'thumbnail': traverse_obj(item, 'thumbnail', 'thumbnailM'),
'subtitles': subtitles, 'subtitles': subtitles,
'duration': int_or_none(item.get('duration')), 'duration': int_or_none(item.get('duration')),
'track': title, 'track': title,
'artist': item.get('artists_names'), 'artist': traverse_obj(item, 'artistsNames', 'artists_names'),
'album': album.get('name') or album.get('title'), 'album': traverse_obj(album, 'name', 'title'),
'album_artist': album.get('artists_names'), 'album_artist': traverse_obj(album, 'artistsNames', 'artists_names'),
} }
def _real_initialize(self):
if not self.get_param('cookiefile') and not self.get_param('cookiesfrombrowser'):
self._request_webpage(HEADRequest(self._DOMAIN), None, note='Updating cookies')
def _real_extract(self, url): def _real_extract(self, url):
page_id = self._match_id(url) song_id, type_url = self._match_valid_url(url).group('id', 'type')
webpage = self._download_webpage(
url.replace('://zingmp3.vn/', '://mp3.zing.vn/'), api = self.get_api_with_signature(name_api=self._SLUG_API[type_url], param={'id': song_id})
page_id, query={'play_song': 1})
data_path = self._search_regex( return self._process_data(self._download_json(api, song_id)['data'], song_id, type_url)
r'data-xml="([^"]+)', webpage, 'data path')
return self._process_data(self._download_json( def get_api_with_signature(self, name_api, param):
'https://mp3.zing.vn/xhr' + data_path, page_id)['data']) sha256 = hashlib.sha256(''.join(f'{k}={v}' for k, v in param.items()).encode('utf-8')).hexdigest()
data = {
'apiKey': self._API_KEY,
'sig': hmac.new(self._SECRET_KEY, f'{name_api}{sha256}'.encode('utf-8'), hashlib.sha512).hexdigest(),
**param,
}
return f'{self._DOMAIN}{name_api}?{urllib.parse.urlencode(data)}'
class ZingMp3IE(ZingMp3BaseIE): class ZingMp3IE(ZingMp3BaseIE):
_VALID_URL = ZingMp3BaseIE._VALID_URL_TMPL % 'bai-hat|video-clip' _VALID_URL = ZingMp3BaseIE._VALID_URL_TMPL % 'bai-hat|video-clip|embed'
_TESTS = [{ _TESTS = [{
'url': 'http://mp3.zing.vn/bai-hat/Xa-Mai-Xa-Bao-Thy/ZWZB9WAB.html', 'url': 'https://mp3.zing.vn/bai-hat/Xa-Mai-Xa-Bao-Thy/ZWZB9WAB.html',
'md5': 'ead7ae13693b3205cbc89536a077daed', 'md5': 'ead7ae13693b3205cbc89536a077daed',
'info_dict': { 'info_dict': {
'id': 'ZWZB9WAB', 'id': 'ZWZB9WAB',
@ -108,7 +148,7 @@ class ZingMp3IE(ZingMp3BaseIE):
'album_artist': 'Bảo Thy', 'album_artist': 'Bảo Thy',
}, },
}, { }, {
'url': 'https://mp3.zing.vn/video-clip/Suong-Hoa-Dua-Loi-K-ICM-RYO/ZO8ZF7C7.html', 'url': 'https://zingmp3.vn/video-clip/Suong-Hoa-Dua-Loi-K-ICM-RYO/ZO8ZF7C7.html',
'md5': 'e9c972b693aa88301ef981c8151c4343', 'md5': 'e9c972b693aa88301ef981c8151c4343',
'info_dict': { 'info_dict': {
'id': 'ZO8ZF7C7', 'id': 'ZO8ZF7C7',
@ -119,15 +159,18 @@ class ZingMp3IE(ZingMp3BaseIE):
'track': 'Sương Hoa Đưa Lối', 'track': 'Sương Hoa Đưa Lối',
'artist': 'K-ICM, RYO', 'artist': 'K-ICM, RYO',
}, },
}, {
'url': 'https://zingmp3.vn/embed/song/ZWZEI76B?start=false',
'only_matching': True,
}, { }, {
'url': 'https://zingmp3.vn/bai-hat/Xa-Mai-Xa-Bao-Thy/ZWZB9WAB.html', 'url': 'https://zingmp3.vn/bai-hat/Xa-Mai-Xa-Bao-Thy/ZWZB9WAB.html',
'only_matching': True, 'only_matching': True,
}] }]
IE_NAME = 'zingmp3' IE_NAME = 'zingmp3'
IE_DESC = 'mp3.zing.vn' IE_DESC = 'zingmp3.vn'
def _process_data(self, data): def _process_data(self, data, song_id, type_url):
return self._extract_item(data, True) return self._extract_item(data, song_id, type_url, True)
class ZingMp3AlbumIE(ZingMp3BaseIE): class ZingMp3AlbumIE(ZingMp3BaseIE):
@ -139,7 +182,7 @@ class ZingMp3AlbumIE(ZingMp3BaseIE):
'id': 'ZWZBWDAF', 'id': 'ZWZBWDAF',
'title': 'Lâu Đài Tình Ái', 'title': 'Lâu Đài Tình Ái',
}, },
'playlist_count': 10, 'playlist_count': 9,
}, { }, {
'url': 'http://mp3.zing.vn/playlist/Duong-Hong-Loan-apollobee/IWCAACCB.html', 'url': 'http://mp3.zing.vn/playlist/Duong-Hong-Loan-apollobee/IWCAACCB.html',
'only_matching': True, 'only_matching': True,
@ -149,12 +192,12 @@ class ZingMp3AlbumIE(ZingMp3BaseIE):
}] }]
IE_NAME = 'zingmp3:album' IE_NAME = 'zingmp3:album'
def _process_data(self, data): def _process_data(self, data, song_id, type_url):
def entries(): def entries():
for item in (data.get('items') or []): for item in traverse_obj(data, ('song', 'items')) or []:
entry = self._extract_item(item, False) entry = self._extract_item(item, song_id, type_url, False)
if entry: if entry:
yield entry yield entry
info = data.get('info') or {}
return self.playlist_result( return self.playlist_result(entries(), traverse_obj(data, 'id', 'encodeId'),
entries(), info.get('id'), info.get('name') or info.get('title')) traverse_obj(data, 'name', 'title'))