From: ping Date: Thu, 18 Jun 2015 06:15:54 +0000 (+0800) Subject: [kuwo] New extractor for kuwo.cn X-Git-Url: http://git.oshgnacknak.de/?a=commitdiff_plain;h=8f73e89ca0ecde0a8bbd1f1463e9a06a53c6b573;p=youtube-dl [kuwo] New extractor for kuwo.cn --- diff --git a/youtube_dl/extractor/__init__.py b/youtube_dl/extractor/__init__.py index 6fdaf90b2..82cd85c44 100644 --- a/youtube_dl/extractor/__init__.py +++ b/youtube_dl/extractor/__init__.py @@ -257,6 +257,15 @@ from .keek import KeekIE from .kontrtube import KontrTubeIE from .krasview import KrasViewIE from .ku6 import Ku6IE +from .kuwo import ( + KuwoIE, + KuwoAlbumIE, + KuwoChartIE, + KuwoSingerIE, + KuwoSingerMusicIE, + KuwoCategoryIE, + KuwoMvIE, +) from .la7 import LA7IE from .laola1tv import Laola1TvIE from .letv import ( diff --git a/youtube_dl/extractor/kuwo.py b/youtube_dl/extractor/kuwo.py new file mode 100644 index 000000000..6a96a1aa4 --- /dev/null +++ b/youtube_dl/extractor/kuwo.py @@ -0,0 +1,326 @@ +# coding: utf-8 +from __future__ import unicode_literals + +import re +import itertools + +from .common import InfoExtractor +from ..utils import ( + get_element_by_id, + clean_html, + ExtractorError, +) + + +class KuwoIE(InfoExtractor): + IE_NAME = 'kuwo:song' + _VALID_URL = r'http://www\.kuwo\.cn/yinyue/(?P[0-9]+?)/' + _TESTS = [{ + 'url': 'http://www.kuwo.cn/yinyue/635632/', + 'info_dict': { + 'id': '635632', + 'ext': 'ape', + 'title': '爱我别走', + 'creator': '张震岳', + 'upload_date': '20080122', + 'description': 'md5:ed13f58e3c3bf3f7fd9fbc4e5a7aa75c' + }, + }, { + 'url': 'http://www.kuwo.cn/yinyue/6446136/', + 'info_dict': { + 'id': '6446136', + 'ext': 'mp3', + 'title': '心', + 'creator': 'IU', + 'upload_date': '20150518', + }, + 'params': { + 'format': 'mp3-320' + }, + }] + _FORMATS = [ + {'format': 'ape', 'ext': 'ape', 'preference': 100}, + {'format': 'mp3-320', 'ext': 'mp3', 'br': '320kmp3', 'abr': 320, 'preference': 80}, + {'format': 'mp3-192', 'ext': 'mp3', 'br': '192kmp3', 'abr': 192, 'preference': 70}, + {'format': 'mp3-128', 'ext': 'mp3', 'br': '128kmp3', 'abr': 128, 'preference': 60}, + {'format': 'wma', 'ext': 'wma', 'preference': 20}, + {'format': 'aac', 'ext': 'aac', 'abr': 48, 'preference': 10} + ] + + def _get_formats(self, song_id): + formats = [] + for file_format in self._FORMATS: + song_url = self._download_webpage( + "http://antiserver.kuwo.cn/anti.s?format=%s&br=%s&rid=MUSIC_%s&type=convert_url&response=url" % + (file_format['ext'], file_format.get('br', ''), song_id), + song_id, note="Download %s url info" % file_format["format"], + ) + if song_url.startswith('http://') or song_url.startswith('https://'): + formats.append({ + 'url': song_url, + 'format_id': file_format['format'], + 'format': file_format['format'], + 'preference': file_format['preference'], + 'abr': file_format.get('abr'), + }) + self._sort_formats(formats) + return formats + + def _real_extract(self, url): + song_id = self._match_id(url) + webpage = self._download_webpage( + url, song_id, note='Download song detail info', + errnote='Unable to get song detail info') + + song_name = self._html_search_regex( + r'

', webpage, 'song name') + singer_name = self._html_search_regex( + r'
.+?title="(.+?)".+?
', webpage, 'singer name', + flags=re.DOTALL, default=None) + lrc_content = clean_html(get_element_by_id("lrcContent", webpage)) + if lrc_content == '暂无': # indicates no lyrics + lrc_content = None + + formats = self._get_formats(song_id) + + album_id = self._html_search_regex( + r'

.+?[0-9]+?)/' + _TEST = { + 'url': 'http://www.kuwo.cn/album/502294/', + 'info_dict': { + 'id': '502294', + 'title': 'M', + 'description': 'md5:6a7235a84cc6400ec3b38a7bdaf1d60c', + }, + 'playlist_count': 2, + } + + def _real_extract(self, url): + album_id = self._match_id(url) + + webpage = self._download_webpage( + url, album_id, note='Download album info', + errnote='Unable to get album info') + + album_name = self._html_search_regex( + r'

', webpage, + 'album name', flags=re.DOTALL) + album_intro = clean_html( + re.sub(r'^.+简介:', '', get_element_by_id("intro", webpage).strip())) + + entries = [ + self.url_result("http://www.kuwo.cn/yinyue/%s/" % song_id, 'Kuwo', song_id) + for song_id in re.findall( + r'

', + webpage) + ] + return self.playlist_result(entries, album_id, album_name, album_intro) + + +class KuwoChartIE(InfoExtractor): + IE_NAME = 'kuwo:chart' + _VALID_URL = r'http://yinyue\.kuwo\.cn/billboard_(?P.+?).htm' + _TEST = { + 'url': 'http://yinyue.kuwo.cn/billboard_香港中文龙虎榜.htm', + 'info_dict': { + 'id': '香港中文龙虎榜', + 'title': '香港中文龙虎榜', + 'description': 're:[0-9]{4}第[0-9]{2}期', + }, + 'playlist_mincount': 10, + } + + def _real_extract(self, url): + chart_id = self._match_id(url) + webpage = self._download_webpage( + url, chart_id, note='Download chart info', + errnote='Unable to get chart info') + + chart_name = self._html_search_regex( + r'

(.+?)

', webpage, 'chart name') + + chart_desc = self._html_search_regex( + r'

([0-9]{4}第[0-9]{2}期)

', webpage, 'chart desc') + + entries = [ + self.url_result("http://www.kuwo.cn/yinyue/%s/" % song_id, 'Kuwo', song_id) + for song_id in re.findall( + r'.+?', webpage) + ] + return self.playlist_result(entries, chart_id, chart_name, chart_desc) + + +class KuwoSingerIE(InfoExtractor): + IE_NAME = 'kuwo:singer' + _VALID_URL = r'http://www\.kuwo\.cn/mingxing/(?P[^/]+?)/$' + _TEST = { + 'url': 'http://www.kuwo.cn/mingxing/bruno+mars/', + 'info_dict': { + 'id': 'bruno+mars', + 'title': 'Bruno Mars', + }, + 'playlist_count': 10, + } + + def _real_extract(self, url): + singer_id = self._match_id(url) + webpage = self._download_webpage( + url, singer_id, note='Download singer info', + errnote='Unable to get singer info') + + singer_name = self._html_search_regex( + r'姓名:(.+?)', webpage, 'singer name') + + entries = [ + self.url_result("http://www.kuwo.cn/yinyue/%s/" % song_id, 'Kuwo', song_id) + for song_id in re.findall( + r'.+?', + webpage, flags=re.DOTALL) + ] + return self.playlist_result(entries, singer_id, singer_name) + + +class KuwoSingerMusicIE(InfoExtractor): + IE_NAME = 'kuwo:singermusic' + _VALID_URL = r'http://www\.kuwo\.cn/mingxing/(?P[^/]+?)/music(_[0-9]+)?.htm' + _TEST = { + 'url': 'http://www.kuwo.cn/mingxing/Ali/music.htm', + 'info_dict': { + 'id': 'Ali', + 'title': 'Ali的热门歌曲', + }, + 'playlist_mincount': 95, + } + + def _real_extract(self, url): + singer_id = self._match_id(url) + + list_name = None + entries = [] + for page_num in itertools.count(1): + webpage = self._download_webpage( + 'http://www.kuwo.cn/mingxing/%s/music_%d.htm' % (singer_id, page_num), + singer_id, note='Download song list page #%d' % page_num, + errnote='Unable to get song list page #%d' % page_num) + + if list_name is None: + list_name = self._html_search_regex( + r'

([^<>]+)', webpage, 'list name') + + entries.extend([ + self.url_result("http://www.kuwo.cn/yinyue/%s/" % song_id, 'Kuwo', song_id) + for song_id in re.findall( + r'

下一页', webpage): + break + + return self.playlist_result(entries, singer_id, list_name) + + +class KuwoCategoryIE(InfoExtractor): + IE_NAME = 'kuwo:category' + _VALID_URL = r'http://yinyue\.kuwo\.cn/yy/cinfo_(?P[0-9]+?).htm' + _TEST = { + 'url': 'http://yinyue.kuwo.cn/yy/cinfo_86375.htm', + 'info_dict': { + 'id': '86375', + 'title': '八十年代精选', + 'description': '这些都是属于八十年代的回忆!', + }, + 'playlist_count': 30, + } + + def _real_extract(self, url): + category_id = self._match_id(url) + webpage = self._download_webpage( + url, category_id, note='Download category info', + errnote='Unable to get category info') + + category_name = self._html_search_regex( + r'

[^<>]+?

', webpage, 'category name') + + category_desc = re.sub( + r'^.+简介:', '', get_element_by_id("intro", webpage).strip()) + + jsonm = self._parse_json(self._html_search_regex( + r'var jsonm = (\{.+?\});', webpage, 'category songs'), category_id) + + entries = [ + self.url_result( + "http://www.kuwo.cn/yinyue/%s/" % song['musicrid'], + 'Kuwo', song['musicrid']) + for song in jsonm['musiclist'] + ] + return self.playlist_result(entries, category_id, category_name, category_desc) + + +class KuwoMvIE(KuwoIE): + IE_NAME = 'kuwo:mv' + _VALID_URL = r'http://www\.kuwo\.cn/mv/(?P[0-9]+?)/' + _TESTS = [{ + 'url': 'http://www.kuwo.cn/mv/6480076/', + 'info_dict': { + 'id': '6480076', + 'ext': 'mkv', + 'title': '我们家MV', + 'creator': '2PM', + }, + }] + _FORMATS = KuwoIE._FORMATS + [ + {'format': 'mkv', 'ext': 'mkv', 'preference': 250}, + {'format': 'mp4', 'ext': 'mp4', 'preference': 200}, + ] + + def _real_extract(self, url): + song_id = self._match_id(url) + webpage = self._download_webpage( + url, song_id, note='Download mv detail info: %s' % song_id, + errnote='Unable to get mv detail info: %s' % song_id) + + mobj = re.search( + r'

[^<>]+[^<>]+

', + webpage) + if mobj: + song_name = mobj.group('song') + singer_name = mobj.group('singer') + else: + raise ExtractorError("Unable to find song or singer names") + + formats = self._get_formats(song_id) + + return { + 'id': song_id, + 'title': song_name, + 'creator': singer_name, + 'formats': formats, + }