]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/kuwo.py
[extractor] Deprecate `_sort_formats`
[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
108 album_id = self._html_search_regex(
109 r'<a[^>]+href="http://www\.kuwo\.cn/album/(\d+)/"',
110 webpage, 'album id', fatal=False)
111
112 publish_time = None
113 if album_id is not None:
114 album_info_page = self._download_webpage(
115 'http://www.kuwo.cn/album/%s/' % album_id, song_id,
116 note='Download album detail info',
117 errnote='Unable to get album detail info')
118
119 publish_time = self._html_search_regex(
120 r'发行时间:(\d{4}-\d{2}-\d{2})', album_info_page,
121 'publish time', fatal=False)
122 if publish_time:
123 publish_time = publish_time.replace('-', '')
124
125 return {
126 'id': song_id,
127 'title': song_name,
128 'creator': singer_name,
129 'upload_date': publish_time,
130 'description': lrc_content,
131 'formats': formats,
132 }
133
134
135 class KuwoAlbumIE(InfoExtractor):
136 IE_NAME = 'kuwo:album'
137 IE_DESC = '酷我音乐 - 专辑'
138 _VALID_URL = r'https?://(?:www\.)?kuwo\.cn/album/(?P<id>\d+?)/'
139 _TEST = {
140 'url': 'http://www.kuwo.cn/album/502294/',
141 'info_dict': {
142 'id': '502294',
143 'title': 'Made\xa0Series\xa0《M》',
144 'description': 'md5:d463f0d8a0ff3c3ea3d6ed7452a9483f',
145 },
146 'playlist_count': 2,
147 }
148
149 def _real_extract(self, url):
150 album_id = self._match_id(url)
151
152 webpage = self._download_webpage(
153 url, album_id, note='Download album info',
154 errnote='Unable to get album info')
155
156 album_name = self._html_search_regex(
157 r'<div[^>]+class="comm"[^<]+<h1[^>]+title="([^"]+)"', webpage,
158 'album name')
159 album_intro = remove_start(
160 clean_html(get_element_by_id('intro', webpage)),
161 '%s简介:' % album_name)
162
163 entries = [
164 self.url_result(song_url, 'Kuwo') for song_url in re.findall(
165 r'<p[^>]+class="listen"><a[^>]+href="(http://www\.kuwo\.cn/yinyue/\d+/)"',
166 webpage)
167 ]
168 return self.playlist_result(entries, album_id, album_name, album_intro)
169
170
171 class KuwoChartIE(InfoExtractor):
172 IE_NAME = 'kuwo:chart'
173 IE_DESC = '酷我音乐 - 排行榜'
174 _VALID_URL = r'https?://yinyue\.kuwo\.cn/billboard_(?P<id>[^.]+).htm'
175 _TEST = {
176 'url': 'http://yinyue.kuwo.cn/billboard_香港中文龙虎榜.htm',
177 'info_dict': {
178 'id': '香港中文龙虎榜',
179 },
180 'playlist_mincount': 7,
181 }
182
183 def _real_extract(self, url):
184 chart_id = self._match_id(url)
185 webpage = self._download_webpage(
186 url, chart_id, note='Download chart info',
187 errnote='Unable to get chart info')
188
189 entries = [
190 self.url_result(song_url, 'Kuwo') for song_url in re.findall(
191 r'<a[^>]+href="(http://www\.kuwo\.cn/yinyue/\d+)', webpage)
192 ]
193 return self.playlist_result(entries, chart_id)
194
195
196 class KuwoSingerIE(InfoExtractor):
197 IE_NAME = 'kuwo:singer'
198 IE_DESC = '酷我音乐 - 歌手'
199 _VALID_URL = r'https?://(?:www\.)?kuwo\.cn/mingxing/(?P<id>[^/]+)'
200 _TESTS = [{
201 'url': 'http://www.kuwo.cn/mingxing/bruno+mars/',
202 'info_dict': {
203 'id': 'bruno+mars',
204 'title': 'Bruno\xa0Mars',
205 },
206 'playlist_mincount': 329,
207 }, {
208 'url': 'http://www.kuwo.cn/mingxing/Ali/music.htm',
209 'info_dict': {
210 'id': 'Ali',
211 'title': 'Ali',
212 },
213 'playlist_mincount': 95,
214 'skip': 'Regularly stalls travis build', # See https://travis-ci.org/ytdl-org/youtube-dl/jobs/78878540
215 }]
216
217 PAGE_SIZE = 15
218
219 def _real_extract(self, url):
220 singer_id = self._match_id(url)
221 webpage = self._download_webpage(
222 url, singer_id, note='Download singer info',
223 errnote='Unable to get singer info')
224
225 singer_name = self._html_search_regex(
226 r'<h1>([^<]+)</h1>', webpage, 'singer name')
227
228 artist_id = self._html_search_regex(
229 r'data-artistid="(\d+)"', webpage, 'artist id')
230
231 page_count = int(self._html_search_regex(
232 r'data-page="(\d+)"', webpage, 'page count'))
233
234 def page_func(page_num):
235 webpage = self._download_webpage(
236 'http://www.kuwo.cn/artist/contentMusicsAjax',
237 singer_id, note='Download song list page #%d' % (page_num + 1),
238 errnote='Unable to get song list page #%d' % (page_num + 1),
239 query={'artistId': artist_id, 'pn': page_num, 'rn': self.PAGE_SIZE})
240
241 return [
242 self.url_result(compat_urlparse.urljoin(url, song_url), 'Kuwo')
243 for song_url in re.findall(
244 r'<div[^>]+class="name"><a[^>]+href="(/yinyue/\d+)',
245 webpage)
246 ]
247
248 entries = InAdvancePagedList(page_func, page_count, self.PAGE_SIZE)
249
250 return self.playlist_result(entries, singer_id, singer_name)
251
252
253 class KuwoCategoryIE(InfoExtractor):
254 IE_NAME = 'kuwo:category'
255 IE_DESC = '酷我音乐 - 分类'
256 _VALID_URL = r'https?://yinyue\.kuwo\.cn/yy/cinfo_(?P<id>\d+?).htm'
257 _TEST = {
258 'url': 'http://yinyue.kuwo.cn/yy/cinfo_86375.htm',
259 'info_dict': {
260 'id': '86375',
261 'title': '八十年代精选',
262 'description': '这些都是属于八十年代的回忆!',
263 },
264 'playlist_mincount': 24,
265 }
266
267 def _real_extract(self, url):
268 category_id = self._match_id(url)
269 webpage = self._download_webpage(
270 url, category_id, note='Download category info',
271 errnote='Unable to get category info')
272
273 category_name = self._html_search_regex(
274 r'<h1[^>]+title="([^<>]+?)">[^<>]+?</h1>', webpage, 'category name')
275
276 category_desc = remove_start(
277 get_element_by_id('intro', webpage).strip(),
278 '%s简介:' % category_name)
279 if category_desc == '暂无':
280 category_desc = None
281
282 jsonm = self._parse_json(self._html_search_regex(
283 r'var\s+jsonm\s*=\s*([^;]+);', webpage, 'category songs'), category_id)
284
285 entries = [
286 self.url_result('http://www.kuwo.cn/yinyue/%s/' % song['musicrid'], 'Kuwo')
287 for song in jsonm['musiclist']
288 ]
289 return self.playlist_result(entries, category_id, category_name, category_desc)
290
291
292 class KuwoMvIE(KuwoBaseIE):
293 IE_NAME = 'kuwo:mv'
294 IE_DESC = '酷我音乐 - MV'
295 _VALID_URL = r'https?://(?:www\.)?kuwo\.cn/mv/(?P<id>\d+?)/'
296 _TEST = {
297 'url': 'http://www.kuwo.cn/mv/6480076/',
298 'info_dict': {
299 'id': '6480076',
300 'ext': 'mp4',
301 'title': 'My HouseMV',
302 'creator': '2PM',
303 },
304 # In this video, music URLs (anti.s) are blocked outside China and
305 # USA, while the MV URL (mvurl) is available globally, so force the MV
306 # URL for consistent results in different countries
307 'params': {
308 'format': 'mv',
309 },
310 }
311 _FORMATS = KuwoBaseIE._FORMATS + [
312 {'format': 'mkv', 'ext': 'mkv', 'preference': 250},
313 {'format': 'mp4', 'ext': 'mp4', 'preference': 200},
314 ]
315
316 def _real_extract(self, url):
317 song_id = self._match_id(url)
318 webpage = self._download_webpage(
319 url, song_id, note='Download mv detail info: %s' % song_id,
320 errnote='Unable to get mv detail info: %s' % song_id)
321
322 mobj = re.search(
323 r'<h1[^>]+title="(?P<song>[^"]+)">[^<]+<span[^>]+title="(?P<singer>[^"]+)"',
324 webpage)
325 if mobj:
326 song_name = mobj.group('song')
327 singer_name = mobj.group('singer')
328 else:
329 raise ExtractorError('Unable to find song or singer names')
330
331 formats = self._get_formats(song_id, tolerate_ip_deny=True)
332
333 mv_url = self._download_webpage(
334 'http://www.kuwo.cn/yy/st/mvurl?rid=MUSIC_%s' % song_id,
335 song_id, note='Download %s MV URL' % song_id)
336 formats.append({
337 'url': mv_url,
338 'format_id': 'mv',
339 })
340
341 return {
342 'id': song_id,
343 'title': song_name,
344 'creator': singer_name,
345 'formats': formats,
346 }