]>
Commit | Line | Data |
---|---|---|
fe7710cb YCH |
1 | # -*- coding: utf-8 -*- |
2 | from __future__ import unicode_literals | |
3 | ||
4 | import base64 | |
affd04a4 | 5 | |
fe7710cb | 6 | from .common import InfoExtractor |
affd04a4 | 7 | from ..compat import compat_urllib_parse_unquote |
fe7710cb YCH |
8 | from ..utils import ( |
9 | ExtractorError, | |
10 | parse_iso8601, | |
affd04a4 | 11 | parse_duration, |
fe7710cb YCH |
12 | ) |
13 | ||
fe7710cb YCH |
14 | |
15 | class XuiteIE(InfoExtractor): | |
4fa5f402 | 16 | IE_DESC = '隨意窩Xuite影音' |
affd04a4 | 17 | _REGEX_BASE64 = r'(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?' |
21933799 | 18 | _VALID_URL = r'https?://vlog\.xuite\.net/(?:play|embed)/(?P<id>%s)' % _REGEX_BASE64 |
fe7710cb YCH |
19 | _TESTS = [{ |
20 | # Audio | |
21 | 'url': 'http://vlog.xuite.net/play/RGkzc1ZULTM4NjA5MTQuZmx2', | |
75bb5c70 | 22 | 'md5': 'e79284c87b371424885448d11f6398c8', |
fe7710cb | 23 | 'info_dict': { |
affd04a4 | 24 | 'id': '3860914', |
fe7710cb | 25 | 'ext': 'mp3', |
a2838383 YCH |
26 | 'title': '孤單南半球-歐德陽', |
27 | 'thumbnail': 're:^https?://.*\.jpg$', | |
affd04a4 S |
28 | 'duration': 247.246, |
29 | 'timestamp': 1314932940, | |
30 | 'upload_date': '20110902', | |
31 | 'uploader': '阿能', | |
32 | 'uploader_id': '15973816', | |
a2838383 | 33 | 'categories': ['個人短片'], |
affd04a4 | 34 | }, |
fe7710cb YCH |
35 | }, { |
36 | # Video with only one format | |
37 | 'url': 'http://vlog.xuite.net/play/TkRZNjhULTM0NDE2MjkuZmx2', | |
38 | 'md5': 'c45737fc8ac5dc8ac2f92ecbcecf505e', | |
39 | 'info_dict': { | |
affd04a4 | 40 | 'id': '3441629', |
fe7710cb | 41 | 'ext': 'mp4', |
fe7710cb | 42 | 'title': '孫燕姿 - 眼淚成詩', |
a2838383 | 43 | 'thumbnail': 're:^https?://.*\.jpg$', |
affd04a4 S |
44 | 'duration': 217.399, |
45 | 'timestamp': 1299383640, | |
46 | 'upload_date': '20110306', | |
47 | 'uploader': 'Valen', | |
48 | 'uploader_id': '10400126', | |
a2838383 | 49 | 'categories': ['影視娛樂'], |
affd04a4 | 50 | }, |
fe7710cb YCH |
51 | }, { |
52 | # Video with two formats | |
53 | 'url': 'http://vlog.xuite.net/play/bWo1N1pLLTIxMzAxMTcwLmZsdg==', | |
54 | 'md5': '1166e0f461efe55b62e26a2d2a68e6de', | |
55 | 'info_dict': { | |
affd04a4 | 56 | 'id': '21301170', |
fe7710cb | 57 | 'ext': 'mp4', |
fe7710cb | 58 | 'title': '暗殺教室 02', |
affd04a4 | 59 | 'description': '字幕:【極影字幕社】', |
a2838383 | 60 | 'thumbnail': 're:^https?://.*\.jpg$', |
affd04a4 S |
61 | 'duration': 1384.907, |
62 | 'timestamp': 1421481240, | |
63 | 'upload_date': '20150117', | |
64 | 'uploader': '我只是想認真點', | |
65 | 'uploader_id': '242127761', | |
a2838383 | 66 | 'categories': ['電玩動漫'], |
affd04a4 S |
67 | }, |
68 | }, { | |
69 | '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', | |
70 | 'only_matching': True, | |
fe7710cb YCH |
71 | }] |
72 | ||
9b4774b2 YCH |
73 | @staticmethod |
74 | def base64_decode_utf8(data): | |
75 | return base64.b64decode(data.encode('utf-8')).decode('utf-8') | |
76 | ||
77 | @staticmethod | |
78 | def base64_encode_utf8(data): | |
79 | return base64.b64encode(data.encode('utf-8')).decode('utf-8') | |
80 | ||
affd04a4 | 81 | def _extract_flv_config(self, media_id): |
9b4774b2 | 82 | base64_media_id = self.base64_encode_utf8(media_id) |
affd04a4 S |
83 | flv_config = self._download_xml( |
84 | 'http://vlog.xuite.net/flash/player?media=%s' % base64_media_id, | |
85 | 'flv config') | |
fe7710cb YCH |
86 | prop_dict = {} |
87 | for prop in flv_config.findall('./property'): | |
9b4774b2 | 88 | prop_id = self.base64_decode_utf8(prop.attrib['id']) |
affd04a4 | 89 | # CDATA may be empty in flv config |
fe7710cb | 90 | if not prop.text: |
affd04a4 | 91 | continue |
9b4774b2 | 92 | encoded_content = self.base64_decode_utf8(prop.text) |
fe7710cb | 93 | prop_dict[prop_id] = compat_urllib_parse_unquote(encoded_content) |
fe7710cb YCH |
94 | return prop_dict |
95 | ||
fe7710cb YCH |
96 | def _real_extract(self, url): |
97 | video_id = self._match_id(url) | |
98 | ||
affd04a4 S |
99 | webpage = self._download_webpage(url, video_id) |
100 | ||
101 | error_msg = self._search_regex( | |
102 | r'<div id="error-message-content">([^<]+)', | |
103 | webpage, 'error message', default=None) | |
104 | if error_msg: | |
105 | raise ExtractorError( | |
106 | '%s returned error: %s' % (self.IE_NAME, error_msg), | |
107 | expected=True) | |
fe7710cb | 108 | |
affd04a4 S |
109 | video_id = self._html_search_regex( |
110 | r'data-mediaid="(\d+)"', webpage, 'media id') | |
111 | flv_config = self._extract_flv_config(video_id) | |
fe7710cb | 112 | |
affd04a4 S |
113 | FORMATS = { |
114 | 'audio': 'mp3', | |
115 | 'video': 'mp4', | |
116 | } | |
117 | ||
118 | formats = [] | |
119 | for format_tag in ('src', 'hq_src'): | |
120 | video_url = flv_config.get(format_tag) | |
121 | if not video_url: | |
122 | continue | |
123 | format_id = self._search_regex( | |
124 | r'\bq=(.+?)\b', video_url, 'format id', default=format_tag) | |
125 | formats.append({ | |
126 | 'url': video_url, | |
127 | 'ext': FORMATS.get(flv_config['type'], 'mp4'), | |
128 | 'format_id': format_id, | |
129 | 'height': int(format_id) if format_id.isnumeric() else None, | |
130 | }) | |
131 | self._sort_formats(formats) | |
132 | ||
133 | timestamp = flv_config.get('publish_datetime') | |
134 | if timestamp: | |
135 | timestamp = parse_iso8601(timestamp + ' +0800', ' ') | |
136 | ||
137 | category = flv_config.get('category') | |
138 | categories = [category] if category else [] | |
139 | ||
140 | return { | |
fe7710cb YCH |
141 | 'id': video_id, |
142 | 'title': flv_config['title'], | |
affd04a4 S |
143 | 'description': flv_config.get('description'), |
144 | 'thumbnail': flv_config.get('thumb'), | |
145 | 'timestamp': timestamp, | |
146 | 'uploader': flv_config.get('author_name'), | |
147 | 'uploader_id': flv_config.get('author_id'), | |
148 | 'duration': parse_duration(flv_config.get('duration')), | |
149 | 'categories': categories, | |
150 | 'formats': formats, | |
fe7710cb | 151 | } |