]>
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 | |
231ea2a3 YCH |
37 | 'url': 'http://vlog.xuite.net/play/WUxxR2xCLTI1OTI1MDk5LmZsdg==', |
38 | 'md5': '21f7b39c009b5a4615b4463df6eb7a46', | |
fe7710cb | 39 | 'info_dict': { |
231ea2a3 | 40 | 'id': '25925099', |
fe7710cb | 41 | 'ext': 'mp4', |
231ea2a3 | 42 | 'title': 'BigBuckBunny_320x180', |
a2838383 | 43 | 'thumbnail': 're:^https?://.*\.jpg$', |
231ea2a3 YCH |
44 | 'duration': 596.458, |
45 | 'timestamp': 1454242500, | |
46 | 'upload_date': '20160131', | |
47 | 'uploader': 'yan12125', | |
48 | 'uploader_id': '12158353', | |
49 | 'categories': ['個人短片'], | |
50 | 'description': 'http://download.blender.org/peach/bigbuckbunny_movies/BigBuckBunny_320x180.mp4', | |
affd04a4 | 51 | }, |
fe7710cb YCH |
52 | }, { |
53 | # Video with two formats | |
54 | 'url': 'http://vlog.xuite.net/play/bWo1N1pLLTIxMzAxMTcwLmZsdg==', | |
55 | 'md5': '1166e0f461efe55b62e26a2d2a68e6de', | |
56 | 'info_dict': { | |
affd04a4 | 57 | 'id': '21301170', |
fe7710cb | 58 | 'ext': 'mp4', |
fe7710cb | 59 | 'title': '暗殺教室 02', |
affd04a4 | 60 | 'description': '字幕:【極影字幕社】', |
a2838383 | 61 | 'thumbnail': 're:^https?://.*\.jpg$', |
affd04a4 S |
62 | 'duration': 1384.907, |
63 | 'timestamp': 1421481240, | |
64 | 'upload_date': '20150117', | |
65 | 'uploader': '我只是想認真點', | |
66 | 'uploader_id': '242127761', | |
a2838383 | 67 | 'categories': ['電玩動漫'], |
affd04a4 | 68 | }, |
506d0e96 | 69 | 'skip': 'Video removed', |
c6054e32 YCH |
70 | }, { |
71 | # Video with encoded media id | |
72 | # from http://forgetfulbc.blogspot.com/2016/06/date.html | |
73 | 'url': 'http://vlog.xuite.net/embed/cE1xbENoLTI3NDQ3MzM2LmZsdg==?ar=0&as=0', | |
74 | 'info_dict': { | |
75 | 'id': 'cE1xbENoLTI3NDQ3MzM2LmZsdg==', | |
76 | 'ext': 'mp4', | |
77 | 'title': '男女平權只是口號?專家解釋約會時男生是否該幫女生付錢 (中字)', | |
78 | 'description': 'md5:f0abdcb69df300f522a5442ef3146f2a', | |
79 | 'timestamp': 1466160960, | |
80 | 'upload_date': '20160617', | |
81 | 'uploader': 'B.C. & Lowy', | |
82 | 'uploader_id': '232279340', | |
83 | }, | |
affd04a4 S |
84 | }, { |
85 | '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', | |
86 | 'only_matching': True, | |
fe7710cb YCH |
87 | }] |
88 | ||
9b4774b2 YCH |
89 | @staticmethod |
90 | def base64_decode_utf8(data): | |
91 | return base64.b64decode(data.encode('utf-8')).decode('utf-8') | |
92 | ||
93 | @staticmethod | |
94 | def base64_encode_utf8(data): | |
95 | return base64.b64encode(data.encode('utf-8')).decode('utf-8') | |
96 | ||
c6054e32 | 97 | def _extract_flv_config(self, encoded_media_id): |
affd04a4 | 98 | flv_config = self._download_xml( |
c6054e32 | 99 | 'http://vlog.xuite.net/flash/player?media=%s' % encoded_media_id, |
affd04a4 | 100 | 'flv config') |
fe7710cb YCH |
101 | prop_dict = {} |
102 | for prop in flv_config.findall('./property'): | |
9b4774b2 | 103 | prop_id = self.base64_decode_utf8(prop.attrib['id']) |
affd04a4 | 104 | # CDATA may be empty in flv config |
fe7710cb | 105 | if not prop.text: |
affd04a4 | 106 | continue |
9b4774b2 | 107 | encoded_content = self.base64_decode_utf8(prop.text) |
fe7710cb | 108 | prop_dict[prop_id] = compat_urllib_parse_unquote(encoded_content) |
fe7710cb YCH |
109 | return prop_dict |
110 | ||
fe7710cb YCH |
111 | def _real_extract(self, url): |
112 | video_id = self._match_id(url) | |
113 | ||
affd04a4 S |
114 | webpage = self._download_webpage(url, video_id) |
115 | ||
116 | error_msg = self._search_regex( | |
117 | r'<div id="error-message-content">([^<]+)', | |
118 | webpage, 'error message', default=None) | |
119 | if error_msg: | |
120 | raise ExtractorError( | |
121 | '%s returned error: %s' % (self.IE_NAME, error_msg), | |
122 | expected=True) | |
fe7710cb | 123 | |
c6054e32 YCH |
124 | encoded_media_id = self._search_regex( |
125 | r'attributes\.name\s*=\s*"([^"]+)"', webpage, | |
126 | 'encoded media id', default=None) | |
127 | if encoded_media_id is None: | |
128 | video_id = self._html_search_regex( | |
129 | r'data-mediaid="(\d+)"', webpage, 'media id') | |
130 | encoded_media_id = self.base64_encode_utf8(video_id) | |
131 | flv_config = self._extract_flv_config(encoded_media_id) | |
fe7710cb | 132 | |
affd04a4 S |
133 | FORMATS = { |
134 | 'audio': 'mp3', | |
135 | 'video': 'mp4', | |
136 | } | |
137 | ||
138 | formats = [] | |
139 | for format_tag in ('src', 'hq_src'): | |
140 | video_url = flv_config.get(format_tag) | |
141 | if not video_url: | |
142 | continue | |
143 | format_id = self._search_regex( | |
144 | r'\bq=(.+?)\b', video_url, 'format id', default=format_tag) | |
145 | formats.append({ | |
146 | 'url': video_url, | |
147 | 'ext': FORMATS.get(flv_config['type'], 'mp4'), | |
148 | 'format_id': format_id, | |
149 | 'height': int(format_id) if format_id.isnumeric() else None, | |
150 | }) | |
151 | self._sort_formats(formats) | |
152 | ||
153 | timestamp = flv_config.get('publish_datetime') | |
154 | if timestamp: | |
155 | timestamp = parse_iso8601(timestamp + ' +0800', ' ') | |
156 | ||
157 | category = flv_config.get('category') | |
158 | categories = [category] if category else [] | |
159 | ||
160 | return { | |
fe7710cb YCH |
161 | 'id': video_id, |
162 | 'title': flv_config['title'], | |
affd04a4 S |
163 | 'description': flv_config.get('description'), |
164 | 'thumbnail': flv_config.get('thumb'), | |
165 | 'timestamp': timestamp, | |
166 | 'uploader': flv_config.get('author_name'), | |
167 | 'uploader_id': flv_config.get('author_id'), | |
168 | 'duration': parse_duration(flv_config.get('duration')), | |
169 | 'categories': categories, | |
170 | 'formats': formats, | |
fe7710cb | 171 | } |