]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/kuwo.py
[extractor] Deprecate `_sort_formats`
[yt-dlp.git] / yt_dlp / extractor / kuwo.py
CommitLineData
8f73e89c 1import re
8f73e89c 2
3from .common import InfoExtractor
b2bd968f 4from ..compat import compat_urlparse
8f73e89c 5from ..utils import (
6 get_element_by_id,
7 clean_html,
8 ExtractorError,
daef04a4 9 InAdvancePagedList,
a31e3e7d 10 remove_start,
8f73e89c 11)
12
13
a9684c0d 14class KuwoBaseIE(InfoExtractor):
8f73e89c 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
3ff8279e 24 def _get_formats(self, song_id, tolerate_ip_deny=False):
8f73e89c 25 formats = []
26 for file_format in self._FORMATS:
e621a344
YCH
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
8f73e89c 35 song_url = self._download_webpage(
e621a344 36 'http://antiserver.kuwo.cn/anti.s',
cf2c5fda 37 song_id, note='Download %s url info' % file_format['format'],
38cce791 38 query=query, headers=self.geo_verification_headers(),
8f73e89c 39 )
58be9220 40
3ff8279e 41 if song_url == 'IPDeny' and not tolerate_ip_deny:
58be9220
YCH
42 raise ExtractorError('This song is blocked in this region', expected=True)
43
8f73e89c 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'],
f983b875 49 'quality': file_format['preference'],
8f73e89c 50 'abr': file_format.get('abr'),
51 })
3ff8279e 52
8f73e89c 53 return formats
54
a9684c0d
YCH
55
56class KuwoIE(KuwoBaseIE):
57 IE_NAME = 'kuwo:song'
0f08d7f8 58 IE_DESC = '酷我音乐'
92519402 59 _VALID_URL = r'https?://(?:www\.)?kuwo\.cn/yinyue/(?P<id>\d+)'
a9684c0d
YCH
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 },
c44c7895 70 'skip': 'this song has been offline because of copyright issues',
a9684c0d
YCH
71 }, {
72 'url': 'http://www.kuwo.cn/yinyue/6446136/',
73 'info_dict': {
74 'id': '6446136',
75 'ext': 'mp3',
76 'title': '心',
a82398bd 77 'description': 'md5:5d0e947b242c35dc0eb1d2fce9fbf02c',
a9684c0d
YCH
78 'creator': 'IU',
79 'upload_date': '20150518',
80 },
81 'params': {
6db354a9 82 'format': 'mp3-320',
a9684c0d 83 },
daef04a4
YCH
84 }, {
85 'url': 'http://www.kuwo.cn/yinyue/3197154?catalog=yueku2016',
86 'only_matching': True,
a9684c0d
YCH
87 }]
88
8f73e89c 89 def _real_extract(self, url):
90 song_id = self._match_id(url)
961516bf 91 webpage, urlh = self._download_webpage_handle(
8f73e89c 92 url, song_id, note='Download song detail info',
93 errnote='Unable to get song detail info')
961516bf 94 if song_id not in urlh.geturl() or '对不起,该歌曲由于版权问题已被下线,将返回网站首页' in webpage:
71936506 95 raise ExtractorError('this song has been offline because of copyright issues', expected=True)
8f73e89c 96
97 song_name = self._html_search_regex(
a82398bd
YCH
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), '歌手')
cf2c5fda 102 lrc_content = clean_html(get_element_by_id('lrcContent', webpage))
8f73e89c 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(
a82398bd 109 r'<a[^>]+href="http://www\.kuwo\.cn/album/(\d+)/"',
094790d2 110 webpage, 'album id', fatal=False)
8f73e89c 111
112 publish_time = None
113 if album_id is not None:
114 album_info_page = self._download_webpage(
cf2c5fda 115 'http://www.kuwo.cn/album/%s/' % album_id, song_id,
8f73e89c 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,
094790d2 121 'publish time', fatal=False)
8f73e89c 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
135class KuwoAlbumIE(InfoExtractor):
136 IE_NAME = 'kuwo:album'
edd66be5 137 IE_DESC = '酷我音乐 - 专辑'
92519402 138 _VALID_URL = r'https?://(?:www\.)?kuwo\.cn/album/(?P<id>\d+?)/'
8f73e89c 139 _TEST = {
140 'url': 'http://www.kuwo.cn/album/502294/',
141 'info_dict': {
142 'id': '502294',
7aab3696
YCH
143 'title': 'Made\xa0Series\xa0《M》',
144 'description': 'md5:d463f0d8a0ff3c3ea3d6ed7452a9483f',
8f73e89c 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(
a31e3e7d
YCH
157 r'<div[^>]+class="comm"[^<]+<h1[^>]+title="([^"]+)"', webpage,
158 'album name')
159 album_intro = remove_start(
cf2c5fda 160 clean_html(get_element_by_id('intro', webpage)),
a31e3e7d 161 '%s简介:' % album_name)
8f73e89c 162
163 entries = [
d3b89088
YCH
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+/)"',
8f73e89c 166 webpage)
167 ]
168 return self.playlist_result(entries, album_id, album_name, album_intro)
169
170
171class KuwoChartIE(InfoExtractor):
172 IE_NAME = 'kuwo:chart'
edd66be5 173 IE_DESC = '酷我音乐 - 排行榜'
5886b38d 174 _VALID_URL = r'https?://yinyue\.kuwo\.cn/billboard_(?P<id>[^.]+).htm'
8f73e89c 175 _TEST = {
176 'url': 'http://yinyue.kuwo.cn/billboard_香港中文龙虎榜.htm',
177 'info_dict': {
178 'id': '香港中文龙虎榜',
8f73e89c 179 },
6db354a9 180 'playlist_mincount': 7,
8f73e89c 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
8f73e89c 189 entries = [
d3b89088 190 self.url_result(song_url, 'Kuwo') for song_url in re.findall(
daef04a4 191 r'<a[^>]+href="(http://www\.kuwo\.cn/yinyue/\d+)', webpage)
8f73e89c 192 ]
daef04a4 193 return self.playlist_result(entries, chart_id)
8f73e89c 194
195
196class KuwoSingerIE(InfoExtractor):
197 IE_NAME = 'kuwo:singer'
edd66be5 198 IE_DESC = '酷我音乐 - 歌手'
92519402 199 _VALID_URL = r'https?://(?:www\.)?kuwo\.cn/mingxing/(?P<id>[^/]+)'
1633491b 200 _TESTS = [{
8f73e89c 201 'url': 'http://www.kuwo.cn/mingxing/bruno+mars/',
202 'info_dict': {
203 'id': 'bruno+mars',
7aab3696 204 'title': 'Bruno\xa0Mars',
8f73e89c 205 },
daef04a4 206 'playlist_mincount': 329,
1633491b 207 }, {
8f73e89c 208 'url': 'http://www.kuwo.cn/mingxing/Ali/music.htm',
209 'info_dict': {
210 'id': 'Ali',
1633491b 211 'title': 'Ali',
8f73e89c 212 },
213 'playlist_mincount': 95,
067aa17e 214 'skip': 'Regularly stalls travis build', # See https://travis-ci.org/ytdl-org/youtube-dl/jobs/78878540
1633491b 215 }]
8f73e89c 216
daef04a4
YCH
217 PAGE_SIZE = 15
218
8f73e89c 219 def _real_extract(self, url):
220 singer_id = self._match_id(url)
1633491b 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(
daef04a4
YCH
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'))
8f73e89c 233
daef04a4 234 def page_func(page_num):
8f73e89c 235 webpage = self._download_webpage(
daef04a4
YCH
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})
8f73e89c 240
daef04a4 241 return [
b2bd968f
YCH
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+)',
8f73e89c 245 webpage)
daef04a4 246 ]
1633491b 247
daef04a4 248 entries = InAdvancePagedList(page_func, page_count, self.PAGE_SIZE)
8f73e89c 249
1633491b 250 return self.playlist_result(entries, singer_id, singer_name)
8f73e89c 251
252
253class KuwoCategoryIE(InfoExtractor):
254 IE_NAME = 'kuwo:category'
edd66be5 255 IE_DESC = '酷我音乐 - 分类'
5886b38d 256 _VALID_URL = r'https?://yinyue\.kuwo\.cn/yy/cinfo_(?P<id>\d+?).htm'
8f73e89c 257 _TEST = {
258 'url': 'http://yinyue.kuwo.cn/yy/cinfo_86375.htm',
259 'info_dict': {
260 'id': '86375',
261 'title': '八十年代精选',
7d08f607 262 'description': '这些都是属于八十年代的回忆!',
8f73e89c 263 },
03dd60ca 264 'playlist_mincount': 24,
8f73e89c 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(
a31e3e7d 274 r'<h1[^>]+title="([^<>]+?)">[^<>]+?</h1>', webpage, 'category name')
a34af8d0 275
a31e3e7d 276 category_desc = remove_start(
cf2c5fda 277 get_element_by_id('intro', webpage).strip(),
a31e3e7d 278 '%s简介:' % category_name)
a0a309b9
YCH
279 if category_desc == '暂无':
280 category_desc = None
a34af8d0 281
8f73e89c 282 jsonm = self._parse_json(self._html_search_regex(
a31e3e7d 283 r'var\s+jsonm\s*=\s*([^;]+);', webpage, 'category songs'), category_id)
8f73e89c 284
285 entries = [
d3b89088 286 self.url_result('http://www.kuwo.cn/yinyue/%s/' % song['musicrid'], 'Kuwo')
8f73e89c 287 for song in jsonm['musiclist']
288 ]
289 return self.playlist_result(entries, category_id, category_name, category_desc)
290
291
a9684c0d 292class KuwoMvIE(KuwoBaseIE):
8f73e89c 293 IE_NAME = 'kuwo:mv'
edd66be5 294 IE_DESC = '酷我音乐 - MV'
92519402 295 _VALID_URL = r'https?://(?:www\.)?kuwo\.cn/mv/(?P<id>\d+?)/'
a9684c0d 296 _TEST = {
8f73e89c 297 'url': 'http://www.kuwo.cn/mv/6480076/',
298 'info_dict': {
299 'id': '6480076',
3ff8279e
YCH
300 'ext': 'mp4',
301 'title': 'My HouseMV',
6db354a9 302 'creator': '2PM',
8f73e89c 303 },
3ff8279e
YCH
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 },
a9684c0d
YCH
310 }
311 _FORMATS = KuwoBaseIE._FORMATS + [
8f73e89c 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(
a31e3e7d 323 r'<h1[^>]+title="(?P<song>[^"]+)">[^<]+<span[^>]+title="(?P<singer>[^"]+)"',
8f73e89c 324 webpage)
325 if mobj:
326 song_name = mobj.group('song')
327 singer_name = mobj.group('singer')
328 else:
cf2c5fda 329 raise ExtractorError('Unable to find song or singer names')
8f73e89c 330
3ff8279e
YCH
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
8f73e89c 341 return {
342 'id': song_id,
343 'title': song_name,
344 'creator': singer_name,
345 'formats': formats,
346 }