Merge branch 'IE_Xuite' of https://github.com/yan12125/youtube-dl into yan12125-IE_Xuite
[youtube-dl] / youtube_dl / extractor / xuite.py
1 # -*- coding: utf-8 -*-
2 from __future__ import unicode_literals
3
4 import base64
5 from .common import InfoExtractor
6 from ..compat import (
7     compat_urlparse,
8     compat_urllib_parse_unquote,
9     compat_parse_qs
10 )
11 from ..utils import (
12     ExtractorError,
13     parse_iso8601,
14     parse_duration
15 )
16
17 # ref: http://stackoverflow.com/questions/475074/regex-to-parse-or-validate-base64-data
18 REGEX_BASE64 = r'(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?'
19
20 CST_ZONE = +8  # China Standard Time
21
22
23 class XuiteIE(InfoExtractor):
24     _VALID_URL = r'http://vlog.xuite.net/play/(?P<id>%s)(/.*)?' % REGEX_BASE64
25     _TESTS = [{
26         # Audio
27         'url': 'http://vlog.xuite.net/play/RGkzc1ZULTM4NjA5MTQuZmx2',
28         'md5': '63a42c705772aa53fd4c1a0027f86adf',
29         'info_dict': {
30             'id': 'RGkzc1ZULTM4NjA5MTQuZmx2',
31             'ext': 'mp3',
32             'upload_date': '20110902',
33             'uploader_id': '15973816',
34             'uploader': '阿能',
35             'timestamp': 1314932940,
36             'title': '孤單南半球-歐德陽',
37             'thumbnail': 're:^https?://.*\.jpg$',
38             'categories': ['個人短片'],
39             'duration': 247.246
40         }
41     }, {
42         # Audio with alternative form of url
43         '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',
44         'md5': 'c91645f324de53d82ebb80930e1a73d2',
45         'info_dict': {
46             'id': 'S1dDUjdyLTMyOTc3NjcuZmx2',
47             'ext': 'mp3',
48             'upload_date': '20101226',
49             'uploader_id': '10102699',
50             'uploader': '蠍',
51             'timestamp': 1293367080,
52             'title': '孫燕姿-眼淚成詩',
53             'thumbnail': 're:^https?://.*\.jpg$',
54             'categories': ['個人短片'],
55             'duration': 223.19
56         }
57     }, {
58         # Video with only one format
59         'url': 'http://vlog.xuite.net/play/TkRZNjhULTM0NDE2MjkuZmx2',
60         'md5': 'c45737fc8ac5dc8ac2f92ecbcecf505e',
61         'info_dict': {
62             'id': 'TkRZNjhULTM0NDE2MjkuZmx2',
63             'ext': 'mp4',
64             'upload_date': '20110306',
65             'uploader_id': '10400126',
66             'uploader': 'Valen',
67             'timestamp': 1299383640,
68             'title': '孫燕姿 - 眼淚成詩',
69             'thumbnail': 're:^https?://.*\.jpg$',
70             'categories': ['影視娛樂'],
71             'duration': 217.399
72         }
73     }, {
74         # Video with two formats
75         'url': 'http://vlog.xuite.net/play/bWo1N1pLLTIxMzAxMTcwLmZsdg==',
76         'md5': '1166e0f461efe55b62e26a2d2a68e6de',
77         'info_dict': {
78             'id': 'bWo1N1pLLTIxMzAxMTcwLmZsdg==',
79             'ext': 'mp4',
80             'upload_date': '20150117',
81             'uploader_id': '242127761',
82             'uploader': '我只是想認真點',
83             'timestamp': 1421481240,
84             'title': '暗殺教室 02',
85             'thumbnail': 're:^https?://.*\.jpg$',
86             'categories': ['電玩動漫'],
87             'duration': 1384.907
88         }
89     }]
90
91     def _flv_config(self, media_id):
92         base64_media_id = base64.b64encode(media_id.encode('utf-8')).decode('utf-8')
93         flv_config_url = 'http://vlog.xuite.net/flash/player?media=' + base64_media_id
94         flv_config = self._download_xml(flv_config_url, 'flv config')
95
96         prop_dict = {}
97         for prop in flv_config.findall('./property'):
98             prop_id = base64.b64decode(prop.attrib['id']).decode('utf-8')
99
100             if not prop.text:
101                 continue  # CDATA may be empty in flv config
102
103             encoded_content = base64.b64decode(prop.text).decode('utf-8')
104             prop_dict[prop_id] = compat_urllib_parse_unquote(encoded_content)
105
106         return prop_dict
107
108     def _type_string(self, media_url):
109         query_string = compat_urlparse.urlparse(media_url).query
110         type_string = compat_parse_qs(query_string)['q'][0]
111         return type_string
112
113     def _guess_ext(self, media_url):
114         type_string = self._type_string(media_url)
115         if type_string == 'mp3':
116             return 'mp3'
117         elif type_string == '360' or type_string == '720':
118             return 'mp4'
119         else:
120             raise ExtractorError('Unknown type string %s' % type_string)
121
122     def _real_extract(self, url):
123         video_id = self._match_id(url)
124
125         page = self._download_webpage(url, video_id)
126         media_id = self._html_search_regex(r'data-mediaid="(\d+)"', page, 'media id')
127         flv_config = self._flv_config(media_id)
128
129         timestamp_local = parse_iso8601(flv_config['publish_datetime'], ' ')
130         timestamp_gmt = timestamp_local - CST_ZONE * 3600
131
132         ret_attrs = {
133             'id': video_id,
134             'title': flv_config['title'],
135             'thumbnail': flv_config['thumb'],
136             'uploader': flv_config['author_name'],
137             'timestamp': timestamp_gmt,
138             'uploader_id': flv_config['author_id'],
139             'duration': parse_duration(flv_config['duration']),
140             'categories': [flv_config['category']]
141         }
142
143         if 'hq_src' in flv_config:
144             urls = [flv_config['src'], flv_config['hq_src']]
145
146             ret_attrs['formats'] = []
147
148             for url in urls:
149                 ret_attrs['formats'].append({
150                     'url': url,
151                     'ext': self._guess_ext(url),
152                     'format_id': self._type_string(url),
153                     'height': int(self._type_string(url))
154                 })
155         else:
156             ret_attrs['url'] = flv_config['src']
157             ret_attrs['ext'] = self._guess_ext(flv_config['src'])
158
159         return ret_attrs