From 012cd9364fdc61edce68b12bc034e14ba79b7f4c Mon Sep 17 00:00:00 2001 From: Yen Chi Hsuan Date: Tue, 17 Mar 2015 19:03:29 +0800 Subject: [PATCH 1/6] [QQMusic] Add new extractor --- youtube_dl/extractor/__init__.py | 1 + youtube_dl/extractor/qqmusic.py | 56 ++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 youtube_dl/extractor/qqmusic.py diff --git a/youtube_dl/extractor/__init__.py b/youtube_dl/extractor/__init__.py index 1bb3e1a1c..f2e6dd5c8 100644 --- a/youtube_dl/extractor/__init__.py +++ b/youtube_dl/extractor/__init__.py @@ -384,6 +384,7 @@ from .promptfile import PromptFileIE from .prosiebensat1 import ProSiebenSat1IE from .puls4 import Puls4IE from .pyvideo import PyvideoIE +from .qqmusic import QQMusicIE from .quickvid import QuickVidIE from .r7 import R7IE from .radiode import RadioDeIE diff --git a/youtube_dl/extractor/qqmusic.py b/youtube_dl/extractor/qqmusic.py new file mode 100644 index 000000000..3dc637392 --- /dev/null +++ b/youtube_dl/extractor/qqmusic.py @@ -0,0 +1,56 @@ +# coding: utf-8 +from __future__ import unicode_literals + +from .common import InfoExtractor +from ..utils import strip_jsonp + +# guid is a random number generated in javascript, but seems a fixed number +# also works +guid = '1' + + +class QQMusicIE(InfoExtractor): + _VALID_URL = r'http://y.qq.com/#type=song&mid=(?P[0-9A-Za-z]+)' + _TESTS = [{ + 'url': 'http://y.qq.com/#type=song&mid=004295Et37taLD', + 'md5': 'bed90b6db2a7a7a7e11bc585f471f63a', + 'info_dict': { + 'id': '004295Et37taLD', + 'ext': 'm4a', + 'title': '可惜没如果', + 'upload_date': '20141227', + 'creator': '林俊杰', + } + }] + + def _real_extract(self, url): + mid = self._match_id(url) + + detail_info_page = self._download_webpage( + 'http://s.plcloud.music.qq.com/fcgi-bin/fcg_yqq_song_detail_info.fcg?songmid=%s&play=0' % mid, + mid, note='Download sont detail info', + errnote='Unable to get song detail info') + + song_name = self._html_search_regex( + r"songname:\s*'([^']+)'", detail_info_page, 'song name') + + publish_time = self._html_search_regex( + r'发行时间:(\d{4}-\d{2}-\d{2})', detail_info_page, + 'publish time').replace('-', '') + + singer = self._html_search_regex( + r"singer:\s*'([^']+)", detail_info_page, 'singer') + + vkey = self._download_json( + 'http://base.music.qq.com/fcgi-bin/fcg_musicexpress.fcg?json=3&guid=%s' % guid, + mid, note='Retrieve vkey', errnote='Unable to get vkey', + transform_source=strip_jsonp)['key'] + song_url = 'http://cc.stream.qqmusic.qq.com/C200%s.m4a?vkey=%s&guid=%s&fromtag=0' % (mid, vkey, guid) + + return { + 'id': mid, + 'url': song_url, + 'title': song_name, + 'upload_date': publish_time, + 'creator': singer, + } From c1a3464b1c102e7475c05e1ff2c7d7fb69318d8c Mon Sep 17 00:00:00 2001 From: Yen Chi Hsuan Date: Wed, 18 Mar 2015 13:56:02 +0800 Subject: [PATCH 2/6] [QQMusic] Implement the guid algorithm --- youtube_dl/extractor/qqmusic.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/youtube_dl/extractor/qqmusic.py b/youtube_dl/extractor/qqmusic.py index 3dc637392..93440b954 100644 --- a/youtube_dl/extractor/qqmusic.py +++ b/youtube_dl/extractor/qqmusic.py @@ -1,13 +1,12 @@ # coding: utf-8 from __future__ import unicode_literals +import random +import time + from .common import InfoExtractor from ..utils import strip_jsonp -# guid is a random number generated in javascript, but seems a fixed number -# also works -guid = '1' - class QQMusicIE(InfoExtractor): _VALID_URL = r'http://y.qq.com/#type=song&mid=(?P[0-9A-Za-z]+)' @@ -23,6 +22,13 @@ class QQMusicIE(InfoExtractor): } }] + # Reference: m_r_GetRUin() in top_player.js + # http://imgcache.gtimg.cn/music/portal_v3/y/top_player.js + @staticmethod + def m_r_get_ruin(): + curMs = int(time.time() * 1000) % 1000 + return int(round(random.random() * 2147483647) * curMs % 1E10) + def _real_extract(self, url): mid = self._match_id(url) @@ -41,6 +47,8 @@ class QQMusicIE(InfoExtractor): singer = self._html_search_regex( r"singer:\s*'([^']+)", detail_info_page, 'singer') + guid = self.m_r_get_ruin() + vkey = self._download_json( 'http://base.music.qq.com/fcgi-bin/fcg_musicexpress.fcg?json=3&guid=%s' % guid, mid, note='Retrieve vkey', errnote='Unable to get vkey', From 16b0b7827f3e2892c90d6d35e5be72fdea360dfc Mon Sep 17 00:00:00 2001 From: Yen Chi Hsuan Date: Wed, 18 Mar 2015 14:59:33 +0800 Subject: [PATCH 3/6] [QQMusic] Add singer info extractor --- youtube_dl/extractor/__init__.py | 5 ++- youtube_dl/extractor/qqmusic.py | 53 +++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/youtube_dl/extractor/__init__.py b/youtube_dl/extractor/__init__.py index f2e6dd5c8..e83d8bb46 100644 --- a/youtube_dl/extractor/__init__.py +++ b/youtube_dl/extractor/__init__.py @@ -384,7 +384,10 @@ from .promptfile import PromptFileIE from .prosiebensat1 import ProSiebenSat1IE from .puls4 import Puls4IE from .pyvideo import PyvideoIE -from .qqmusic import QQMusicIE +from .qqmusic import ( + QQMusicIE, + QQMusicSingerIE +) from .quickvid import QuickVidIE from .r7 import R7IE from .radiode import RadioDeIE diff --git a/youtube_dl/extractor/qqmusic.py b/youtube_dl/extractor/qqmusic.py index 93440b954..1c22943a5 100644 --- a/youtube_dl/extractor/qqmusic.py +++ b/youtube_dl/extractor/qqmusic.py @@ -3,9 +3,11 @@ from __future__ import unicode_literals import random import time +import re from .common import InfoExtractor from ..utils import strip_jsonp +from ..compat import compat_urllib_request class QQMusicIE(InfoExtractor): @@ -34,7 +36,7 @@ class QQMusicIE(InfoExtractor): detail_info_page = self._download_webpage( 'http://s.plcloud.music.qq.com/fcgi-bin/fcg_yqq_song_detail_info.fcg?songmid=%s&play=0' % mid, - mid, note='Download sont detail info', + mid, note='Download song detail info', errnote='Unable to get song detail info') song_name = self._html_search_regex( @@ -62,3 +64,52 @@ class QQMusicIE(InfoExtractor): 'upload_date': publish_time, 'creator': singer, } + + +class QQMusicSingerIE(InfoExtractor): + _VALID_URL = r'http://y.qq.com/#type=singer&mid=(?P[0-9A-Za-z]+)' + _TEST = { + 'url': 'http://y.qq.com/#type=singer&mid=001BLpXF2DyJe2', + 'info_dict': { + 'id': '001BLpXF2DyJe2', + 'title': '林俊杰', + 'description': 'md5:2a222d89ba4455a3af19940c0481bb78', + }, + 'playlist_count': 12, + } + + def _real_extract(self, url): + mid = self._match_id(url) + + singer_page = self._download_webpage( + 'http://y.qq.com/y/static/singer/%s/%s/%s.html' % (mid[-2], mid[-1], mid), + 'Download singer page') + + entries = [] + + for item in re.findall(r'([^<>]+)', singer_page): + song_mid = item.split('|')[-5] + entries.append(self.url_result( + 'http://y.qq.com/#type=song&mid=' + song_mid, 'QQMusic', song_mid)) + + singer_name = self._html_search_regex( + r"singername\s*:\s*'([^']+)'", singer_page, 'singer name', + default=None) + + singer_id = self._html_search_regex( + r"singerid\s*:\s*'([0-9]+)'", singer_page, 'singer id', + default=None) + + singer_desc = None + + if singer_id: + req = compat_urllib_request.Request( + 'http://s.plcloud.music.qq.com/fcgi-bin/fcg_get_singer_desc.fcg?utf8=1&outCharset=utf-8&format=xml&singerid=%s' % singer_id) + req.add_header( + 'Referer', 'http://s.plcloud.music.qq.com/xhr_proxy_utf8.html') + singer_desc_page = self._download_xml( + req, 'Donwload singer description XML') + + singer_desc = singer_desc_page.find('./data/info/desc').text + + return self.playlist_result(entries, mid, singer_name, singer_desc) From 5e75c3a88633c2675d96c94127ac1fc881b85435 Mon Sep 17 00:00:00 2001 From: Yen Chi Hsuan Date: Thu, 19 Mar 2015 01:47:07 +0800 Subject: [PATCH 4/6] [QQMusic] Add album info extractor --- youtube_dl/extractor/__init__.py | 3 +- youtube_dl/extractor/qqmusic.py | 72 +++++++++++++++++++++++++++----- 2 files changed, 63 insertions(+), 12 deletions(-) diff --git a/youtube_dl/extractor/__init__.py b/youtube_dl/extractor/__init__.py index e83d8bb46..4fa002a32 100644 --- a/youtube_dl/extractor/__init__.py +++ b/youtube_dl/extractor/__init__.py @@ -386,7 +386,8 @@ from .puls4 import Puls4IE from .pyvideo import PyvideoIE from .qqmusic import ( QQMusicIE, - QQMusicSingerIE + QQMusicSingerIE, + QQMusicAlbumIE, ) from .quickvid import QuickVidIE from .r7 import R7IE diff --git a/youtube_dl/extractor/qqmusic.py b/youtube_dl/extractor/qqmusic.py index 1c22943a5..d0ea4a769 100644 --- a/youtube_dl/extractor/qqmusic.py +++ b/youtube_dl/extractor/qqmusic.py @@ -6,7 +6,10 @@ import time import re from .common import InfoExtractor -from ..utils import strip_jsonp +from ..utils import ( + strip_jsonp, + unescapeHTML, +) from ..compat import compat_urllib_request @@ -66,7 +69,28 @@ class QQMusicIE(InfoExtractor): } -class QQMusicSingerIE(InfoExtractor): +class QQPlaylistBaseIE(InfoExtractor): + @staticmethod + def qq_static_url(category, mid): + return 'http://y.qq.com/y/static/%s/%s/%s/%s.html' % (category, mid[-2], mid[-1], mid) + + @staticmethod + def qq_song_url(mid): + return 'http://y.qq.com/#type=song&mid=%s' % mid + + @classmethod + def get_entries_from_page(cls, page): + entries = [] + + for item in re.findall(r'class="data"[^<>]*>([^<>]+)[0-9A-Za-z]+)' _TEST = { 'url': 'http://y.qq.com/#type=singer&mid=001BLpXF2DyJe2', @@ -82,15 +106,9 @@ class QQMusicSingerIE(InfoExtractor): mid = self._match_id(url) singer_page = self._download_webpage( - 'http://y.qq.com/y/static/singer/%s/%s/%s.html' % (mid[-2], mid[-1], mid), - 'Download singer page') + self.qq_static_url('singer', mid), mid, 'Download singer page') - entries = [] - - for item in re.findall(r'([^<>]+)', singer_page): - song_mid = item.split('|')[-5] - entries.append(self.url_result( - 'http://y.qq.com/#type=song&mid=' + song_mid, 'QQMusic', song_mid)) + entries = self.get_entries_from_page(singer_page) singer_name = self._html_search_regex( r"singername\s*:\s*'([^']+)'", singer_page, 'singer name', @@ -108,8 +126,40 @@ class QQMusicSingerIE(InfoExtractor): req.add_header( 'Referer', 'http://s.plcloud.music.qq.com/xhr_proxy_utf8.html') singer_desc_page = self._download_xml( - req, 'Donwload singer description XML') + req, mid, 'Donwload singer description XML') singer_desc = singer_desc_page.find('./data/info/desc').text return self.playlist_result(entries, mid, singer_name, singer_desc) + + +class QQMusicAlbumIE(QQPlaylistBaseIE): + _VALID_URL = r'http://y.qq.com/#type=album&mid=(?P[0-9A-Za-z]+)' + + _TEST = { + 'url': 'http://y.qq.com/#type=album&mid=000gXCTb2AhRR1&play=0', + 'info_dict': { + 'id': '000gXCTb2AhRR1', + 'title': '我们都是这样长大的', + 'description': 'md5:d216c55a2d4b3537fe4415b8767d74d6', + }, + 'playlist_count': 4, + } + + def _real_extract(self, url): + mid = self._match_id(url) + + album_page = self._download_webpage( + self.qq_static_url('album', mid), mid, 'Download album page') + + entries = self.get_entries_from_page(album_page) + + album_name = self._html_search_regex( + r"albumname\s*:\s*'([^']+)',", album_page, 'album name', + default=None) + + album_detail = self._html_search_regex( + r'
\s*

((?:[^<>]+(?:
)?)+)

', + album_page, 'album details', default=None) + + return self.playlist_result(entries, mid, album_name, album_detail) From c971b7efdd6708b1f27573bd5aebbf9fa836396d Mon Sep 17 00:00:00 2001 From: Yen Chi Hsuan Date: Sat, 21 Mar 2015 11:38:53 +0800 Subject: [PATCH 5/6] [QQMusic] Song extractor: Add lyrics as description Note: Test fails on python 3 due to encoding issues --- youtube_dl/extractor/qqmusic.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/youtube_dl/extractor/qqmusic.py b/youtube_dl/extractor/qqmusic.py index d0ea4a769..e8aacbc3d 100644 --- a/youtube_dl/extractor/qqmusic.py +++ b/youtube_dl/extractor/qqmusic.py @@ -24,6 +24,7 @@ class QQMusicIE(InfoExtractor): 'title': '可惜没如果', 'upload_date': '20141227', 'creator': '林俊杰', + 'description': 'md5:242c97c2847e0495583b7b13764f7106', } }] @@ -47,10 +48,16 @@ class QQMusicIE(InfoExtractor): publish_time = self._html_search_regex( r'发行时间:(\d{4}-\d{2}-\d{2})', detail_info_page, - 'publish time').replace('-', '') + 'publish time', default=None) + if publish_time: + publish_time = publish_time.replace('-', '') singer = self._html_search_regex( - r"singer:\s*'([^']+)", detail_info_page, 'singer') + r"singer:\s*'([^']+)", detail_info_page, 'singer', default=None) + + lrc_content = self._html_search_regex( + r'
]*>([^<>]+)
', + detail_info_page, 'LRC lyrics', default=None) guid = self.m_r_get_ruin() @@ -66,6 +73,7 @@ class QQMusicIE(InfoExtractor): 'title': song_name, 'upload_date': publish_time, 'creator': singer, + 'description': lrc_content, } @@ -74,10 +82,6 @@ class QQPlaylistBaseIE(InfoExtractor): def qq_static_url(category, mid): return 'http://y.qq.com/y/static/%s/%s/%s/%s.html' % (category, mid[-2], mid[-1], mid) - @staticmethod - def qq_song_url(mid): - return 'http://y.qq.com/#type=song&mid=%s' % mid - @classmethod def get_entries_from_page(cls, page): entries = [] @@ -85,7 +89,8 @@ class QQPlaylistBaseIE(InfoExtractor): for item in re.findall(r'class="data"[^<>]*>([^<>]+) Date: Sat, 21 Mar 2015 12:21:27 +0800 Subject: [PATCH 6/6] [extractor/common] Add the encoding parameter The QQMusic info extractor need forced encoding for correct working. --- youtube_dl/extractor/common.py | 34 ++++++++++++++++++++------------- youtube_dl/extractor/qqmusic.py | 4 ++-- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/youtube_dl/extractor/common.py b/youtube_dl/extractor/common.py index e5245ec3f..087af0fe9 100644 --- a/youtube_dl/extractor/common.py +++ b/youtube_dl/extractor/common.py @@ -324,7 +324,7 @@ class InfoExtractor(object): self._downloader.report_warning(errmsg) return False - def _download_webpage_handle(self, url_or_request, video_id, note=None, errnote=None, fatal=True): + def _download_webpage_handle(self, url_or_request, video_id, note=None, errnote=None, fatal=True, encoding=None): """ Returns a tuple (page content as string, URL handle) """ # Strip hashes from the URL (#1038) if isinstance(url_or_request, (compat_str, str)): @@ -334,14 +334,11 @@ class InfoExtractor(object): if urlh is False: assert not fatal return False - content = self._webpage_read_content(urlh, url_or_request, video_id, note, errnote, fatal) + content = self._webpage_read_content(urlh, url_or_request, video_id, note, errnote, fatal, encoding=encoding) return (content, urlh) - def _webpage_read_content(self, urlh, url_or_request, video_id, note=None, errnote=None, fatal=True, prefix=None): - content_type = urlh.headers.get('Content-Type', '') - webpage_bytes = urlh.read() - if prefix is not None: - webpage_bytes = prefix + webpage_bytes + @staticmethod + def _guess_encoding_from_content(content_type, webpage_bytes): m = re.match(r'[a-zA-Z0-9_.-]+/[a-zA-Z0-9_.-]+\s*;\s*charset=(.+)', content_type) if m: encoding = m.group(1) @@ -354,6 +351,16 @@ class InfoExtractor(object): encoding = 'utf-16' else: encoding = 'utf-8' + + return encoding + + def _webpage_read_content(self, urlh, url_or_request, video_id, note=None, errnote=None, fatal=True, prefix=None, encoding=None): + content_type = urlh.headers.get('Content-Type', '') + webpage_bytes = urlh.read() + if prefix is not None: + webpage_bytes = prefix + webpage_bytes + if not encoding: + encoding = self._guess_encoding_from_content(content_type, webpage_bytes) if self._downloader.params.get('dump_intermediate_pages', False): try: url = url_or_request.get_full_url() @@ -410,13 +417,13 @@ class InfoExtractor(object): return content - def _download_webpage(self, url_or_request, video_id, note=None, errnote=None, fatal=True, tries=1, timeout=5): + def _download_webpage(self, url_or_request, video_id, note=None, errnote=None, fatal=True, tries=1, timeout=5, encoding=None): """ Returns the data of the page as a string """ success = False try_count = 0 while success is False: try: - res = self._download_webpage_handle(url_or_request, video_id, note, errnote, fatal) + res = self._download_webpage_handle(url_or_request, video_id, note, errnote, fatal, encoding=encoding) success = True except compat_http_client.IncompleteRead as e: try_count += 1 @@ -431,10 +438,10 @@ class InfoExtractor(object): def _download_xml(self, url_or_request, video_id, note='Downloading XML', errnote='Unable to download XML', - transform_source=None, fatal=True): + transform_source=None, fatal=True, encoding=None): """Return the xml as an xml.etree.ElementTree.Element""" xml_string = self._download_webpage( - url_or_request, video_id, note, errnote, fatal=fatal) + url_or_request, video_id, note, errnote, fatal=fatal, encoding=encoding) if xml_string is False: return xml_string if transform_source: @@ -445,9 +452,10 @@ class InfoExtractor(object): note='Downloading JSON metadata', errnote='Unable to download JSON metadata', transform_source=None, - fatal=True): + fatal=True, encoding=None): json_string = self._download_webpage( - url_or_request, video_id, note, errnote, fatal=fatal) + url_or_request, video_id, note, errnote, fatal=fatal, + encoding=encoding) if (not fatal) and json_string is False: return None return self._parse_json( diff --git a/youtube_dl/extractor/qqmusic.py b/youtube_dl/extractor/qqmusic.py index e8aacbc3d..174c8e0ae 100644 --- a/youtube_dl/extractor/qqmusic.py +++ b/youtube_dl/extractor/qqmusic.py @@ -24,7 +24,7 @@ class QQMusicIE(InfoExtractor): 'title': '可惜没如果', 'upload_date': '20141227', 'creator': '林俊杰', - 'description': 'md5:242c97c2847e0495583b7b13764f7106', + 'description': 'md5:4348ff1dd24036906baa7b6f973f8d30', } }] @@ -41,7 +41,7 @@ class QQMusicIE(InfoExtractor): detail_info_page = self._download_webpage( 'http://s.plcloud.music.qq.com/fcgi-bin/fcg_yqq_song_detail_info.fcg?songmid=%s&play=0' % mid, mid, note='Download song detail info', - errnote='Unable to get song detail info') + errnote='Unable to get song detail info', encoding='gbk') song_name = self._html_search_regex( r"songname:\s*'([^']+)'", detail_info_page, 'song name')