[kwuo] Remove _sort_formats() from KuwoBaseIE._get_formats()
[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             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
35             if song_url == 'IPDeny' and not tolerate_ip_deny:
36                 raise ExtractorError('This song is blocked in this region', expected=True)
37
38             if song_url.startswith('http://') or song_url.startswith('https://'):
39                 formats.append({
40                     'url': song_url,
41                     'format_id': file_format['format'],
42                     'format': file_format['format'],
43                     'preference': file_format['preference'],
44                     'abr': file_format.get('abr'),
45                 })
46
47         return formats
48
49
50 class KuwoIE(KuwoBaseIE):
51     IE_NAME = 'kuwo:song'
52     IE_DESC = '酷我音乐'
53     _VALID_URL = r'https?://www\.kuwo\.cn/yinyue/(?P<id>\d+)'
54     _TESTS = [{
55         'url': 'http://www.kuwo.cn/yinyue/635632/',
56         'info_dict': {
57             'id': '635632',
58             'ext': 'ape',
59             'title': '爱我别走',
60             'creator': '张震岳',
61             'upload_date': '20080122',
62             'description': 'md5:ed13f58e3c3bf3f7fd9fbc4e5a7aa75c'
63         },
64         'skip': 'this song has been offline because of copyright issues',
65     }, {
66         'url': 'http://www.kuwo.cn/yinyue/6446136/',
67         'info_dict': {
68             'id': '6446136',
69             'ext': 'mp3',
70             'title': '心',
71             'description': 'md5:b2ab6295d014005bfc607525bfc1e38a',
72             'creator': 'IU',
73             'upload_date': '20150518',
74         },
75         'params': {
76             'format': 'mp3-320'
77         },
78     }, {
79         'url': 'http://www.kuwo.cn/yinyue/3197154?catalog=yueku2016',
80         'only_matching': True,
81     }]
82
83     def _real_extract(self, url):
84         song_id = self._match_id(url)
85         webpage = self._download_webpage(
86             url, song_id, note='Download song detail info',
87             errnote='Unable to get song detail info')
88         if '对不起,该歌曲由于版权问题已被下线,将返回网站首页' in webpage:
89             raise ExtractorError('this song has been offline because of copyright issues', expected=True)
90
91         song_name = self._html_search_regex(
92             r'(?s)class="(?:[^"\s]+\s+)*title(?:\s+[^"\s]+)*".*?<h1[^>]+title="([^"]+)"', webpage, 'song name')
93         singer_name = self._html_search_regex(
94             r'<div[^>]+class="s_img">\s*<a[^>]+title="([^>]+)"',
95             webpage, 'singer name', fatal=False)
96         lrc_content = clean_html(get_element_by_id('lrcContent', webpage))
97         if lrc_content == '暂无':     # indicates no lyrics
98             lrc_content = None
99
100         formats = self._get_formats(song_id)
101         self._sort_formats(formats)
102
103         album_id = self._html_search_regex(
104             r'<p[^>]+class="album"[^<]+<a[^>]+href="http://www\.kuwo\.cn/album/(\d+)/"',
105             webpage, 'album id', fatal=False)
106
107         publish_time = None
108         if album_id is not None:
109             album_info_page = self._download_webpage(
110                 'http://www.kuwo.cn/album/%s/' % album_id, song_id,
111                 note='Download album detail info',
112                 errnote='Unable to get album detail info')
113
114             publish_time = self._html_search_regex(
115                 r'发行时间:(\d{4}-\d{2}-\d{2})', album_info_page,
116                 'publish time', fatal=False)
117             if publish_time:
118                 publish_time = publish_time.replace('-', '')
119
120         return {
121             'id': song_id,
122             'title': song_name,
123             'creator': singer_name,
124             'upload_date': publish_time,
125             'description': lrc_content,
126             'formats': formats,
127         }
128
129
130 class KuwoAlbumIE(InfoExtractor):
131     IE_NAME = 'kuwo:album'
132     IE_DESC = '酷我音乐 - 专辑'
133     _VALID_URL = r'https?://www\.kuwo\.cn/album/(?P<id>\d+?)/'
134     _TEST = {
135         'url': 'http://www.kuwo.cn/album/502294/',
136         'info_dict': {
137             'id': '502294',
138             'title': 'M',
139             'description': 'md5:6a7235a84cc6400ec3b38a7bdaf1d60c',
140         },
141         'playlist_count': 2,
142     }
143
144     def _real_extract(self, url):
145         album_id = self._match_id(url)
146
147         webpage = self._download_webpage(
148             url, album_id, note='Download album info',
149             errnote='Unable to get album info')
150
151         album_name = self._html_search_regex(
152             r'<div[^>]+class="comm"[^<]+<h1[^>]+title="([^"]+)"', webpage,
153             'album name')
154         album_intro = remove_start(
155             clean_html(get_element_by_id('intro', webpage)),
156             '%s简介:' % album_name)
157
158         entries = [
159             self.url_result(song_url, 'Kuwo') for song_url in re.findall(
160                 r'<p[^>]+class="listen"><a[^>]+href="(http://www\.kuwo\.cn/yinyue/\d+/)"',
161                 webpage)
162         ]
163         return self.playlist_result(entries, album_id, album_name, album_intro)
164
165
166 class KuwoChartIE(InfoExtractor):
167     IE_NAME = 'kuwo:chart'
168     IE_DESC = '酷我音乐 - 排行榜'
169     _VALID_URL = r'https?://yinyue\.kuwo\.cn/billboard_(?P<id>[^.]+).htm'
170     _TEST = {
171         'url': 'http://yinyue.kuwo.cn/billboard_香港中文龙虎榜.htm',
172         'info_dict': {
173             'id': '香港中文龙虎榜',
174         },
175         'playlist_mincount': 10,
176     }
177
178     def _real_extract(self, url):
179         chart_id = self._match_id(url)
180         webpage = self._download_webpage(
181             url, chart_id, note='Download chart info',
182             errnote='Unable to get chart info')
183
184         entries = [
185             self.url_result(song_url, 'Kuwo') for song_url in re.findall(
186                 r'<a[^>]+href="(http://www\.kuwo\.cn/yinyue/\d+)', webpage)
187         ]
188         return self.playlist_result(entries, chart_id)
189
190
191 class KuwoSingerIE(InfoExtractor):
192     IE_NAME = 'kuwo:singer'
193     IE_DESC = '酷我音乐 - 歌手'
194     _VALID_URL = r'https?://www\.kuwo\.cn/mingxing/(?P<id>[^/]+)'
195     _TESTS = [{
196         'url': 'http://www.kuwo.cn/mingxing/bruno+mars/',
197         'info_dict': {
198             'id': 'bruno+mars',
199             'title': 'Bruno Mars',
200         },
201         'playlist_mincount': 329,
202     }, {
203         'url': 'http://www.kuwo.cn/mingxing/Ali/music.htm',
204         'info_dict': {
205             'id': 'Ali',
206             'title': 'Ali',
207         },
208         'playlist_mincount': 95,
209         'skip': 'Regularly stalls travis build',  # See https://travis-ci.org/rg3/youtube-dl/jobs/78878540
210     }]
211
212     PAGE_SIZE = 15
213
214     def _real_extract(self, url):
215         singer_id = self._match_id(url)
216         webpage = self._download_webpage(
217             url, singer_id, note='Download singer info',
218             errnote='Unable to get singer info')
219
220         singer_name = self._html_search_regex(
221             r'<h1>([^<]+)</h1>', webpage, 'singer name')
222
223         artist_id = self._html_search_regex(
224             r'data-artistid="(\d+)"', webpage, 'artist id')
225
226         page_count = int(self._html_search_regex(
227             r'data-page="(\d+)"', webpage, 'page count'))
228
229         def page_func(page_num):
230             webpage = self._download_webpage(
231                 'http://www.kuwo.cn/artist/contentMusicsAjax',
232                 singer_id, note='Download song list page #%d' % (page_num + 1),
233                 errnote='Unable to get song list page #%d' % (page_num + 1),
234                 query={'artistId': artist_id, 'pn': page_num, 'rn': self.PAGE_SIZE})
235
236             return [
237                 self.url_result(song_url, 'Kuwo') for song_url in re.findall(
238                     r'<div[^>]+class="name"><a[^>]+href="(http://www\.kuwo\.cn/yinyue/\d+)',
239                     webpage)
240             ]
241
242         entries = InAdvancePagedList(page_func, page_count, self.PAGE_SIZE)
243
244         return self.playlist_result(entries, singer_id, singer_name)
245
246
247 class KuwoCategoryIE(InfoExtractor):
248     IE_NAME = 'kuwo:category'
249     IE_DESC = '酷我音乐 - 分类'
250     _VALID_URL = r'https?://yinyue\.kuwo\.cn/yy/cinfo_(?P<id>\d+?).htm'
251     _TEST = {
252         'url': 'http://yinyue.kuwo.cn/yy/cinfo_86375.htm',
253         'info_dict': {
254             'id': '86375',
255             'title': '八十年代精选',
256             'description': '这些都是属于八十年代的回忆!',
257         },
258         'playlist_count': 30,
259     }
260
261     def _real_extract(self, url):
262         category_id = self._match_id(url)
263         webpage = self._download_webpage(
264             url, category_id, note='Download category info',
265             errnote='Unable to get category info')
266
267         category_name = self._html_search_regex(
268             r'<h1[^>]+title="([^<>]+?)">[^<>]+?</h1>', webpage, 'category name')
269
270         category_desc = remove_start(
271             get_element_by_id('intro', webpage).strip(),
272             '%s简介:' % category_name)
273
274         jsonm = self._parse_json(self._html_search_regex(
275             r'var\s+jsonm\s*=\s*([^;]+);', webpage, 'category songs'), category_id)
276
277         entries = [
278             self.url_result('http://www.kuwo.cn/yinyue/%s/' % song['musicrid'], 'Kuwo')
279             for song in jsonm['musiclist']
280         ]
281         return self.playlist_result(entries, category_id, category_name, category_desc)
282
283
284 class KuwoMvIE(KuwoBaseIE):
285     IE_NAME = 'kuwo:mv'
286     IE_DESC = '酷我音乐 - MV'
287     _VALID_URL = r'https?://www\.kuwo\.cn/mv/(?P<id>\d+?)/'
288     _TEST = {
289         'url': 'http://www.kuwo.cn/mv/6480076/',
290         'info_dict': {
291             'id': '6480076',
292             'ext': 'mp4',
293             'title': 'My HouseMV',
294             'creator': '2PM',
295         },
296         # In this video, music URLs (anti.s) are blocked outside China and
297         # USA, while the MV URL (mvurl) is available globally, so force the MV
298         # URL for consistent results in different countries
299         'params': {
300             'format': 'mv',
301         },
302     }
303     _FORMATS = KuwoBaseIE._FORMATS + [
304         {'format': 'mkv', 'ext': 'mkv', 'preference': 250},
305         {'format': 'mp4', 'ext': 'mp4', 'preference': 200},
306     ]
307
308     def _real_extract(self, url):
309         song_id = self._match_id(url)
310         webpage = self._download_webpage(
311             url, song_id, note='Download mv detail info: %s' % song_id,
312             errnote='Unable to get mv detail info: %s' % song_id)
313
314         mobj = re.search(
315             r'<h1[^>]+title="(?P<song>[^"]+)">[^<]+<span[^>]+title="(?P<singer>[^"]+)"',
316             webpage)
317         if mobj:
318             song_name = mobj.group('song')
319             singer_name = mobj.group('singer')
320         else:
321             raise ExtractorError('Unable to find song or singer names')
322
323         formats = self._get_formats(song_id, tolerate_ip_deny=True)
324
325         mv_url = self._download_webpage(
326             'http://www.kuwo.cn/yy/st/mvurl?rid=MUSIC_%s' % song_id,
327             song_id, note='Download %s MV URL' % song_id)
328         formats.append({
329             'url': mv_url,
330             'format_id': 'mv',
331         })
332
333         self._sort_formats(formats)
334
335         return {
336             'id': song_id,
337             'title': song_name,
338             'creator': singer_name,
339             'formats': formats,
340         }