]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/kuwo.py
Merge pull request #8754 from remitamine/5min
[yt-dlp.git] / youtube_dl / extractor / kuwo.py
CommitLineData
8f73e89c 1# coding: utf-8
2from __future__ import unicode_literals
3
4import re
5import itertools
6
7from .common import InfoExtractor
8from ..utils import (
9 get_element_by_id,
10 clean_html,
11 ExtractorError,
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
26 def _get_formats(self, song_id):
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
YCH
34
35 if song_url == 'IPDeny':
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 })
46 self._sort_formats(formats)
47 return formats
48
a9684c0d
YCH
49
50class KuwoIE(KuwoBaseIE):
51 IE_NAME = 'kuwo:song'
0f08d7f8 52 IE_DESC = '酷我音乐'
9f01c1a8 53 _VALID_URL = r'http://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 },
78 }]
79
8f73e89c 80 def _real_extract(self, url):
81 song_id = self._match_id(url)
82 webpage = self._download_webpage(
83 url, song_id, note='Download song detail info',
84 errnote='Unable to get song detail info')
5db34f68 85 if '对不起,该歌曲由于版权问题已被下线,将返回网站首页' in webpage:
71936506 86 raise ExtractorError('this song has been offline because of copyright issues', expected=True)
8f73e89c 87
88 song_name = self._html_search_regex(
5db34f68 89 r'(?s)class="(?:[^"\s]+\s+)*title(?:\s+[^"\s]+)*".*?<h1[^>]+title="([^"]+)"', webpage, 'song name')
8f73e89c 90 singer_name = self._html_search_regex(
a31e3e7d 91 r'<div[^>]+class="s_img">\s*<a[^>]+title="([^>]+)"',
094790d2 92 webpage, 'singer name', fatal=False)
cf2c5fda 93 lrc_content = clean_html(get_element_by_id('lrcContent', webpage))
8f73e89c 94 if lrc_content == '暂无': # indicates no lyrics
95 lrc_content = None
96
97 formats = self._get_formats(song_id)
98
99 album_id = self._html_search_regex(
a31e3e7d 100 r'<p[^>]+class="album"[^<]+<a[^>]+href="http://www\.kuwo\.cn/album/(\d+)/"',
094790d2 101 webpage, 'album id', fatal=False)
8f73e89c 102
103 publish_time = None
104 if album_id is not None:
105 album_info_page = self._download_webpage(
cf2c5fda 106 'http://www.kuwo.cn/album/%s/' % album_id, song_id,
8f73e89c 107 note='Download album detail info',
108 errnote='Unable to get album detail info')
109
110 publish_time = self._html_search_regex(
111 r'发行时间:(\d{4}-\d{2}-\d{2})', album_info_page,
094790d2 112 'publish time', fatal=False)
8f73e89c 113 if publish_time:
114 publish_time = publish_time.replace('-', '')
115
116 return {
117 'id': song_id,
118 'title': song_name,
119 'creator': singer_name,
120 'upload_date': publish_time,
121 'description': lrc_content,
122 'formats': formats,
123 }
124
125
126class KuwoAlbumIE(InfoExtractor):
127 IE_NAME = 'kuwo:album'
edd66be5 128 IE_DESC = '酷我音乐 - 专辑'
9f01c1a8 129 _VALID_URL = r'http://www\.kuwo\.cn/album/(?P<id>\d+?)/'
8f73e89c 130 _TEST = {
131 'url': 'http://www.kuwo.cn/album/502294/',
132 'info_dict': {
133 'id': '502294',
134 'title': 'M',
135 'description': 'md5:6a7235a84cc6400ec3b38a7bdaf1d60c',
136 },
137 'playlist_count': 2,
138 }
139
140 def _real_extract(self, url):
141 album_id = self._match_id(url)
142
143 webpage = self._download_webpage(
144 url, album_id, note='Download album info',
145 errnote='Unable to get album info')
146
147 album_name = self._html_search_regex(
a31e3e7d
YCH
148 r'<div[^>]+class="comm"[^<]+<h1[^>]+title="([^"]+)"', webpage,
149 'album name')
150 album_intro = remove_start(
cf2c5fda 151 clean_html(get_element_by_id('intro', webpage)),
a31e3e7d 152 '%s简介:' % album_name)
8f73e89c 153
154 entries = [
d3b89088
YCH
155 self.url_result(song_url, 'Kuwo') for song_url in re.findall(
156 r'<p[^>]+class="listen"><a[^>]+href="(http://www\.kuwo\.cn/yinyue/\d+/)"',
8f73e89c 157 webpage)
158 ]
159 return self.playlist_result(entries, album_id, album_name, album_intro)
160
161
162class KuwoChartIE(InfoExtractor):
163 IE_NAME = 'kuwo:chart'
edd66be5 164 IE_DESC = '酷我音乐 - 排行榜'
a31e3e7d 165 _VALID_URL = r'http://yinyue\.kuwo\.cn/billboard_(?P<id>[^.]+).htm'
8f73e89c 166 _TEST = {
167 'url': 'http://yinyue.kuwo.cn/billboard_香港中文龙虎榜.htm',
168 'info_dict': {
169 'id': '香港中文龙虎榜',
170 'title': '香港中文龙虎榜',
9f01c1a8 171 'description': 're:\d{4}第\d{2}期',
8f73e89c 172 },
173 'playlist_mincount': 10,
174 }
175
176 def _real_extract(self, url):
177 chart_id = self._match_id(url)
178 webpage = self._download_webpage(
179 url, chart_id, note='Download chart info',
180 errnote='Unable to get chart info')
181
182 chart_name = self._html_search_regex(
a31e3e7d 183 r'<h1[^>]+class="unDis">([^<]+)</h1>', webpage, 'chart name')
8f73e89c 184
185 chart_desc = self._html_search_regex(
a31e3e7d 186 r'<p[^>]+class="tabDef">(\d{4}第\d{2}期)</p>', webpage, 'chart desc')
8f73e89c 187
188 entries = [
d3b89088
YCH
189 self.url_result(song_url, 'Kuwo') for song_url in re.findall(
190 r'<a[^>]+href="(http://www\.kuwo\.cn/yinyue/\d+)/"', webpage)
8f73e89c 191 ]
192 return self.playlist_result(entries, chart_id, chart_name, chart_desc)
193
194
195class KuwoSingerIE(InfoExtractor):
196 IE_NAME = 'kuwo:singer'
edd66be5 197 IE_DESC = '酷我音乐 - 歌手'
1633491b 198 _VALID_URL = r'http://www\.kuwo\.cn/mingxing/(?P<id>[^/]+)'
199 _TESTS = [{
8f73e89c 200 'url': 'http://www.kuwo.cn/mingxing/bruno+mars/',
201 'info_dict': {
202 'id': 'bruno+mars',
203 'title': 'Bruno Mars',
204 },
205 'playlist_count': 10,
1633491b 206 }, {
8f73e89c 207 'url': 'http://www.kuwo.cn/mingxing/Ali/music.htm',
208 'info_dict': {
209 'id': 'Ali',
1633491b 210 'title': 'Ali',
8f73e89c 211 },
212 'playlist_mincount': 95,
12bbd32a 213 'skip': 'Regularly stalls travis build', # See https://travis-ci.org/rg3/youtube-dl/jobs/78878540
1633491b 214 }]
8f73e89c 215
216 def _real_extract(self, url):
217 singer_id = self._match_id(url)
1633491b 218 webpage = self._download_webpage(
219 url, singer_id, note='Download singer info',
220 errnote='Unable to get singer info')
221
222 singer_name = self._html_search_regex(
a31e3e7d 223 r'<div class="title clearfix">\s*<h1>([^<]+)<span', webpage, 'singer name'
1633491b 224 )
8f73e89c 225
8f73e89c 226 entries = []
9f01c1a8 227 first_page_only = False if re.search(r'/music(?:_\d+)?\.htm', url) else True
8f73e89c 228 for page_num in itertools.count(1):
229 webpage = self._download_webpage(
230 'http://www.kuwo.cn/mingxing/%s/music_%d.htm' % (singer_id, page_num),
231 singer_id, note='Download song list page #%d' % page_num,
232 errnote='Unable to get song list page #%d' % page_num)
233
8f73e89c 234 entries.extend([
d3b89088
YCH
235 self.url_result(song_url, 'Kuwo') for song_url in re.findall(
236 r'<p[^>]+class="m_name"><a[^>]+href="(http://www\.kuwo\.cn/yinyue/\d+)/',
8f73e89c 237 webpage)
1633491b 238 ][:10 if first_page_only else None])
239
a31e3e7d 240 if first_page_only or not re.search(r'<a[^>]+href="[^"]+">下一页</a>', webpage):
8f73e89c 241 break
242
1633491b 243 return self.playlist_result(entries, singer_id, singer_name)
8f73e89c 244
245
246class KuwoCategoryIE(InfoExtractor):
247 IE_NAME = 'kuwo:category'
edd66be5 248 IE_DESC = '酷我音乐 - 分类'
9f01c1a8 249 _VALID_URL = r'http://yinyue\.kuwo\.cn/yy/cinfo_(?P<id>\d+?).htm'
8f73e89c 250 _TEST = {
251 'url': 'http://yinyue.kuwo.cn/yy/cinfo_86375.htm',
252 'info_dict': {
253 'id': '86375',
254 'title': '八十年代精选',
255 'description': '这些都是属于八十年代的回忆!',
256 },
257 'playlist_count': 30,
258 }
259
260 def _real_extract(self, url):
261 category_id = self._match_id(url)
262 webpage = self._download_webpage(
263 url, category_id, note='Download category info',
264 errnote='Unable to get category info')
265
266 category_name = self._html_search_regex(
a31e3e7d 267 r'<h1[^>]+title="([^<>]+?)">[^<>]+?</h1>', webpage, 'category name')
a34af8d0 268
a31e3e7d 269 category_desc = remove_start(
cf2c5fda 270 get_element_by_id('intro', webpage).strip(),
a31e3e7d 271 '%s简介:' % category_name)
a34af8d0 272
8f73e89c 273 jsonm = self._parse_json(self._html_search_regex(
a31e3e7d 274 r'var\s+jsonm\s*=\s*([^;]+);', webpage, 'category songs'), category_id)
8f73e89c 275
276 entries = [
d3b89088 277 self.url_result('http://www.kuwo.cn/yinyue/%s/' % song['musicrid'], 'Kuwo')
8f73e89c 278 for song in jsonm['musiclist']
279 ]
280 return self.playlist_result(entries, category_id, category_name, category_desc)
281
282
a9684c0d 283class KuwoMvIE(KuwoBaseIE):
8f73e89c 284 IE_NAME = 'kuwo:mv'
edd66be5 285 IE_DESC = '酷我音乐 - MV'
9f01c1a8 286 _VALID_URL = r'http://www\.kuwo\.cn/mv/(?P<id>\d+?)/'
a9684c0d 287 _TEST = {
8f73e89c 288 'url': 'http://www.kuwo.cn/mv/6480076/',
289 'info_dict': {
290 'id': '6480076',
291 'ext': 'mkv',
292 'title': '我们家MV',
293 'creator': '2PM',
294 },
a9684c0d
YCH
295 }
296 _FORMATS = KuwoBaseIE._FORMATS + [
8f73e89c 297 {'format': 'mkv', 'ext': 'mkv', 'preference': 250},
298 {'format': 'mp4', 'ext': 'mp4', 'preference': 200},
299 ]
300
301 def _real_extract(self, url):
302 song_id = self._match_id(url)
303 webpage = self._download_webpage(
304 url, song_id, note='Download mv detail info: %s' % song_id,
305 errnote='Unable to get mv detail info: %s' % song_id)
306
307 mobj = re.search(
a31e3e7d 308 r'<h1[^>]+title="(?P<song>[^"]+)">[^<]+<span[^>]+title="(?P<singer>[^"]+)"',
8f73e89c 309 webpage)
310 if mobj:
311 song_name = mobj.group('song')
312 singer_name = mobj.group('singer')
313 else:
cf2c5fda 314 raise ExtractorError('Unable to find song or singer names')
8f73e89c 315
316 formats = self._get_formats(song_id)
317
318 return {
319 'id': song_id,
320 'title': song_name,
321 'creator': singer_name,
322 'formats': formats,
323 }