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