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