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