[kuwo:category] Fix description and update test
[youtube-dl] / youtube_dl / extractor / kuwo.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8     get_element_by_id,
9     clean_html,
10     ExtractorError,
11     InAdvancePagedList,
12     remove_start,
13 )
14
15
16 class KuwoBaseIE(InfoExtractor):
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, tolerate_ip_deny=False):
27         formats = []
28         for file_format in self._FORMATS:
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
42             song_url = self._download_webpage(
43                 'http://antiserver.kuwo.cn/anti.s',
44                 song_id, note='Download %s url info' % file_format['format'],
45                 query=query, headers=headers,
46             )
47
48             if song_url == 'IPDeny' and not tolerate_ip_deny:
49                 raise ExtractorError('This song is blocked in this region', expected=True)
50
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                 })
59
60         return formats
61
62
63 class KuwoIE(KuwoBaseIE):
64     IE_NAME = 'kuwo:song'
65     IE_DESC = '酷我音乐'
66     _VALID_URL = r'https?://www\.kuwo\.cn/yinyue/(?P<id>\d+)'
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         },
77         'skip': 'this song has been offline because of copyright issues',
78     }, {
79         'url': 'http://www.kuwo.cn/yinyue/6446136/',
80         'info_dict': {
81             'id': '6446136',
82             'ext': 'mp3',
83             'title': '心',
84             'description': 'md5:5d0e947b242c35dc0eb1d2fce9fbf02c',
85             'creator': 'IU',
86             'upload_date': '20150518',
87         },
88         'params': {
89             'format': 'mp3-320'
90         },
91     }, {
92         'url': 'http://www.kuwo.cn/yinyue/3197154?catalog=yueku2016',
93         'only_matching': True,
94     }]
95
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')
101         if '对不起,该歌曲由于版权问题已被下线,将返回网站首页' in webpage:
102             raise ExtractorError('this song has been offline because of copyright issues', expected=True)
103
104         song_name = self._html_search_regex(
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), '歌手')
109         lrc_content = clean_html(get_element_by_id('lrcContent', webpage))
110         if lrc_content == '暂无':     # indicates no lyrics
111             lrc_content = None
112
113         formats = self._get_formats(song_id)
114         self._sort_formats(formats)
115
116         album_id = self._html_search_regex(
117             r'<a[^>]+href="http://www\.kuwo\.cn/album/(\d+)/"',
118             webpage, 'album id', fatal=False)
119
120         publish_time = None
121         if album_id is not None:
122             album_info_page = self._download_webpage(
123                 'http://www.kuwo.cn/album/%s/' % album_id, song_id,
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,
129                 'publish time', fatal=False)
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'
145     IE_DESC = '酷我音乐 - 专辑'
146     _VALID_URL = r'https?://www\.kuwo\.cn/album/(?P<id>\d+?)/'
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(
165             r'<div[^>]+class="comm"[^<]+<h1[^>]+title="([^"]+)"', webpage,
166             'album name')
167         album_intro = remove_start(
168             clean_html(get_element_by_id('intro', webpage)),
169             '%s简介:' % album_name)
170
171         entries = [
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+/)"',
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'
181     IE_DESC = '酷我音乐 - 排行榜'
182     _VALID_URL = r'https?://yinyue\.kuwo\.cn/billboard_(?P<id>[^.]+).htm'
183     _TEST = {
184         'url': 'http://yinyue.kuwo.cn/billboard_香港中文龙虎榜.htm',
185         'info_dict': {
186             'id': '香港中文龙虎榜',
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
197         entries = [
198             self.url_result(song_url, 'Kuwo') for song_url in re.findall(
199                 r'<a[^>]+href="(http://www\.kuwo\.cn/yinyue/\d+)', webpage)
200         ]
201         return self.playlist_result(entries, chart_id)
202
203
204 class KuwoSingerIE(InfoExtractor):
205     IE_NAME = 'kuwo:singer'
206     IE_DESC = '酷我音乐 - 歌手'
207     _VALID_URL = r'https?://www\.kuwo\.cn/mingxing/(?P<id>[^/]+)'
208     _TESTS = [{
209         'url': 'http://www.kuwo.cn/mingxing/bruno+mars/',
210         'info_dict': {
211             'id': 'bruno+mars',
212             'title': 'Bruno Mars',
213         },
214         'playlist_mincount': 329,
215     }, {
216         'url': 'http://www.kuwo.cn/mingxing/Ali/music.htm',
217         'info_dict': {
218             'id': 'Ali',
219             'title': 'Ali',
220         },
221         'playlist_mincount': 95,
222         'skip': 'Regularly stalls travis build',  # See https://travis-ci.org/rg3/youtube-dl/jobs/78878540
223     }]
224
225     PAGE_SIZE = 15
226
227     def _real_extract(self, url):
228         singer_id = self._match_id(url)
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(
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'))
241
242         def page_func(page_num):
243             webpage = self._download_webpage(
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})
248
249             return [
250                 self.url_result(song_url, 'Kuwo') for song_url in re.findall(
251                     r'<div[^>]+class="name"><a[^>]+href="(http://www\.kuwo\.cn/yinyue/\d+)',
252                     webpage)
253             ]
254
255         entries = InAdvancePagedList(page_func, page_count, self.PAGE_SIZE)
256
257         return self.playlist_result(entries, singer_id, singer_name)
258
259
260 class KuwoCategoryIE(InfoExtractor):
261     IE_NAME = 'kuwo:category'
262     IE_DESC = '酷我音乐 - 分类'
263     _VALID_URL = r'https?://yinyue\.kuwo\.cn/yy/cinfo_(?P<id>\d+?).htm'
264     _TEST = {
265         'url': 'http://yinyue.kuwo.cn/yy/cinfo_86375.htm',
266         'info_dict': {
267             'id': '86375',
268             'title': '八十年代精选',
269         },
270         'playlist_mincount': 24,
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(
280             r'<h1[^>]+title="([^<>]+?)">[^<>]+?</h1>', webpage, 'category name')
281
282         category_desc = remove_start(
283             get_element_by_id('intro', webpage).strip(),
284             '%s简介:' % category_name)
285         if category_desc == '暂无':
286             category_desc = None
287
288         jsonm = self._parse_json(self._html_search_regex(
289             r'var\s+jsonm\s*=\s*([^;]+);', webpage, 'category songs'), category_id)
290
291         entries = [
292             self.url_result('http://www.kuwo.cn/yinyue/%s/' % song['musicrid'], 'Kuwo')
293             for song in jsonm['musiclist']
294         ]
295         return self.playlist_result(entries, category_id, category_name, category_desc)
296
297
298 class KuwoMvIE(KuwoBaseIE):
299     IE_NAME = 'kuwo:mv'
300     IE_DESC = '酷我音乐 - MV'
301     _VALID_URL = r'https?://www\.kuwo\.cn/mv/(?P<id>\d+?)/'
302     _TEST = {
303         'url': 'http://www.kuwo.cn/mv/6480076/',
304         'info_dict': {
305             'id': '6480076',
306             'ext': 'mp4',
307             'title': 'My HouseMV',
308             'creator': '2PM',
309         },
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         },
316     }
317     _FORMATS = KuwoBaseIE._FORMATS + [
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(
329             r'<h1[^>]+title="(?P<song>[^"]+)">[^<]+<span[^>]+title="(?P<singer>[^"]+)"',
330             webpage)
331         if mobj:
332             song_name = mobj.group('song')
333             singer_name = mobj.group('singer')
334         else:
335             raise ExtractorError('Unable to find song or singer names')
336
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)
348
349         return {
350             'id': song_id,
351             'title': song_name,
352             'creator': singer_name,
353             'formats': formats,
354         }