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