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