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