From a31e3e7dcb9d0471d90ec8562934a144d25d7132 Mon Sep 17 00:00:00 2001 From: Yen Chi Hsuan Date: Fri, 10 Jul 2015 23:23:07 +0800 Subject: [PATCH] [kuwo] Regular expression improvements 1. Prevent .+ and .* 2. Use [^>]+ instead of spaces for HTML tags 3. Remove unnecessary trailing parts --- youtube_dl/extractor/kuwo.py | 47 +++++++++++++++------------- youtube_dl/extractor/neteasemusic.py | 2 +- 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/youtube_dl/extractor/kuwo.py b/youtube_dl/extractor/kuwo.py index 9c62191b5..1095a26e2 100644 --- a/youtube_dl/extractor/kuwo.py +++ b/youtube_dl/extractor/kuwo.py @@ -9,6 +9,7 @@ get_element_by_id, clean_html, ExtractorError, + remove_start, ) @@ -73,10 +74,10 @@ def _real_extract(self, url): errnote='Unable to get song detail info') song_name = self._html_search_regex( - r'

', webpage, 'song name') + r']+title="([^"]+)">', webpage, 'song name') singer_name = self._html_search_regex( - r'
.+?title="(.+?)".+?
', webpage, 'singer name', - flags=re.DOTALL, default=None) + r']+class="s_img">\s*]+title="([^>]+)"', + webpage, 'singer name', default=None) lrc_content = clean_html(get_element_by_id("lrcContent", webpage)) if lrc_content == '暂无': # indicates no lyrics lrc_content = None @@ -84,7 +85,7 @@ def _real_extract(self, url): formats = self._get_formats(song_id) album_id = self._html_search_regex( - r'

.+?]+class="album"[^<]+]+href="http://www\.kuwo\.cn/album/(\d+)/"', webpage, 'album id', default=None, fatal=False) publish_time = None @@ -131,15 +132,16 @@ def _real_extract(self, url): 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())) + r']+class="comm"[^<]+]+title="([^"]+)"', webpage, + 'album name') + album_intro = remove_start( + clean_html(get_element_by_id("intro", webpage)), + '%s简介:' % album_name) entries = [ self.url_result("http://www.kuwo.cn/yinyue/%s/" % song_id, 'Kuwo', song_id) for song_id in re.findall( - r'

', + r']+class="listen">]+href="http://www\.kuwo\.cn/yinyue/(\d+)/"', webpage) ] return self.playlist_result(entries, album_id, album_name, album_intro) @@ -147,7 +149,7 @@ def _real_extract(self, url): class KuwoChartIE(InfoExtractor): IE_NAME = 'kuwo:chart' - _VALID_URL = r'http://yinyue\.kuwo\.cn/billboard_(?P.+?).htm' + _VALID_URL = r'http://yinyue\.kuwo\.cn/billboard_(?P[^.]+).htm' _TEST = { 'url': 'http://yinyue.kuwo.cn/billboard_香港中文龙虎榜.htm', 'info_dict': { @@ -165,15 +167,15 @@ def _real_extract(self, url): errnote='Unable to get chart info') chart_name = self._html_search_regex( - r'

(.+?)

', webpage, 'chart name') + r']+class="unDis">([^<]+)', webpage, 'chart name') chart_desc = self._html_search_regex( - r'

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

', webpage, 'chart desc') + r']+class="tabDef">(\d{4}第\d{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) + r']+href="http://www\.kuwo\.cn/yinyue/(\d+)/"', webpage) ] return self.playlist_result(entries, chart_id, chart_name, chart_desc) @@ -204,11 +206,11 @@ def _real_extract(self, url): errnote='Unable to get singer info') singer_name = self._html_search_regex( - r'
[\n\s\t]*?

(.+?)\s*

([^<]+)]+href="http://www\.kuwo\.cn/yinyue/([0-9]+)/', webpage) ][:10 if first_page_only else None]) - if first_page_only or not re.search(r'下一页', webpage): + if first_page_only or not re.search(r']+href="[^"]+">下一页', webpage): break return self.playlist_result(entries, singer_id, singer_name) @@ -248,13 +250,14 @@ def _real_extract(self, url): errnote='Unable to get category info') category_name = self._html_search_regex( - r'

[^<>]+?

', webpage, 'category name') + r']+title="([^<>]+?)">[^<>]+?

', webpage, 'category name') - category_desc = re.sub( - r'^.+简介:', '', get_element_by_id("intro", webpage).strip()) + category_desc = remove_start( + get_element_by_id("intro", webpage).strip(), + '%s简介:' % category_name) jsonm = self._parse_json(self._html_search_regex( - r'var jsonm = (\{.+?\});', webpage, 'category songs'), category_id) + r'var\s+jsonm\s*=\s*([^;]+);', webpage, 'category songs'), category_id) entries = [ self.url_result( @@ -289,7 +292,7 @@ def _real_extract(self, url): errnote='Unable to get mv detail info: %s' % song_id) mobj = re.search( - r'

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

', + r']+title="(?P[^"]+)">[^<]+]+title="(?P[^"]+)"', webpage) if mobj: song_name = mobj.group('song') diff --git a/youtube_dl/extractor/neteasemusic.py b/youtube_dl/extractor/neteasemusic.py index bdfe7e63f..ee52efaee 100644 --- a/youtube_dl/extractor/neteasemusic.py +++ b/youtube_dl/extractor/neteasemusic.py @@ -229,7 +229,7 @@ def _real_extract(self, url): if info['artist']['trans']: name = '%s - %s' % (name, info['artist']['trans']) if info['artist']['alias']: - name = '%s - %s' % (name, ";".join(info['artist']['alias'])) + name = '%s - %s' % (name, ';'.join(info['artist']['alias'])) entries = [ self.url_result('http://music.163.com/#/song?id=%s' % song['id'],