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