]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/zingmp3.py
New upstream version 2020.06.16.1
[youtubedl] / youtube_dl / extractor / zingmp3.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8 ExtractorError,
9 int_or_none,
10 update_url_query,
11 )
12
13
14 class ZingMp3BaseInfoExtractor(InfoExtractor):
15
16 def _extract_item(self, item, page_type, fatal=True):
17 error_message = item.get('msg')
18 if error_message:
19 if not fatal:
20 return
21 raise ExtractorError(
22 '%s returned error: %s' % (self.IE_NAME, error_message),
23 expected=True)
24
25 formats = []
26 for quality, source_url in zip(item.get('qualities') or item.get('quality', []), item.get('source_list') or item.get('source', [])):
27 if not source_url or source_url == 'require vip':
28 continue
29 if not re.match(r'https?://', source_url):
30 source_url = '//' + source_url
31 source_url = self._proto_relative_url(source_url, 'http:')
32 quality_num = int_or_none(quality)
33 f = {
34 'format_id': quality,
35 'url': source_url,
36 }
37 if page_type == 'video':
38 f.update({
39 'height': quality_num,
40 'ext': 'mp4',
41 })
42 else:
43 f.update({
44 'abr': quality_num,
45 'ext': 'mp3',
46 })
47 formats.append(f)
48
49 cover = item.get('cover')
50
51 return {
52 'title': (item.get('name') or item.get('title')).strip(),
53 'formats': formats,
54 'thumbnail': 'http:/' + cover if cover else None,
55 'artist': item.get('artist'),
56 }
57
58 def _extract_player_json(self, player_json_url, id, page_type, playlist_title=None):
59 player_json = self._download_json(player_json_url, id, 'Downloading Player JSON')
60 items = player_json['data']
61 if 'item' in items:
62 items = items['item']
63
64 if len(items) == 1:
65 # one single song
66 data = self._extract_item(items[0], page_type)
67 data['id'] = id
68
69 return data
70 else:
71 # playlist of songs
72 entries = []
73
74 for i, item in enumerate(items, 1):
75 entry = self._extract_item(item, page_type, fatal=False)
76 if not entry:
77 continue
78 entry['id'] = '%s-%d' % (id, i)
79 entries.append(entry)
80
81 return {
82 '_type': 'playlist',
83 'id': id,
84 'title': playlist_title,
85 'entries': entries,
86 }
87
88
89 class ZingMp3IE(ZingMp3BaseInfoExtractor):
90 _VALID_URL = r'https?://mp3\.zing\.vn/(?:bai-hat|album|playlist|video-clip)/[^/]+/(?P<id>\w+)\.html'
91 _TESTS = [{
92 'url': 'http://mp3.zing.vn/bai-hat/Xa-Mai-Xa-Bao-Thy/ZWZB9WAB.html',
93 'md5': 'ead7ae13693b3205cbc89536a077daed',
94 'info_dict': {
95 'id': 'ZWZB9WAB',
96 'title': 'Xa Mãi Xa',
97 'ext': 'mp3',
98 'thumbnail': r're:^https?://.*\.jpg$',
99 },
100 }, {
101 'url': 'http://mp3.zing.vn/video-clip/Let-It-Go-Frozen-OST-Sungha-Jung/ZW6BAEA0.html',
102 'md5': '870295a9cd8045c0e15663565902618d',
103 'info_dict': {
104 'id': 'ZW6BAEA0',
105 'title': 'Let It Go (Frozen OST)',
106 'ext': 'mp4',
107 },
108 }, {
109 'url': 'http://mp3.zing.vn/album/Lau-Dai-Tinh-Ai-Bang-Kieu-Minh-Tuyet/ZWZBWDAF.html',
110 'info_dict': {
111 '_type': 'playlist',
112 'id': 'ZWZBWDAF',
113 'title': 'Lâu Đài Tình Ái - Bằng Kiều,Minh Tuyết | Album 320 lossless',
114 },
115 'playlist_count': 10,
116 'skip': 'removed at the request of the owner',
117 }, {
118 'url': 'http://mp3.zing.vn/playlist/Duong-Hong-Loan-apollobee/IWCAACCB.html',
119 'only_matching': True,
120 }]
121 IE_NAME = 'zingmp3'
122 IE_DESC = 'mp3.zing.vn'
123
124 def _real_extract(self, url):
125 page_id = self._match_id(url)
126
127 webpage = self._download_webpage(url, page_id)
128
129 player_json_url = self._search_regex([
130 r'data-xml="([^"]+)',
131 r'&amp;xmlURL=([^&]+)&'
132 ], webpage, 'player xml url')
133
134 playlist_title = None
135 page_type = self._search_regex(r'/(?:html5)?xml/([^/-]+)', player_json_url, 'page type')
136 if page_type == 'video':
137 player_json_url = update_url_query(player_json_url, {'format': 'json'})
138 else:
139 player_json_url = player_json_url.replace('/xml/', '/html5xml/')
140 if page_type == 'album':
141 playlist_title = self._og_search_title(webpage)
142
143 return self._extract_player_json(player_json_url, page_id, page_type, playlist_title)