]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/kuwo.py
[extractor] Fix bug in 617f658b7ec1193749848c1b7343acab125dbc46
[yt-dlp.git] / yt_dlp / extractor / kuwo.py
1 import re
2
3 from .common import InfoExtractor
4 from ..compat import compat_urlparse
5 from ..utils import (
6 get_element_by_id,
7 clean_html,
8 ExtractorError,
9 InAdvancePagedList,
10 remove_start,
11 )
12
13
14 class KuwoBaseIE(InfoExtractor):
15 _FORMATS = [
16 {'format': 'ape', 'ext': 'ape', 'preference': 100},
17 {'format': 'mp3-320', 'ext': 'mp3', 'br': '320kmp3', 'abr': 320, 'preference': 80},
18 {'format': 'mp3-192', 'ext': 'mp3', 'br': '192kmp3', 'abr': 192, 'preference': 70},
19 {'format': 'mp3-128', 'ext': 'mp3', 'br': '128kmp3', 'abr': 128, 'preference': 60},
20 {'format': 'wma', 'ext': 'wma', 'preference': 20},
21 {'format': 'aac', 'ext': 'aac', 'abr': 48, 'preference': 10}
22 ]
23
24 def _get_formats(self, song_id, tolerate_ip_deny=False):
25 formats = []
26 for file_format in self._FORMATS:
27 query = {
28 'format': file_format['ext'],
29 'br': file_format.get('br', ''),
30 'rid': 'MUSIC_%s' % song_id,
31 'type': 'convert_url',
32 'response': 'url'
33 }
34
35 song_url = self._download_webpage(
36 'http://antiserver.kuwo.cn/anti.s',
37 song_id, note='Download %s url info' % file_format['format'],
38 query=query, headers=self.geo_verification_headers(),
39 )
40
41 if song_url == 'IPDeny' and not tolerate_ip_deny:
42 raise ExtractorError('This song is blocked in this region', expected=True)
43
44 if song_url.startswith('http://') or song_url.startswith('https://'):
45 formats.append({
46 'url': song_url,
47 'format_id': file_format['format'],
48 'format': file_format['format'],
49 'quality': file_format['preference'],
50 'abr': file_format.get('abr'),
51 })
52
53 return formats
54
55
56 class KuwoIE(KuwoBaseIE):
57 IE_NAME = 'kuwo:song'
58 IE_DESC = '酷我音乐'
59 _VALID_URL = r'https?://(?:www\.)?kuwo\.cn/yinyue/(?P<id>\d+)'
60 _TESTS = [{
61 'url': 'http://www.kuwo.cn/yinyue/635632/',
62 'info_dict': {
63 'id': '635632',
64 'ext': 'ape',
65 'title': '爱我别走',
66 'creator': '张震岳',
67 'upload_date': '20080122',
68 'description': 'md5:ed13f58e3c3bf3f7fd9fbc4e5a7aa75c'
69 },
70 'skip': 'this song has been offline because of copyright issues',
71 }, {
72 'url': 'http://www.kuwo.cn/yinyue/6446136/',
73 'info_dict': {
74 'id': '6446136',
75 'ext': 'mp3',
76 'title': '心',
77 'description': 'md5:5d0e947b242c35dc0eb1d2fce9fbf02c',
78 'creator': 'IU',
79 'upload_date': '20150518',
80 },
81 'params': {
82 'format': 'mp3-320',
83 },
84 }, {
85 'url': 'http://www.kuwo.cn/yinyue/3197154?catalog=yueku2016',
86 'only_matching': True,
87 }]
88
89 def _real_extract(self, url):
90 song_id = self._match_id(url)
91 webpage, urlh = self._download_webpage_handle(
92 url, song_id, note='Download song detail info',
93 errnote='Unable to get song detail info')
94 if song_id not in urlh.geturl() or '对不起,该歌曲由于版权问题已被下线,将返回网站首页' in webpage:
95 raise ExtractorError('this song has been offline because of copyright issues', expected=True)
96
97 song_name = self._html_search_regex(
98 r'<p[^>]+id="lrcName">([^<]+)</p>', webpage, 'song name')
99 singer_name = remove_start(self._html_search_regex(
100 r'<a[^>]+href="http://www\.kuwo\.cn/artist/content\?name=([^"]+)">',
101 webpage, 'singer name', fatal=False), '歌手')
102 lrc_content = clean_html(get_element_by_id('lrcContent', webpage))
103 if lrc_content == '暂无': # indicates no lyrics
104 lrc_content = None
105
106 formats = self._get_formats(song_id)
107 self._sort_formats(formats)
108
109 album_id = self._html_search_regex(
110 r'<a[^>]+href="http://www\.kuwo\.cn/album/(\d+)/"',
111 webpage, 'album id', fatal=False)
112
113 publish_time = None
114 if album_id is not None:
115 album_info_page = self._download_webpage(
116 'http://www.kuwo.cn/album/%s/' % album_id, song_id,
117 note='Download album detail info',
118 errnote='Unable to get album detail info')
119
120 publish_time = self._html_search_regex(
121 r'发行时间:(\d{4}-\d{2}-\d{2})', album_info_page,
122 'publish time', fatal=False)
123 if publish_time:
124 publish_time = publish_time.replace('-', '')
125
126 return {
127 'id': song_id,
128 'title': song_name,
129 'creator': singer_name,
130 'upload_date': publish_time,
131 'description': lrc_content,
132 'formats': formats,
133 }
134
135
136 class KuwoAlbumIE(InfoExtractor):
137 IE_NAME = 'kuwo:album'
138 IE_DESC = '酷我音乐 - 专辑'
139 _VALID_URL = r'https?://(?:www\.)?kuwo\.cn/album/(?P<id>\d+?)/'
140 _TEST = {
141 'url': 'http://www.kuwo.cn/album/502294/',
142 'info_dict': {
143 'id': '502294',
144 'title': 'Made\xa0Series\xa0《M》',
145 'description': 'md5:d463f0d8a0ff3c3ea3d6ed7452a9483f',
146 },
147 'playlist_count': 2,
148 }
149
150 def _real_extract(self, url):
151 album_id = self._match_id(url)
152
153 webpage = self._download_webpage(
154 url, album_id, note='Download album info',
155 errnote='Unable to get album info')
156
157 album_name = self._html_search_regex(
158 r'<div[^>]+class="comm"[^<]+<h1[^>]+title="([^"]+)"', webpage,
159 'album name')
160 album_intro = remove_start(
161 clean_html(get_element_by_id('intro', webpage)),
162 '%s简介:' % album_name)
163
164 entries = [
165 self.url_result(song_url, 'Kuwo') for song_url in re.findall(
166 r'<p[^>]+class="listen"><a[^>]+href="(http://www\.kuwo\.cn/yinyue/\d+/)"',
167 webpage)
168 ]
169 return self.playlist_result(entries, album_id, album_name, album_intro)
170
171
172 class KuwoChartIE(InfoExtractor):
173 IE_NAME = 'kuwo:chart'
174 IE_DESC = '酷我音乐 - 排行榜'
175 _VALID_URL = r'https?://yinyue\.kuwo\.cn/billboard_(?P<id>[^.]+).htm'
176 _TEST = {
177 'url': 'http://yinyue.kuwo.cn/billboard_香港中文龙虎榜.htm',
178 'info_dict': {
179 'id': '香港中文龙虎榜',
180 },
181 'playlist_mincount': 7,
182 }
183
184 def _real_extract(self, url):
185 chart_id = self._match_id(url)
186 webpage = self._download_webpage(
187 url, chart_id, note='Download chart info',
188 errnote='Unable to get chart info')
189
190 entries = [
191 self.url_result(song_url, 'Kuwo') for song_url in re.findall(
192 r'<a[^>]+href="(http://www\.kuwo\.cn/yinyue/\d+)', webpage)
193 ]
194 return self.playlist_result(entries, chart_id)
195
196
197 class KuwoSingerIE(InfoExtractor):
198 IE_NAME = 'kuwo:singer'
199 IE_DESC = '酷我音乐 - 歌手'
200 _VALID_URL = r'https?://(?:www\.)?kuwo\.cn/mingxing/(?P<id>[^/]+)'
201 _TESTS = [{
202 'url': 'http://www.kuwo.cn/mingxing/bruno+mars/',
203 'info_dict': {
204 'id': 'bruno+mars',
205 'title': 'Bruno\xa0Mars',
206 },
207 'playlist_mincount': 329,
208 }, {
209 'url': 'http://www.kuwo.cn/mingxing/Ali/music.htm',
210 'info_dict': {
211 'id': 'Ali',
212 'title': 'Ali',
213 },
214 'playlist_mincount': 95,
215 'skip': 'Regularly stalls travis build', # See https://travis-ci.org/ytdl-org/youtube-dl/jobs/78878540
216 }]
217
218 PAGE_SIZE = 15
219
220 def _real_extract(self, url):
221 singer_id = self._match_id(url)
222 webpage = self._download_webpage(
223 url, singer_id, note='Download singer info',
224 errnote='Unable to get singer info')
225
226 singer_name = self._html_search_regex(
227 r'<h1>([^<]+)</h1>', webpage, 'singer name')
228
229 artist_id = self._html_search_regex(
230 r'data-artistid="(\d+)"', webpage, 'artist id')
231
232 page_count = int(self._html_search_regex(
233 r'data-page="(\d+)"', webpage, 'page count'))
234
235 def page_func(page_num):
236 webpage = self._download_webpage(
237 'http://www.kuwo.cn/artist/contentMusicsAjax',
238 singer_id, note='Download song list page #%d' % (page_num + 1),
239 errnote='Unable to get song list page #%d' % (page_num + 1),
240 query={'artistId': artist_id, 'pn': page_num, 'rn': self.PAGE_SIZE})
241
242 return [
243 self.url_result(compat_urlparse.urljoin(url, song_url), 'Kuwo')
244 for song_url in re.findall(
245 r'<div[^>]+class="name"><a[^>]+href="(/yinyue/\d+)',
246 webpage)
247 ]
248
249 entries = InAdvancePagedList(page_func, page_count, self.PAGE_SIZE)
250
251 return self.playlist_result(entries, singer_id, singer_name)
252
253
254 class KuwoCategoryIE(InfoExtractor):
255 IE_NAME = 'kuwo:category'
256 IE_DESC = '酷我音乐 - 分类'
257 _VALID_URL = r'https?://yinyue\.kuwo\.cn/yy/cinfo_(?P<id>\d+?).htm'
258 _TEST = {
259 'url': 'http://yinyue.kuwo.cn/yy/cinfo_86375.htm',
260 'info_dict': {
261 'id': '86375',
262 'title': '八十年代精选',
263 'description': '这些都是属于八十年代的回忆!',
264 },
265 'playlist_mincount': 24,
266 }
267
268 def _real_extract(self, url):
269 category_id = self._match_id(url)
270 webpage = self._download_webpage(
271 url, category_id, note='Download category info',
272 errnote='Unable to get category info')
273
274 category_name = self._html_search_regex(
275 r'<h1[^>]+title="([^<>]+?)">[^<>]+?</h1>', webpage, 'category name')
276
277 category_desc = remove_start(
278 get_element_by_id('intro', webpage).strip(),
279 '%s简介:' % category_name)
280 if category_desc == '暂无':
281 category_desc = None
282
283 jsonm = self._parse_json(self._html_search_regex(
284 r'var\s+jsonm\s*=\s*([^;]+);', webpage, 'category songs'), category_id)
285
286 entries = [
287 self.url_result('http://www.kuwo.cn/yinyue/%s/' % song['musicrid'], 'Kuwo')
288 for song in jsonm['musiclist']
289 ]
290 return self.playlist_result(entries, category_id, category_name, category_desc)
291
292
293 class KuwoMvIE(KuwoBaseIE):
294 IE_NAME = 'kuwo:mv'
295 IE_DESC = '酷我音乐 - MV'
296 _VALID_URL = r'https?://(?:www\.)?kuwo\.cn/mv/(?P<id>\d+?)/'
297 _TEST = {
298 'url': 'http://www.kuwo.cn/mv/6480076/',
299 'info_dict': {
300 'id': '6480076',
301 'ext': 'mp4',
302 'title': 'My HouseMV',
303 'creator': '2PM',
304 },
305 # In this video, music URLs (anti.s) are blocked outside China and
306 # USA, while the MV URL (mvurl) is available globally, so force the MV
307 # URL for consistent results in different countries
308 'params': {
309 'format': 'mv',
310 },
311 }
312 _FORMATS = KuwoBaseIE._FORMATS + [
313 {'format': 'mkv', 'ext': 'mkv', 'preference': 250},
314 {'format': 'mp4', 'ext': 'mp4', 'preference': 200},
315 ]
316
317 def _real_extract(self, url):
318 song_id = self._match_id(url)
319 webpage = self._download_webpage(
320 url, song_id, note='Download mv detail info: %s' % song_id,
321 errnote='Unable to get mv detail info: %s' % song_id)
322
323 mobj = re.search(
324 r'<h1[^>]+title="(?P<song>[^"]+)">[^<]+<span[^>]+title="(?P<singer>[^"]+)"',
325 webpage)
326 if mobj:
327 song_name = mobj.group('song')
328 singer_name = mobj.group('singer')
329 else:
330 raise ExtractorError('Unable to find song or singer names')
331
332 formats = self._get_formats(song_id, tolerate_ip_deny=True)
333
334 mv_url = self._download_webpage(
335 'http://www.kuwo.cn/yy/st/mvurl?rid=MUSIC_%s' % song_id,
336 song_id, note='Download %s MV URL' % song_id)
337 formats.append({
338 'url': mv_url,
339 'format_id': 'mv',
340 })
341
342 self._sort_formats(formats)
343
344 return {
345 'id': song_id,
346 'title': song_name,
347 'creator': singer_name,
348 'formats': formats,
349 }