]>
Commit | Line | Data |
---|---|---|
8f73e89c | 1 | # coding: utf-8 |
2 | from __future__ import unicode_literals | |
3 | ||
4 | import re | |
8f73e89c | 5 | |
6 | from .common import InfoExtractor | |
7 | from ..utils import ( | |
8 | get_element_by_id, | |
9 | clean_html, | |
10 | ExtractorError, | |
daef04a4 | 11 | InAdvancePagedList, |
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 | ||
3ff8279e | 26 | def _get_formats(self, song_id, tolerate_ip_deny=False): |
8f73e89c | 27 | formats = [] |
28 | for file_format in self._FORMATS: | |
e621a344 YCH |
29 | headers = {} |
30 | cn_verification_proxy = self._downloader.params.get('cn_verification_proxy') | |
31 | if cn_verification_proxy: | |
32 | headers['Ytdl-request-proxy'] = cn_verification_proxy | |
33 | ||
34 | query = { | |
35 | 'format': file_format['ext'], | |
36 | 'br': file_format.get('br', ''), | |
37 | 'rid': 'MUSIC_%s' % song_id, | |
38 | 'type': 'convert_url', | |
39 | 'response': 'url' | |
40 | } | |
41 | ||
8f73e89c | 42 | song_url = self._download_webpage( |
e621a344 | 43 | 'http://antiserver.kuwo.cn/anti.s', |
cf2c5fda | 44 | song_id, note='Download %s url info' % file_format['format'], |
e621a344 | 45 | query=query, headers=headers, |
8f73e89c | 46 | ) |
58be9220 | 47 | |
3ff8279e | 48 | if song_url == 'IPDeny' and not tolerate_ip_deny: |
58be9220 YCH |
49 | raise ExtractorError('This song is blocked in this region', expected=True) |
50 | ||
8f73e89c | 51 | if song_url.startswith('http://') or song_url.startswith('https://'): |
52 | formats.append({ | |
53 | 'url': song_url, | |
54 | 'format_id': file_format['format'], | |
55 | 'format': file_format['format'], | |
56 | 'preference': file_format['preference'], | |
57 | 'abr': file_format.get('abr'), | |
58 | }) | |
3ff8279e | 59 | |
8f73e89c | 60 | return formats |
61 | ||
a9684c0d YCH |
62 | |
63 | class KuwoIE(KuwoBaseIE): | |
64 | IE_NAME = 'kuwo:song' | |
0f08d7f8 | 65 | IE_DESC = '酷我音乐' |
597d52fa | 66 | _VALID_URL = r'https?://www\.kuwo\.cn/yinyue/(?P<id>\d+)' |
a9684c0d YCH |
67 | _TESTS = [{ |
68 | 'url': 'http://www.kuwo.cn/yinyue/635632/', | |
69 | 'info_dict': { | |
70 | 'id': '635632', | |
71 | 'ext': 'ape', | |
72 | 'title': '爱我别走', | |
73 | 'creator': '张震岳', | |
74 | 'upload_date': '20080122', | |
75 | 'description': 'md5:ed13f58e3c3bf3f7fd9fbc4e5a7aa75c' | |
76 | }, | |
c44c7895 | 77 | 'skip': 'this song has been offline because of copyright issues', |
a9684c0d YCH |
78 | }, { |
79 | 'url': 'http://www.kuwo.cn/yinyue/6446136/', | |
80 | 'info_dict': { | |
81 | 'id': '6446136', | |
82 | 'ext': 'mp3', | |
83 | 'title': '心', | |
a82398bd | 84 | 'description': 'md5:5d0e947b242c35dc0eb1d2fce9fbf02c', |
a9684c0d YCH |
85 | 'creator': 'IU', |
86 | 'upload_date': '20150518', | |
87 | }, | |
88 | 'params': { | |
89 | 'format': 'mp3-320' | |
90 | }, | |
daef04a4 YCH |
91 | }, { |
92 | 'url': 'http://www.kuwo.cn/yinyue/3197154?catalog=yueku2016', | |
93 | 'only_matching': True, | |
a9684c0d YCH |
94 | }] |
95 | ||
8f73e89c | 96 | def _real_extract(self, url): |
97 | song_id = self._match_id(url) | |
98 | webpage = self._download_webpage( | |
99 | url, song_id, note='Download song detail info', | |
100 | errnote='Unable to get song detail info') | |
5db34f68 | 101 | if '对不起,该歌曲由于版权问题已被下线,将返回网站首页' in webpage: |
71936506 | 102 | raise ExtractorError('this song has been offline because of copyright issues', expected=True) |
8f73e89c | 103 | |
104 | song_name = self._html_search_regex( | |
a82398bd YCH |
105 | r'<p[^>]+id="lrcName">([^<]+)</p>', webpage, 'song name') |
106 | singer_name = remove_start(self._html_search_regex( | |
107 | r'<a[^>]+href="http://www\.kuwo\.cn/artist/content\?name=([^"]+)">', | |
108 | webpage, 'singer name', fatal=False), '歌手') | |
cf2c5fda | 109 | lrc_content = clean_html(get_element_by_id('lrcContent', webpage)) |
8f73e89c | 110 | if lrc_content == '暂无': # indicates no lyrics |
111 | lrc_content = None | |
112 | ||
113 | formats = self._get_formats(song_id) | |
3ae6f8fe | 114 | self._sort_formats(formats) |
8f73e89c | 115 | |
116 | album_id = self._html_search_regex( | |
a82398bd | 117 | r'<a[^>]+href="http://www\.kuwo\.cn/album/(\d+)/"', |
094790d2 | 118 | webpage, 'album id', fatal=False) |
8f73e89c | 119 | |
120 | publish_time = None | |
121 | if album_id is not None: | |
122 | album_info_page = self._download_webpage( | |
cf2c5fda | 123 | 'http://www.kuwo.cn/album/%s/' % album_id, song_id, |
8f73e89c | 124 | note='Download album detail info', |
125 | errnote='Unable to get album detail info') | |
126 | ||
127 | publish_time = self._html_search_regex( | |
128 | r'发行时间:(\d{4}-\d{2}-\d{2})', album_info_page, | |
094790d2 | 129 | 'publish time', fatal=False) |
8f73e89c | 130 | if publish_time: |
131 | publish_time = publish_time.replace('-', '') | |
132 | ||
133 | return { | |
134 | 'id': song_id, | |
135 | 'title': song_name, | |
136 | 'creator': singer_name, | |
137 | 'upload_date': publish_time, | |
138 | 'description': lrc_content, | |
139 | 'formats': formats, | |
140 | } | |
141 | ||
142 | ||
143 | class KuwoAlbumIE(InfoExtractor): | |
144 | IE_NAME = 'kuwo:album' | |
edd66be5 | 145 | IE_DESC = '酷我音乐 - 专辑' |
5886b38d | 146 | _VALID_URL = r'https?://www\.kuwo\.cn/album/(?P<id>\d+?)/' |
8f73e89c | 147 | _TEST = { |
148 | 'url': 'http://www.kuwo.cn/album/502294/', | |
149 | 'info_dict': { | |
150 | 'id': '502294', | |
151 | 'title': 'M', | |
152 | 'description': 'md5:6a7235a84cc6400ec3b38a7bdaf1d60c', | |
153 | }, | |
154 | 'playlist_count': 2, | |
155 | } | |
156 | ||
157 | def _real_extract(self, url): | |
158 | album_id = self._match_id(url) | |
159 | ||
160 | webpage = self._download_webpage( | |
161 | url, album_id, note='Download album info', | |
162 | errnote='Unable to get album info') | |
163 | ||
164 | album_name = self._html_search_regex( | |
a31e3e7d YCH |
165 | r'<div[^>]+class="comm"[^<]+<h1[^>]+title="([^"]+)"', webpage, |
166 | 'album name') | |
167 | album_intro = remove_start( | |
cf2c5fda | 168 | clean_html(get_element_by_id('intro', webpage)), |
a31e3e7d | 169 | '%s简介:' % album_name) |
8f73e89c | 170 | |
171 | entries = [ | |
d3b89088 YCH |
172 | self.url_result(song_url, 'Kuwo') for song_url in re.findall( |
173 | r'<p[^>]+class="listen"><a[^>]+href="(http://www\.kuwo\.cn/yinyue/\d+/)"', | |
8f73e89c | 174 | webpage) |
175 | ] | |
176 | return self.playlist_result(entries, album_id, album_name, album_intro) | |
177 | ||
178 | ||
179 | class KuwoChartIE(InfoExtractor): | |
180 | IE_NAME = 'kuwo:chart' | |
edd66be5 | 181 | IE_DESC = '酷我音乐 - 排行榜' |
5886b38d | 182 | _VALID_URL = r'https?://yinyue\.kuwo\.cn/billboard_(?P<id>[^.]+).htm' |
8f73e89c | 183 | _TEST = { |
184 | 'url': 'http://yinyue.kuwo.cn/billboard_香港中文龙虎榜.htm', | |
185 | 'info_dict': { | |
186 | 'id': '香港中文龙虎榜', | |
8f73e89c | 187 | }, |
188 | 'playlist_mincount': 10, | |
189 | } | |
190 | ||
191 | def _real_extract(self, url): | |
192 | chart_id = self._match_id(url) | |
193 | webpage = self._download_webpage( | |
194 | url, chart_id, note='Download chart info', | |
195 | errnote='Unable to get chart info') | |
196 | ||
8f73e89c | 197 | entries = [ |
d3b89088 | 198 | self.url_result(song_url, 'Kuwo') for song_url in re.findall( |
daef04a4 | 199 | r'<a[^>]+href="(http://www\.kuwo\.cn/yinyue/\d+)', webpage) |
8f73e89c | 200 | ] |
daef04a4 | 201 | return self.playlist_result(entries, chart_id) |
8f73e89c | 202 | |
203 | ||
204 | class KuwoSingerIE(InfoExtractor): | |
205 | IE_NAME = 'kuwo:singer' | |
edd66be5 | 206 | IE_DESC = '酷我音乐 - 歌手' |
5886b38d | 207 | _VALID_URL = r'https?://www\.kuwo\.cn/mingxing/(?P<id>[^/]+)' |
1633491b | 208 | _TESTS = [{ |
8f73e89c | 209 | 'url': 'http://www.kuwo.cn/mingxing/bruno+mars/', |
210 | 'info_dict': { | |
211 | 'id': 'bruno+mars', | |
212 | 'title': 'Bruno Mars', | |
213 | }, | |
daef04a4 | 214 | 'playlist_mincount': 329, |
1633491b | 215 | }, { |
8f73e89c | 216 | 'url': 'http://www.kuwo.cn/mingxing/Ali/music.htm', |
217 | 'info_dict': { | |
218 | 'id': 'Ali', | |
1633491b | 219 | 'title': 'Ali', |
8f73e89c | 220 | }, |
221 | 'playlist_mincount': 95, | |
12bbd32a | 222 | 'skip': 'Regularly stalls travis build', # See https://travis-ci.org/rg3/youtube-dl/jobs/78878540 |
1633491b | 223 | }] |
8f73e89c | 224 | |
daef04a4 YCH |
225 | PAGE_SIZE = 15 |
226 | ||
8f73e89c | 227 | def _real_extract(self, url): |
228 | singer_id = self._match_id(url) | |
1633491b | 229 | webpage = self._download_webpage( |
230 | url, singer_id, note='Download singer info', | |
231 | errnote='Unable to get singer info') | |
232 | ||
233 | singer_name = self._html_search_regex( | |
daef04a4 YCH |
234 | r'<h1>([^<]+)</h1>', webpage, 'singer name') |
235 | ||
236 | artist_id = self._html_search_regex( | |
237 | r'data-artistid="(\d+)"', webpage, 'artist id') | |
238 | ||
239 | page_count = int(self._html_search_regex( | |
240 | r'data-page="(\d+)"', webpage, 'page count')) | |
8f73e89c | 241 | |
daef04a4 | 242 | def page_func(page_num): |
8f73e89c | 243 | webpage = self._download_webpage( |
daef04a4 YCH |
244 | 'http://www.kuwo.cn/artist/contentMusicsAjax', |
245 | singer_id, note='Download song list page #%d' % (page_num + 1), | |
246 | errnote='Unable to get song list page #%d' % (page_num + 1), | |
247 | query={'artistId': artist_id, 'pn': page_num, 'rn': self.PAGE_SIZE}) | |
8f73e89c | 248 | |
daef04a4 | 249 | return [ |
d3b89088 | 250 | self.url_result(song_url, 'Kuwo') for song_url in re.findall( |
daef04a4 | 251 | r'<div[^>]+class="name"><a[^>]+href="(http://www\.kuwo\.cn/yinyue/\d+)', |
8f73e89c | 252 | webpage) |
daef04a4 | 253 | ] |
1633491b | 254 | |
daef04a4 | 255 | entries = InAdvancePagedList(page_func, page_count, self.PAGE_SIZE) |
8f73e89c | 256 | |
1633491b | 257 | return self.playlist_result(entries, singer_id, singer_name) |
8f73e89c | 258 | |
259 | ||
260 | class KuwoCategoryIE(InfoExtractor): | |
261 | IE_NAME = 'kuwo:category' | |
edd66be5 | 262 | IE_DESC = '酷我音乐 - 分类' |
5886b38d | 263 | _VALID_URL = r'https?://yinyue\.kuwo\.cn/yy/cinfo_(?P<id>\d+?).htm' |
8f73e89c | 264 | _TEST = { |
265 | 'url': 'http://yinyue.kuwo.cn/yy/cinfo_86375.htm', | |
266 | 'info_dict': { | |
267 | 'id': '86375', | |
268 | 'title': '八十年代精选', | |
8f73e89c | 269 | }, |
03dd60ca | 270 | 'playlist_mincount': 24, |
8f73e89c | 271 | } |
272 | ||
273 | def _real_extract(self, url): | |
274 | category_id = self._match_id(url) | |
275 | webpage = self._download_webpage( | |
276 | url, category_id, note='Download category info', | |
277 | errnote='Unable to get category info') | |
278 | ||
279 | category_name = self._html_search_regex( | |
a31e3e7d | 280 | r'<h1[^>]+title="([^<>]+?)">[^<>]+?</h1>', webpage, 'category name') |
a34af8d0 | 281 | |
a31e3e7d | 282 | category_desc = remove_start( |
cf2c5fda | 283 | get_element_by_id('intro', webpage).strip(), |
a31e3e7d | 284 | '%s简介:' % category_name) |
a0a309b9 YCH |
285 | if category_desc == '暂无': |
286 | category_desc = None | |
a34af8d0 | 287 | |
8f73e89c | 288 | jsonm = self._parse_json(self._html_search_regex( |
a31e3e7d | 289 | r'var\s+jsonm\s*=\s*([^;]+);', webpage, 'category songs'), category_id) |
8f73e89c | 290 | |
291 | entries = [ | |
d3b89088 | 292 | self.url_result('http://www.kuwo.cn/yinyue/%s/' % song['musicrid'], 'Kuwo') |
8f73e89c | 293 | for song in jsonm['musiclist'] |
294 | ] | |
295 | return self.playlist_result(entries, category_id, category_name, category_desc) | |
296 | ||
297 | ||
a9684c0d | 298 | class KuwoMvIE(KuwoBaseIE): |
8f73e89c | 299 | IE_NAME = 'kuwo:mv' |
edd66be5 | 300 | IE_DESC = '酷我音乐 - MV' |
5886b38d | 301 | _VALID_URL = r'https?://www\.kuwo\.cn/mv/(?P<id>\d+?)/' |
a9684c0d | 302 | _TEST = { |
8f73e89c | 303 | 'url': 'http://www.kuwo.cn/mv/6480076/', |
304 | 'info_dict': { | |
305 | 'id': '6480076', | |
3ff8279e YCH |
306 | 'ext': 'mp4', |
307 | 'title': 'My HouseMV', | |
8f73e89c | 308 | 'creator': '2PM', |
309 | }, | |
3ff8279e YCH |
310 | # In this video, music URLs (anti.s) are blocked outside China and |
311 | # USA, while the MV URL (mvurl) is available globally, so force the MV | |
312 | # URL for consistent results in different countries | |
313 | 'params': { | |
314 | 'format': 'mv', | |
315 | }, | |
a9684c0d YCH |
316 | } |
317 | _FORMATS = KuwoBaseIE._FORMATS + [ | |
8f73e89c | 318 | {'format': 'mkv', 'ext': 'mkv', 'preference': 250}, |
319 | {'format': 'mp4', 'ext': 'mp4', 'preference': 200}, | |
320 | ] | |
321 | ||
322 | def _real_extract(self, url): | |
323 | song_id = self._match_id(url) | |
324 | webpage = self._download_webpage( | |
325 | url, song_id, note='Download mv detail info: %s' % song_id, | |
326 | errnote='Unable to get mv detail info: %s' % song_id) | |
327 | ||
328 | mobj = re.search( | |
a31e3e7d | 329 | r'<h1[^>]+title="(?P<song>[^"]+)">[^<]+<span[^>]+title="(?P<singer>[^"]+)"', |
8f73e89c | 330 | webpage) |
331 | if mobj: | |
332 | song_name = mobj.group('song') | |
333 | singer_name = mobj.group('singer') | |
334 | else: | |
cf2c5fda | 335 | raise ExtractorError('Unable to find song or singer names') |
8f73e89c | 336 | |
3ff8279e YCH |
337 | formats = self._get_formats(song_id, tolerate_ip_deny=True) |
338 | ||
339 | mv_url = self._download_webpage( | |
340 | 'http://www.kuwo.cn/yy/st/mvurl?rid=MUSIC_%s' % song_id, | |
341 | song_id, note='Download %s MV URL' % song_id) | |
342 | formats.append({ | |
343 | 'url': mv_url, | |
344 | 'format_id': 'mv', | |
345 | }) | |
346 | ||
347 | self._sort_formats(formats) | |
8f73e89c | 348 | |
349 | return { | |
350 | 'id': song_id, | |
351 | 'title': song_name, | |
352 | 'creator': singer_name, | |
353 | 'formats': formats, | |
354 | } |