From dd114e5e8e20ac7e49195a02cedb40c0a7773d2c Mon Sep 17 00:00:00 2001 From: Thinh Nguyen Date: Sat, 14 Jul 2018 13:06:52 -0400 Subject: [PATCH] [zingmp3] Update extractor --- youtube_dl/extractor/zingmp3.py | 108 ++++++++++++++++++-------------- 1 file changed, 62 insertions(+), 46 deletions(-) diff --git a/youtube_dl/extractor/zingmp3.py b/youtube_dl/extractor/zingmp3.py index adfdcaabf..86030cb12 100644 --- a/youtube_dl/extractor/zingmp3.py +++ b/youtube_dl/extractor/zingmp3.py @@ -23,11 +23,11 @@ class ZingMp3BaseInfoExtractor(InfoExtractor): expected=True) formats = [] - for quality, source_url in zip(item.get('qualities') or item.get('quality', []), item.get('source_list') or item.get('source', [])): + sources = item.get('source', {}) + for (quality, source_url) in sources.items(): if not source_url or source_url == 'require vip': continue - if not re.match(r'https?://', source_url): - source_url = '//' + source_url + source_url = self._proto_relative_url(source_url, 'http:') quality_num = int_or_none(quality) f = { @@ -46,36 +46,46 @@ class ZingMp3BaseInfoExtractor(InfoExtractor): }) formats.append(f) - cover = item.get('cover') + artist = None + try: + artist = item['artist']['name'] + except KeyError: + pass + + if page_type == 'album': + return { + 'id': item.get('id'), + 'track_id': item.get('id'), + 'artist': artist, + 'title': (item.get('name') or item.get('title')).strip(), + 'track': (item.get('name') or item.get('title')).strip(), + 'track_number': int_or_none(item.get('order')), + 'formats': formats, + 'thumbnail': item.get('thumbnail'), + } return { + 'id': item.get('id'), + 'artist': artist, 'title': (item.get('name') or item.get('title')).strip(), 'formats': formats, - 'thumbnail': 'http:/' + cover if cover else None, - 'artist': item.get('artist'), + 'thumbnail': item.get('thumbnail'), } def _extract_player_json(self, player_json_url, id, page_type, playlist_title=None): player_json = self._download_json(player_json_url, id, 'Downloading Player JSON') - items = player_json['data'] - if 'item' in items: - items = items['item'] - - if len(items) == 1: - # one single song - data = self._extract_item(items[0], page_type) - data['id'] = id - + if 'items' not in player_json['data']: + item = player_json['data'] + data = self._extract_item(item, page_type) return data + else: # playlist of songs entries = [] - - for i, item in enumerate(items, 1): + for i, item in enumerate(player_json['data']['items'], 1): entry = self._extract_item(item, page_type, fatal=False) if not entry: continue - entry['id'] = '%s-%d' % (id, i) entries.append(entry) return { @@ -88,36 +98,41 @@ class ZingMp3BaseInfoExtractor(InfoExtractor): class ZingMp3IE(ZingMp3BaseInfoExtractor): _VALID_URL = r'https?://mp3\.zing\.vn/(?:bai-hat|album|playlist|video-clip)/[^/]+/(?P\w+)\.html' - _TESTS = [{ - 'url': 'http://mp3.zing.vn/bai-hat/Xa-Mai-Xa-Bao-Thy/ZWZB9WAB.html', - 'md5': 'ead7ae13693b3205cbc89536a077daed', - 'info_dict': { - 'id': 'ZWZB9WAB', - 'title': 'Xa Mãi Xa', - 'ext': 'mp3', - 'thumbnail': r're:^https?://.*\.jpg$', + _TESTS = [ + { + 'url': 'http://mp3.zing.vn/bai-hat/Xa-Mai-Xa-Bao-Thy/ZWZB9WAB.html', + 'md5': 'ead7ae13693b3205cbc89536a077daed', + 'info_dict': { + 'id': 'ZWZB9WAB', + 'title': 'Xa Mãi Xa', + 'artist': 'Bảo Thy', + 'ext': 'mp3', + 'thumbnail': r're:^https?://.*\.jpg$', + }, }, - }, { - 'url': 'http://mp3.zing.vn/video-clip/Let-It-Go-Frozen-OST-Sungha-Jung/ZW6BAEA0.html', - 'md5': '870295a9cd8045c0e15663565902618d', - 'info_dict': { - 'id': 'ZW6BAEA0', - 'title': 'Let It Go (Frozen OST)', - 'ext': 'mp4', + { + 'url': 'http://mp3.zing.vn/video-clip/Let-It-Go-Frozen-OST-Sungha-Jung/ZW6BAEA0.html', + 'md5': '870295a9cd8045c0e15663565902618d', + 'info_dict': { + 'id': 'ZW6BAEA0', + 'title': 'Let It Go (Frozen OST)', + 'ext': 'mp4', + }, }, - }, { - 'url': 'http://mp3.zing.vn/album/Lau-Dai-Tinh-Ai-Bang-Kieu-Minh-Tuyet/ZWZBWDAF.html', - 'info_dict': { - '_type': 'playlist', - 'id': 'ZWZBWDAF', - 'title': 'Lâu Đài Tình Ái - Bằng Kiều,Minh Tuyết | Album 320 lossless', + { + 'url': 'https://mp3.zing.vn/album/Con-Trong-Ky-Niem-Le-Quyen/ZWZCO9UW.html', + 'info_dict': { + '_type': 'playlist', + 'id': 'ZWZCO9UW', + 'title': 'Còn Trong Kỷ Niệm - Lệ Quyên | Zing MP3', + }, + 'playlist_count': 9, }, - 'playlist_count': 10, - 'skip': 'removed at the request of the owner', - }, { - 'url': 'http://mp3.zing.vn/playlist/Duong-Hong-Loan-apollobee/IWCAACCB.html', - 'only_matching': True, - }] + { + 'url': 'http://mp3.zing.vn/playlist/Duong-Hong-Loan-apollobee/IWCAACCB.html', + 'only_matching': True, + }, + ] IE_NAME = 'zingmp3' IE_DESC = 'mp3.zing.vn' @@ -130,9 +145,10 @@ class ZingMp3IE(ZingMp3BaseInfoExtractor): r'data-xml="([^"]+)', r'&xmlURL=([^&]+)&' ], webpage, 'player xml url') + player_json_url = "https://mp3.zing.vn/xhr" + player_json_url playlist_title = None - page_type = self._search_regex(r'/(?:html5)?xml/([^/-]+)', player_json_url, 'page type') + page_type = self._search_regex(r'type=([\w]+)', player_json_url, 'page type') if page_type == 'video': player_json_url = update_url_query(player_json_url, {'format': 'json'}) else: