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