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