[xuite] Add height information for the two formats
[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         }
38     }, {
39         # Audio with alternative form of url
40         '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',
41         'md5': 'c91645f324de53d82ebb80930e1a73d2',
42         'info_dict': {
43             'id': 'S1dDUjdyLTMyOTc3NjcuZmx2',
44             'ext': 'mp3',
45             'upload_date': '20101226',
46             'uploader_id': '10102699',
47             'uploader': '蠍',
48             'timestamp': 1293367080,
49             'title': '孫燕姿-眼淚成詩',
50         }
51     }, {
52         # Video with only one format
53         'url': 'http://vlog.xuite.net/play/TkRZNjhULTM0NDE2MjkuZmx2',
54         'md5': 'c45737fc8ac5dc8ac2f92ecbcecf505e',
55         'info_dict': {
56             'id': 'TkRZNjhULTM0NDE2MjkuZmx2',
57             'ext': 'mp4',
58             'upload_date': '20110306',
59             'uploader_id': '10400126',
60             'uploader': 'Valen',
61             'timestamp': 1299383640,
62             'title': '孫燕姿 - 眼淚成詩',
63         }
64     }, {
65         # Video with two formats
66         'url': 'http://vlog.xuite.net/play/bWo1N1pLLTIxMzAxMTcwLmZsdg==',
67         'md5': '1166e0f461efe55b62e26a2d2a68e6de',
68         'info_dict': {
69             'id': 'bWo1N1pLLTIxMzAxMTcwLmZsdg==',
70             'ext': 'mp4',
71             'upload_date': '20150117',
72             'uploader_id': '242127761',
73             'uploader': '我只是想認真點',
74             'timestamp': 1421481240,
75             'title': '暗殺教室 02',
76         }
77     }]
78
79     def _flv_config(self, media_id):
80         base64_media_id = base64.b64encode(media_id.encode('utf-8')).decode('utf-8')
81         flv_config_url = 'http://vlog.xuite.net/flash/player?media=' + base64_media_id
82         flv_config = self._download_xml(flv_config_url, 'flv config')
83
84         prop_dict = {}
85         for prop in flv_config.findall('./property'):
86             prop_id = base64.b64decode(prop.attrib['id']).decode('utf-8')
87
88             if not prop.text:
89                 continue  # CDATA may be empty in flv config
90
91             encoded_content = base64.b64decode(prop.text).decode('utf-8')
92             prop_dict[prop_id] = compat_urllib_parse_unquote(encoded_content)
93
94         return prop_dict
95
96     def _type_string(self, media_url):
97         query_string = compat_urlparse.urlparse(media_url).query
98         type_string = compat_parse_qs(query_string)['q'][0]
99         return type_string
100
101     def _guess_ext(self, media_url):
102         type_string = self._type_string(media_url)
103         if type_string == 'mp3':
104             return 'mp3'
105         elif type_string == '360' or type_string == '720':
106             return 'mp4'
107         else:
108             raise ExtractorError('Unknown type string %s' % type_string)
109
110     def _real_extract(self, url):
111         video_id = self._match_id(url)
112
113         page = self._download_webpage(url, video_id)
114         media_id = self._html_search_regex(r'data-mediaid="(\d+)"', page, 'media id')
115         flv_config = self._flv_config(media_id)
116
117         timestamp_local = parse_iso8601(flv_config['publish_datetime'], ' ')
118         timestamp_gmt = timestamp_local - CST_ZONE * 3600
119
120         ret_attrs = {
121             'id': video_id,
122             'title': flv_config['title'],
123             'thumbnail': flv_config['thumb'],
124             'uploader': flv_config['author_name'],
125             'timestamp': timestamp_gmt,
126             'uploader_id': flv_config['author_id'],
127             'duration': parse_duration(flv_config['duration']),
128             'categories': [flv_config['category']]
129         }
130
131         if 'hq_src' in flv_config:
132             urls = [flv_config['src'], flv_config['hq_src']]
133
134             ret_attrs['formats'] = []
135
136             for url in urls:
137                 ret_attrs['formats'].append({
138                     'url': url,
139                     'ext': self._guess_ext(url),
140                     'format_id': self._type_string(url),
141                     'height': int(self._type_string(url))
142                 })
143         else:
144             ret_attrs['url'] = flv_config['src']
145             ret_attrs['ext'] = self._guess_ext(flv_config['src'])
146
147         return ret_attrs