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