From fe7710cbccdade0b66ef48a7e2eedad71a0702cc Mon Sep 17 00:00:00 2001 From: Yen Chi Hsuan Date: Tue, 27 Jan 2015 23:55:22 +0800 Subject: [PATCH 1/4] [xuite] Add new extractor --- youtube_dl/extractor/__init__.py | 1 + youtube_dl/extractor/xuite.py | 147 +++++++++++++++++++++++++++++++ 2 files changed, 148 insertions(+) create mode 100644 youtube_dl/extractor/xuite.py diff --git a/youtube_dl/extractor/__init__.py b/youtube_dl/extractor/__init__.py index 03c56156a..09070daa4 100644 --- a/youtube_dl/extractor/__init__.py +++ b/youtube_dl/extractor/__init__.py @@ -546,6 +546,7 @@ from .xnxx import XNXXIE from .xvideos import XVideosIE from .xtube import XTubeUserIE, XTubeIE +from .xuite import XuiteIE from .xxxymovies import XXXYMoviesIE from .yahoo import ( YahooIE, diff --git a/youtube_dl/extractor/xuite.py b/youtube_dl/extractor/xuite.py new file mode 100644 index 000000000..943757c27 --- /dev/null +++ b/youtube_dl/extractor/xuite.py @@ -0,0 +1,147 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +import base64 +from .common import InfoExtractor +from ..compat import ( + compat_urlparse, + compat_urllib_parse_unquote, + compat_parse_qs +) +from ..utils import ( + ExtractorError, + parse_iso8601, + parse_duration +) + +# ref: http://stackoverflow.com/questions/475074/regex-to-parse-or-validate-base64-data +REGEX_BASE64 = r'(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?' + +CST_ZONE = +8 # China Standard Time + + +class XuiteIE(InfoExtractor): + _VALID_URL = r'http://vlog.xuite.net/play/(?P%s)(/.*)?' % REGEX_BASE64 + _TESTS = [{ + # Audio + 'url': 'http://vlog.xuite.net/play/RGkzc1ZULTM4NjA5MTQuZmx2', + 'md5': '63a42c705772aa53fd4c1a0027f86adf', + 'info_dict': { + 'id': 'RGkzc1ZULTM4NjA5MTQuZmx2', + 'ext': 'mp3', + 'upload_date': '20110902', + 'uploader_id': '15973816', + 'uploader': '阿能', + 'timestamp': 1314932940, + 'title': '孤單南半球-歐德陽' + } + }, { + # Audio with alternative form of url + 'url': 'http://vlog.xuite.net/play/S1dDUjdyLTMyOTc3NjcuZmx2/%E5%AD%AB%E7%87%95%E5%A7%BF-%E7%9C%BC%E6%B7%9A%E6%88%90%E8%A9%A9', + 'md5': 'c91645f324de53d82ebb80930e1a73d2', + 'info_dict': { + 'id': 'S1dDUjdyLTMyOTc3NjcuZmx2', + 'ext': 'mp3', + 'upload_date': '20101226', + 'uploader_id': '10102699', + 'uploader': '蠍', + 'timestamp': 1293367080, + 'title': '孫燕姿-眼淚成詩', + } + }, { + # Video with only one format + 'url': 'http://vlog.xuite.net/play/TkRZNjhULTM0NDE2MjkuZmx2', + 'md5': 'c45737fc8ac5dc8ac2f92ecbcecf505e', + 'info_dict': { + 'id': 'TkRZNjhULTM0NDE2MjkuZmx2', + 'ext': 'mp4', + 'upload_date': '20110306', + 'uploader_id': '10400126', + 'uploader': 'Valen', + 'timestamp': 1299383640, + 'title': '孫燕姿 - 眼淚成詩', + } + }, { + # Video with two formats + 'url': 'http://vlog.xuite.net/play/bWo1N1pLLTIxMzAxMTcwLmZsdg==', + 'md5': '1166e0f461efe55b62e26a2d2a68e6de', + 'info_dict': { + 'id': 'bWo1N1pLLTIxMzAxMTcwLmZsdg==', + 'ext': 'mp4', + 'upload_date': '20150117', + 'uploader_id': '242127761', + 'uploader': '我只是想認真點', + 'timestamp': 1421481240, + 'title': '暗殺教室 02', + } + }] + + def _flv_config(self, media_id): + base64_media_id = base64.b64encode(media_id.encode('utf-8')).decode('utf-8') + flv_config_url = 'http://vlog.xuite.net/flash/player?media=' + base64_media_id + flv_config = self._download_xml(flv_config_url, 'flv config') + + prop_dict = {} + for prop in flv_config.findall('./property'): + prop_id = base64.b64decode(prop.attrib['id']).decode('utf-8') + + if not prop.text: + continue # CDATA may be empty in flv config + + encoded_content = base64.b64decode(prop.text).decode('utf-8') + prop_dict[prop_id] = compat_urllib_parse_unquote(encoded_content) + + return prop_dict + + def _type_string(self, media_url): + query_string = compat_urlparse.urlparse(media_url).query + type_string = compat_parse_qs(query_string)['q'][0] + return type_string + + def _guess_ext(self, media_url): + type_string = self._type_string(media_url) + if type_string == 'mp3': + return 'mp3' + elif type_string == '360' or type_string == '720': + return 'mp4' + else: + raise ExtractorError('Unknown type string %s' % type_string) + + def _real_extract(self, url): + video_id = self._match_id(url) + + page = self._download_webpage(url, video_id) + media_id = self._html_search_regex(r'data-mediaid="(\d+)"', page, 'media id') + flv_config = self._flv_config(media_id) + + timestamp_local = parse_iso8601(flv_config['publish_datetime'], ' ') + timestamp_gmt = timestamp_local - CST_ZONE * 3600 + + ret_attrs = { + 'id': video_id, + 'title': flv_config['title'], + 'thumbnail': flv_config['thumb'], + 'uploader': flv_config['author_name'], + 'timestamp': timestamp_gmt, + 'uploader_id': flv_config['author_id'], + 'duration': parse_duration(flv_config['duration']), + 'categories': [flv_config['category']] + } + + if 'hq_src' in flv_config: + src = flv_config['src'] + src_hq = flv_config['hq_src'] + ret_attrs['formats'] = [{ + 'url': src, + 'ext': self._guess_ext(src), + 'format_id': self._type_string(src) + }, { + 'url': src_hq, + 'ext': self._guess_ext(src_hq), + 'format_id': self._type_string(src_hq) + }] + else: + ret_attrs['url'] = flv_config['src'] + ret_attrs['ext'] = self._guess_ext(flv_config['src']) + + return ret_attrs From 6348ad12a057cc1c454488ce7c37070cf39a8f06 Mon Sep 17 00:00:00 2001 From: Yen Chi Hsuan Date: Wed, 28 Jan 2015 00:13:40 +0800 Subject: [PATCH 2/4] [xuite] Add height information for the two formats --- youtube_dl/extractor/xuite.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/youtube_dl/extractor/xuite.py b/youtube_dl/extractor/xuite.py index 943757c27..aef9a14a3 100644 --- a/youtube_dl/extractor/xuite.py +++ b/youtube_dl/extractor/xuite.py @@ -129,17 +129,17 @@ def _real_extract(self, url): } if 'hq_src' in flv_config: - src = flv_config['src'] - src_hq = flv_config['hq_src'] - ret_attrs['formats'] = [{ - 'url': src, - 'ext': self._guess_ext(src), - 'format_id': self._type_string(src) - }, { - 'url': src_hq, - 'ext': self._guess_ext(src_hq), - 'format_id': self._type_string(src_hq) - }] + urls = [flv_config['src'], flv_config['hq_src']] + + ret_attrs['formats'] = [] + + for url in urls: + ret_attrs['formats'].append({ + 'url': url, + 'ext': self._guess_ext(url), + 'format_id': self._type_string(url), + 'height': int(self._type_string(url)) + }) else: ret_attrs['url'] = flv_config['src'] ret_attrs['ext'] = self._guess_ext(flv_config['src']) From a28383834b07787b22a837bd6028bf24bea7ec94 Mon Sep 17 00:00:00 2001 From: Yen Chi Hsuan Date: Wed, 28 Jan 2015 01:30:14 +0800 Subject: [PATCH 3/4] [xuite] Update tests --- youtube_dl/extractor/xuite.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/youtube_dl/extractor/xuite.py b/youtube_dl/extractor/xuite.py index aef9a14a3..8466d4bc5 100644 --- a/youtube_dl/extractor/xuite.py +++ b/youtube_dl/extractor/xuite.py @@ -33,7 +33,10 @@ class XuiteIE(InfoExtractor): 'uploader_id': '15973816', 'uploader': '阿能', 'timestamp': 1314932940, - 'title': '孤單南半球-歐德陽' + 'title': '孤單南半球-歐德陽', + 'thumbnail': 're:^https?://.*\.jpg$', + 'categories': ['個人短片'], + 'duration': 247.246 } }, { # Audio with alternative form of url @@ -47,6 +50,9 @@ class XuiteIE(InfoExtractor): 'uploader': '蠍', 'timestamp': 1293367080, 'title': '孫燕姿-眼淚成詩', + 'thumbnail': 're:^https?://.*\.jpg$', + 'categories': ['個人短片'], + 'duration': 223.19 } }, { # Video with only one format @@ -60,6 +66,9 @@ class XuiteIE(InfoExtractor): 'uploader': 'Valen', 'timestamp': 1299383640, 'title': '孫燕姿 - 眼淚成詩', + 'thumbnail': 're:^https?://.*\.jpg$', + 'categories': ['影視娛樂'], + 'duration': 217.399 } }, { # Video with two formats @@ -73,6 +82,9 @@ class XuiteIE(InfoExtractor): 'uploader': '我只是想認真點', 'timestamp': 1421481240, 'title': '暗殺教室 02', + 'thumbnail': 're:^https?://.*\.jpg$', + 'categories': ['電玩動漫'], + 'duration': 1384.907 } }] From affd04a45dcc46f21b55e4d9419812c33b9aa9ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergey=20M=E2=80=A4?= Date: Thu, 29 Jan 2015 22:09:59 +0600 Subject: [PATCH 4/4] [xuite] Simplify and improve --- youtube_dl/extractor/xuite.py | 181 +++++++++++++++------------------- 1 file changed, 82 insertions(+), 99 deletions(-) diff --git a/youtube_dl/extractor/xuite.py b/youtube_dl/extractor/xuite.py index 8466d4bc5..a9dbf8c2d 100644 --- a/youtube_dl/extractor/xuite.py +++ b/youtube_dl/extractor/xuite.py @@ -2,158 +2,141 @@ from __future__ import unicode_literals import base64 + from .common import InfoExtractor -from ..compat import ( - compat_urlparse, - compat_urllib_parse_unquote, - compat_parse_qs -) +from ..compat import compat_urllib_parse_unquote from ..utils import ( ExtractorError, parse_iso8601, - parse_duration + parse_duration, ) -# ref: http://stackoverflow.com/questions/475074/regex-to-parse-or-validate-base64-data -REGEX_BASE64 = r'(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?' - -CST_ZONE = +8 # China Standard Time - class XuiteIE(InfoExtractor): - _VALID_URL = r'http://vlog.xuite.net/play/(?P%s)(/.*)?' % REGEX_BASE64 + _REGEX_BASE64 = r'(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?' + _VALID_URL = r'http://vlog.xuite.net/(?:play|embed)/(?P%s)' % _REGEX_BASE64 _TESTS = [{ # Audio 'url': 'http://vlog.xuite.net/play/RGkzc1ZULTM4NjA5MTQuZmx2', 'md5': '63a42c705772aa53fd4c1a0027f86adf', 'info_dict': { - 'id': 'RGkzc1ZULTM4NjA5MTQuZmx2', + 'id': '3860914', 'ext': 'mp3', - 'upload_date': '20110902', - 'uploader_id': '15973816', - 'uploader': '阿能', - 'timestamp': 1314932940, 'title': '孤單南半球-歐德陽', 'thumbnail': 're:^https?://.*\.jpg$', + 'duration': 247.246, + 'timestamp': 1314932940, + 'upload_date': '20110902', + 'uploader': '阿能', + 'uploader_id': '15973816', 'categories': ['個人短片'], - 'duration': 247.246 - } - }, { - # Audio with alternative form of url - 'url': 'http://vlog.xuite.net/play/S1dDUjdyLTMyOTc3NjcuZmx2/%E5%AD%AB%E7%87%95%E5%A7%BF-%E7%9C%BC%E6%B7%9A%E6%88%90%E8%A9%A9', - 'md5': 'c91645f324de53d82ebb80930e1a73d2', - 'info_dict': { - 'id': 'S1dDUjdyLTMyOTc3NjcuZmx2', - 'ext': 'mp3', - 'upload_date': '20101226', - 'uploader_id': '10102699', - 'uploader': '蠍', - 'timestamp': 1293367080, - 'title': '孫燕姿-眼淚成詩', - 'thumbnail': 're:^https?://.*\.jpg$', - 'categories': ['個人短片'], - 'duration': 223.19 - } + }, }, { # Video with only one format 'url': 'http://vlog.xuite.net/play/TkRZNjhULTM0NDE2MjkuZmx2', 'md5': 'c45737fc8ac5dc8ac2f92ecbcecf505e', 'info_dict': { - 'id': 'TkRZNjhULTM0NDE2MjkuZmx2', + 'id': '3441629', 'ext': 'mp4', - 'upload_date': '20110306', - 'uploader_id': '10400126', - 'uploader': 'Valen', - 'timestamp': 1299383640, 'title': '孫燕姿 - 眼淚成詩', 'thumbnail': 're:^https?://.*\.jpg$', + 'duration': 217.399, + 'timestamp': 1299383640, + 'upload_date': '20110306', + 'uploader': 'Valen', + 'uploader_id': '10400126', 'categories': ['影視娛樂'], - 'duration': 217.399 - } + }, }, { # Video with two formats 'url': 'http://vlog.xuite.net/play/bWo1N1pLLTIxMzAxMTcwLmZsdg==', 'md5': '1166e0f461efe55b62e26a2d2a68e6de', 'info_dict': { - 'id': 'bWo1N1pLLTIxMzAxMTcwLmZsdg==', + 'id': '21301170', 'ext': 'mp4', - 'upload_date': '20150117', - 'uploader_id': '242127761', - 'uploader': '我只是想認真點', - 'timestamp': 1421481240, 'title': '暗殺教室 02', + 'description': '字幕:【極影字幕社】', 'thumbnail': 're:^https?://.*\.jpg$', + 'duration': 1384.907, + 'timestamp': 1421481240, + 'upload_date': '20150117', + 'uploader': '我只是想認真點', + 'uploader_id': '242127761', 'categories': ['電玩動漫'], - 'duration': 1384.907 - } + }, + }, { + 'url': 'http://vlog.xuite.net/play/S1dDUjdyLTMyOTc3NjcuZmx2/%E5%AD%AB%E7%87%95%E5%A7%BF-%E7%9C%BC%E6%B7%9A%E6%88%90%E8%A9%A9', + 'only_matching': True, }] - def _flv_config(self, media_id): + def _extract_flv_config(self, media_id): base64_media_id = base64.b64encode(media_id.encode('utf-8')).decode('utf-8') - flv_config_url = 'http://vlog.xuite.net/flash/player?media=' + base64_media_id - flv_config = self._download_xml(flv_config_url, 'flv config') - + flv_config = self._download_xml( + 'http://vlog.xuite.net/flash/player?media=%s' % base64_media_id, + 'flv config') prop_dict = {} for prop in flv_config.findall('./property'): prop_id = base64.b64decode(prop.attrib['id']).decode('utf-8') - + # CDATA may be empty in flv config if not prop.text: - continue # CDATA may be empty in flv config - + continue encoded_content = base64.b64decode(prop.text).decode('utf-8') prop_dict[prop_id] = compat_urllib_parse_unquote(encoded_content) - return prop_dict - def _type_string(self, media_url): - query_string = compat_urlparse.urlparse(media_url).query - type_string = compat_parse_qs(query_string)['q'][0] - return type_string - - def _guess_ext(self, media_url): - type_string = self._type_string(media_url) - if type_string == 'mp3': - return 'mp3' - elif type_string == '360' or type_string == '720': - return 'mp4' - else: - raise ExtractorError('Unknown type string %s' % type_string) - def _real_extract(self, url): video_id = self._match_id(url) - page = self._download_webpage(url, video_id) - media_id = self._html_search_regex(r'data-mediaid="(\d+)"', page, 'media id') - flv_config = self._flv_config(media_id) + webpage = self._download_webpage(url, video_id) - timestamp_local = parse_iso8601(flv_config['publish_datetime'], ' ') - timestamp_gmt = timestamp_local - CST_ZONE * 3600 + error_msg = self._search_regex( + r'
([^<]+)', + webpage, 'error message', default=None) + if error_msg: + raise ExtractorError( + '%s returned error: %s' % (self.IE_NAME, error_msg), + expected=True) - ret_attrs = { - 'id': video_id, - 'title': flv_config['title'], - 'thumbnail': flv_config['thumb'], - 'uploader': flv_config['author_name'], - 'timestamp': timestamp_gmt, - 'uploader_id': flv_config['author_id'], - 'duration': parse_duration(flv_config['duration']), - 'categories': [flv_config['category']] + video_id = self._html_search_regex( + r'data-mediaid="(\d+)"', webpage, 'media id') + flv_config = self._extract_flv_config(video_id) + + FORMATS = { + 'audio': 'mp3', + 'video': 'mp4', } - if 'hq_src' in flv_config: - urls = [flv_config['src'], flv_config['hq_src']] + formats = [] + for format_tag in ('src', 'hq_src'): + video_url = flv_config.get(format_tag) + if not video_url: + continue + format_id = self._search_regex( + r'\bq=(.+?)\b', video_url, 'format id', default=format_tag) + formats.append({ + 'url': video_url, + 'ext': FORMATS.get(flv_config['type'], 'mp4'), + 'format_id': format_id, + 'height': int(format_id) if format_id.isnumeric() else None, + }) + self._sort_formats(formats) - ret_attrs['formats'] = [] + timestamp = flv_config.get('publish_datetime') + if timestamp: + timestamp = parse_iso8601(timestamp + ' +0800', ' ') - for url in urls: - ret_attrs['formats'].append({ - 'url': url, - 'ext': self._guess_ext(url), - 'format_id': self._type_string(url), - 'height': int(self._type_string(url)) - }) - else: - ret_attrs['url'] = flv_config['src'] - ret_attrs['ext'] = self._guess_ext(flv_config['src']) + category = flv_config.get('category') + categories = [category] if category else [] - return ret_attrs + return { + 'id': video_id, + 'title': flv_config['title'], + 'description': flv_config.get('description'), + 'thumbnail': flv_config.get('thumb'), + 'timestamp': timestamp, + 'uploader': flv_config.get('author_name'), + 'uploader_id': flv_config.get('author_id'), + 'duration': parse_duration(flv_config.get('duration')), + 'categories': categories, + 'formats': formats, + }