| 
									
										
										
										
											2015-01-27 23:55:22 +08:00
										 |  |  | # -*- coding: utf-8 -*- | 
					
						
							|  |  |  | from __future__ import unicode_literals | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import base64 | 
					
						
							| 
									
										
										
										
											2015-01-29 22:09:59 +06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-27 23:55:22 +08:00
										 |  |  | from .common import InfoExtractor | 
					
						
							| 
									
										
										
										
											2015-01-29 22:09:59 +06:00
										 |  |  | from ..compat import compat_urllib_parse_unquote | 
					
						
							| 
									
										
										
										
											2015-01-27 23:55:22 +08:00
										 |  |  | from ..utils import ( | 
					
						
							|  |  |  |     ExtractorError, | 
					
						
							|  |  |  |     parse_iso8601, | 
					
						
							| 
									
										
										
										
											2015-01-29 22:09:59 +06:00
										 |  |  |     parse_duration, | 
					
						
							| 
									
										
										
										
											2015-01-27 23:55:22 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class XuiteIE(InfoExtractor): | 
					
						
							| 
									
										
										
										
											2015-07-11 00:52:56 +08:00
										 |  |  |     IE_DESC = '隨意窩Xuite影音' | 
					
						
							| 
									
										
										
										
											2015-01-29 22:09:59 +06:00
										 |  |  |     _REGEX_BASE64 = r'(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?' | 
					
						
							| 
									
										
										
										
											2015-01-29 23:11:13 +06:00
										 |  |  |     _VALID_URL = r'https?://vlog\.xuite\.net/(?:play|embed)/(?P<id>%s)' % _REGEX_BASE64 | 
					
						
							| 
									
										
										
										
											2015-01-27 23:55:22 +08:00
										 |  |  |     _TESTS = [{ | 
					
						
							|  |  |  |         # Audio | 
					
						
							|  |  |  |         'url': 'http://vlog.xuite.net/play/RGkzc1ZULTM4NjA5MTQuZmx2', | 
					
						
							| 
									
										
										
										
											2015-09-08 20:12:30 +01:00
										 |  |  |         'md5': 'e79284c87b371424885448d11f6398c8', | 
					
						
							| 
									
										
										
										
											2015-01-27 23:55:22 +08:00
										 |  |  |         'info_dict': { | 
					
						
							| 
									
										
										
										
											2015-01-29 22:09:59 +06:00
										 |  |  |             'id': '3860914', | 
					
						
							| 
									
										
										
										
											2015-01-27 23:55:22 +08:00
										 |  |  |             'ext': 'mp3', | 
					
						
							| 
									
										
										
										
											2015-01-28 01:30:14 +08:00
										 |  |  |             'title': '孤單南半球-歐德陽', | 
					
						
							|  |  |  |             'thumbnail': 're:^https?://.*\.jpg$', | 
					
						
							| 
									
										
										
										
											2015-01-29 22:09:59 +06:00
										 |  |  |             'duration': 247.246, | 
					
						
							|  |  |  |             'timestamp': 1314932940, | 
					
						
							|  |  |  |             'upload_date': '20110902', | 
					
						
							|  |  |  |             'uploader': '阿能', | 
					
						
							|  |  |  |             'uploader_id': '15973816', | 
					
						
							| 
									
										
										
										
											2015-01-28 01:30:14 +08:00
										 |  |  |             'categories': ['個人短片'], | 
					
						
							| 
									
										
										
										
											2015-01-29 22:09:59 +06:00
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2015-01-27 23:55:22 +08:00
										 |  |  |     }, { | 
					
						
							|  |  |  |         # Video with only one format | 
					
						
							| 
									
										
										
										
											2016-01-31 20:21:57 +08:00
										 |  |  |         'url': 'http://vlog.xuite.net/play/WUxxR2xCLTI1OTI1MDk5LmZsdg==', | 
					
						
							|  |  |  |         'md5': '21f7b39c009b5a4615b4463df6eb7a46', | 
					
						
							| 
									
										
										
										
											2015-01-27 23:55:22 +08:00
										 |  |  |         'info_dict': { | 
					
						
							| 
									
										
										
										
											2016-01-31 20:21:57 +08:00
										 |  |  |             'id': '25925099', | 
					
						
							| 
									
										
										
										
											2015-01-27 23:55:22 +08:00
										 |  |  |             'ext': 'mp4', | 
					
						
							| 
									
										
										
										
											2016-01-31 20:21:57 +08:00
										 |  |  |             'title': 'BigBuckBunny_320x180', | 
					
						
							| 
									
										
										
										
											2015-01-28 01:30:14 +08:00
										 |  |  |             'thumbnail': 're:^https?://.*\.jpg$', | 
					
						
							| 
									
										
										
										
											2016-01-31 20:21:57 +08:00
										 |  |  |             'duration': 596.458, | 
					
						
							|  |  |  |             'timestamp': 1454242500, | 
					
						
							|  |  |  |             'upload_date': '20160131', | 
					
						
							|  |  |  |             'uploader': 'yan12125', | 
					
						
							|  |  |  |             'uploader_id': '12158353', | 
					
						
							|  |  |  |             'categories': ['個人短片'], | 
					
						
							|  |  |  |             'description': 'http://download.blender.org/peach/bigbuckbunny_movies/BigBuckBunny_320x180.mp4', | 
					
						
							| 
									
										
										
										
											2015-01-29 22:09:59 +06:00
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2015-01-27 23:55:22 +08:00
										 |  |  |     }, { | 
					
						
							|  |  |  |         # Video with two formats | 
					
						
							|  |  |  |         'url': 'http://vlog.xuite.net/play/bWo1N1pLLTIxMzAxMTcwLmZsdg==', | 
					
						
							|  |  |  |         'md5': '1166e0f461efe55b62e26a2d2a68e6de', | 
					
						
							|  |  |  |         'info_dict': { | 
					
						
							| 
									
										
										
										
											2015-01-29 22:09:59 +06:00
										 |  |  |             'id': '21301170', | 
					
						
							| 
									
										
										
										
											2015-01-27 23:55:22 +08:00
										 |  |  |             'ext': 'mp4', | 
					
						
							|  |  |  |             'title': '暗殺教室 02', | 
					
						
							| 
									
										
										
										
											2015-01-29 22:09:59 +06:00
										 |  |  |             'description': '字幕:【極影字幕社】', | 
					
						
							| 
									
										
										
										
											2015-01-28 01:30:14 +08:00
										 |  |  |             'thumbnail': 're:^https?://.*\.jpg$', | 
					
						
							| 
									
										
										
										
											2015-01-29 22:09:59 +06:00
										 |  |  |             'duration': 1384.907, | 
					
						
							|  |  |  |             'timestamp': 1421481240, | 
					
						
							|  |  |  |             'upload_date': '20150117', | 
					
						
							|  |  |  |             'uploader': '我只是想認真點', | 
					
						
							|  |  |  |             'uploader_id': '242127761', | 
					
						
							| 
									
										
										
										
											2015-01-28 01:30:14 +08:00
										 |  |  |             'categories': ['電玩動漫'], | 
					
						
							| 
									
										
										
										
											2015-01-29 22:09:59 +06:00
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2016-06-10 12:29:58 +08:00
										 |  |  |         'skip': 'Video removed', | 
					
						
							| 
									
										
										
										
											2015-01-29 22:09:59 +06:00
										 |  |  |     }, { | 
					
						
							|  |  |  |         '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, | 
					
						
							| 
									
										
										
										
											2015-01-27 23:55:22 +08:00
										 |  |  |     }] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-29 20:40:05 +08:00
										 |  |  |     @staticmethod | 
					
						
							|  |  |  |     def base64_decode_utf8(data): | 
					
						
							|  |  |  |         return base64.b64decode(data.encode('utf-8')).decode('utf-8') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @staticmethod | 
					
						
							|  |  |  |     def base64_encode_utf8(data): | 
					
						
							|  |  |  |         return base64.b64encode(data.encode('utf-8')).decode('utf-8') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-29 22:09:59 +06:00
										 |  |  |     def _extract_flv_config(self, media_id): | 
					
						
							| 
									
										
										
										
											2015-03-29 20:40:05 +08:00
										 |  |  |         base64_media_id = self.base64_encode_utf8(media_id) | 
					
						
							| 
									
										
										
										
											2015-01-29 22:09:59 +06:00
										 |  |  |         flv_config = self._download_xml( | 
					
						
							|  |  |  |             'http://vlog.xuite.net/flash/player?media=%s' % base64_media_id, | 
					
						
							|  |  |  |             'flv config') | 
					
						
							| 
									
										
										
										
											2015-01-27 23:55:22 +08:00
										 |  |  |         prop_dict = {} | 
					
						
							|  |  |  |         for prop in flv_config.findall('./property'): | 
					
						
							| 
									
										
										
										
											2015-03-29 20:40:05 +08:00
										 |  |  |             prop_id = self.base64_decode_utf8(prop.attrib['id']) | 
					
						
							| 
									
										
										
										
											2015-01-29 22:09:59 +06:00
										 |  |  |             # CDATA may be empty in flv config | 
					
						
							| 
									
										
										
										
											2015-01-27 23:55:22 +08:00
										 |  |  |             if not prop.text: | 
					
						
							| 
									
										
										
										
											2015-01-29 22:09:59 +06:00
										 |  |  |                 continue | 
					
						
							| 
									
										
										
										
											2015-03-29 20:40:05 +08:00
										 |  |  |             encoded_content = self.base64_decode_utf8(prop.text) | 
					
						
							| 
									
										
										
										
											2015-01-27 23:55:22 +08:00
										 |  |  |             prop_dict[prop_id] = compat_urllib_parse_unquote(encoded_content) | 
					
						
							|  |  |  |         return prop_dict | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def _real_extract(self, url): | 
					
						
							|  |  |  |         video_id = self._match_id(url) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-29 22:09:59 +06:00
										 |  |  |         webpage = self._download_webpage(url, video_id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         error_msg = self._search_regex( | 
					
						
							|  |  |  |             r'<div id="error-message-content">([^<]+)', | 
					
						
							|  |  |  |             webpage, 'error message', default=None) | 
					
						
							|  |  |  |         if error_msg: | 
					
						
							|  |  |  |             raise ExtractorError( | 
					
						
							|  |  |  |                 '%s returned error: %s' % (self.IE_NAME, error_msg), | 
					
						
							|  |  |  |                 expected=True) | 
					
						
							| 
									
										
										
										
											2015-01-27 23:55:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-29 22:09:59 +06:00
										 |  |  |         video_id = self._html_search_regex( | 
					
						
							|  |  |  |             r'data-mediaid="(\d+)"', webpage, 'media id') | 
					
						
							|  |  |  |         flv_config = self._extract_flv_config(video_id) | 
					
						
							| 
									
										
										
										
											2015-01-27 23:55:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-29 22:09:59 +06:00
										 |  |  |         FORMATS = { | 
					
						
							|  |  |  |             'audio': 'mp3', | 
					
						
							|  |  |  |             'video': 'mp4', | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         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) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         timestamp = flv_config.get('publish_datetime') | 
					
						
							|  |  |  |         if timestamp: | 
					
						
							|  |  |  |             timestamp = parse_iso8601(timestamp + ' +0800', ' ') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         category = flv_config.get('category') | 
					
						
							|  |  |  |         categories = [category] if category else [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return { | 
					
						
							| 
									
										
										
										
											2015-01-27 23:55:22 +08:00
										 |  |  |             'id': video_id, | 
					
						
							|  |  |  |             'title': flv_config['title'], | 
					
						
							| 
									
										
										
										
											2015-01-29 22:09:59 +06:00
										 |  |  |             '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, | 
					
						
							| 
									
										
										
										
											2015-01-27 23:55:22 +08:00
										 |  |  |         } |