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