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