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