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