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