[xuite] Replace the test case with my uploaded one
[youtube-dl] / 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     IE_DESC = '隨意窩Xuite影音'
17     _REGEX_BASE64 = r'(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?'
18     _VALID_URL = r'https?://vlog\.xuite\.net/(?:play|embed)/(?P<id>%s)' % _REGEX_BASE64
19     _TESTS = [{
20         # Audio
21         'url': 'http://vlog.xuite.net/play/RGkzc1ZULTM4NjA5MTQuZmx2',
22         'md5': 'e79284c87b371424885448d11f6398c8',
23         'info_dict': {
24             'id': '3860914',
25             'ext': 'mp3',
26             'title': '孤單南半球-歐德陽',
27             'thumbnail': 're:^https?://.*\.jpg$',
28             'duration': 247.246,
29             'timestamp': 1314932940,
30             'upload_date': '20110902',
31             'uploader': '阿能',
32             'uploader_id': '15973816',
33             'categories': ['個人短片'],
34         },
35     }, {
36         # Video with only one format
37         'url': 'http://vlog.xuite.net/play/WUxxR2xCLTI1OTI1MDk5LmZsdg==',
38         'md5': '21f7b39c009b5a4615b4463df6eb7a46',
39         'info_dict': {
40             'id': '25925099',
41             'ext': 'mp4',
42             'title': 'BigBuckBunny_320x180',
43             'thumbnail': 're:^https?://.*\.jpg$',
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',
51         },
52     }, {
53         # Video with two formats
54         'url': 'http://vlog.xuite.net/play/bWo1N1pLLTIxMzAxMTcwLmZsdg==',
55         'md5': '1166e0f461efe55b62e26a2d2a68e6de',
56         'info_dict': {
57             'id': '21301170',
58             'ext': 'mp4',
59             'title': '暗殺教室 02',
60             'description': '字幕:【極影字幕社】',
61             'thumbnail': 're:^https?://.*\.jpg$',
62             'duration': 1384.907,
63             'timestamp': 1421481240,
64             'upload_date': '20150117',
65             'uploader': '我只是想認真點',
66             'uploader_id': '242127761',
67             'categories': ['電玩動漫'],
68         },
69     }, {
70         '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',
71         'only_matching': True,
72     }]
73
74     @staticmethod
75     def base64_decode_utf8(data):
76         return base64.b64decode(data.encode('utf-8')).decode('utf-8')
77
78     @staticmethod
79     def base64_encode_utf8(data):
80         return base64.b64encode(data.encode('utf-8')).decode('utf-8')
81
82     def _extract_flv_config(self, media_id):
83         base64_media_id = self.base64_encode_utf8(media_id)
84         flv_config = self._download_xml(
85             'http://vlog.xuite.net/flash/player?media=%s' % base64_media_id,
86             'flv config')
87         prop_dict = {}
88         for prop in flv_config.findall('./property'):
89             prop_id = self.base64_decode_utf8(prop.attrib['id'])
90             # CDATA may be empty in flv config
91             if not prop.text:
92                 continue
93             encoded_content = self.base64_decode_utf8(prop.text)
94             prop_dict[prop_id] = compat_urllib_parse_unquote(encoded_content)
95         return prop_dict
96
97     def _real_extract(self, url):
98         video_id = self._match_id(url)
99
100         webpage = self._download_webpage(url, video_id)
101
102         error_msg = self._search_regex(
103             r'<div id="error-message-content">([^<]+)',
104             webpage, 'error message', default=None)
105         if error_msg:
106             raise ExtractorError(
107                 '%s returned error: %s' % (self.IE_NAME, error_msg),
108                 expected=True)
109
110         video_id = self._html_search_regex(
111             r'data-mediaid="(\d+)"', webpage, 'media id')
112         flv_config = self._extract_flv_config(video_id)
113
114         FORMATS = {
115             'audio': 'mp3',
116             'video': 'mp4',
117         }
118
119         formats = []
120         for format_tag in ('src', 'hq_src'):
121             video_url = flv_config.get(format_tag)
122             if not video_url:
123                 continue
124             format_id = self._search_regex(
125                 r'\bq=(.+?)\b', video_url, 'format id', default=format_tag)
126             formats.append({
127                 'url': video_url,
128                 'ext': FORMATS.get(flv_config['type'], 'mp4'),
129                 'format_id': format_id,
130                 'height': int(format_id) if format_id.isnumeric() else None,
131             })
132         self._sort_formats(formats)
133
134         timestamp = flv_config.get('publish_datetime')
135         if timestamp:
136             timestamp = parse_iso8601(timestamp + ' +0800', ' ')
137
138         category = flv_config.get('category')
139         categories = [category] if category else []
140
141         return {
142             'id': video_id,
143             'title': flv_config['title'],
144             'description': flv_config.get('description'),
145             'thumbnail': flv_config.get('thumb'),
146             'timestamp': timestamp,
147             'uploader': flv_config.get('author_name'),
148             'uploader_id': flv_config.get('author_id'),
149             'duration': parse_duration(flv_config.get('duration')),
150             'categories': categories,
151             'formats': formats,
152         }