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