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