]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/xuite.py
[extractor] Deprecate `_sort_formats`
[yt-dlp.git] / yt_dlp / extractor / xuite.py
CommitLineData
fe7710cb 1from .common import InfoExtractor
fe7710cb
YCH
2from ..utils import (
3 ExtractorError,
babbc04d
YCH
4 float_or_none,
5 get_element_by_attribute,
fe7710cb 6 parse_iso8601,
babbc04d 7 remove_end,
fe7710cb
YCH
8)
9
fe7710cb
YCH
10
11class XuiteIE(InfoExtractor):
4fa5f402 12 IE_DESC = '隨意窩Xuite影音'
affd04a4 13 _REGEX_BASE64 = r'(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?'
21933799 14 _VALID_URL = r'https?://vlog\.xuite\.net/(?:play|embed)/(?P<id>%s)' % _REGEX_BASE64
fe7710cb
YCH
15 _TESTS = [{
16 # Audio
17 'url': 'http://vlog.xuite.net/play/RGkzc1ZULTM4NjA5MTQuZmx2',
75bb5c70 18 'md5': 'e79284c87b371424885448d11f6398c8',
fe7710cb 19 'info_dict': {
affd04a4 20 'id': '3860914',
fe7710cb 21 'ext': 'mp3',
a2838383 22 'title': '孤單南半球-歐德陽',
babbc04d 23 'description': '孤單南半球-歐德陽',
ec85ded8 24 'thumbnail': r're:^https?://.*\.jpg$',
affd04a4
S
25 'duration': 247.246,
26 'timestamp': 1314932940,
27 'upload_date': '20110902',
28 'uploader': '阿能',
29 'uploader_id': '15973816',
a2838383 30 'categories': ['個人短片'],
affd04a4 31 },
fe7710cb
YCH
32 }, {
33 # Video with only one format
231ea2a3
YCH
34 'url': 'http://vlog.xuite.net/play/WUxxR2xCLTI1OTI1MDk5LmZsdg==',
35 'md5': '21f7b39c009b5a4615b4463df6eb7a46',
fe7710cb 36 'info_dict': {
231ea2a3 37 'id': '25925099',
fe7710cb 38 'ext': 'mp4',
231ea2a3 39 'title': 'BigBuckBunny_320x180',
ec85ded8 40 'thumbnail': r're:^https?://.*\.jpg$',
231ea2a3
YCH
41 'duration': 596.458,
42 'timestamp': 1454242500,
43 'upload_date': '20160131',
babbc04d 44 'uploader': '屁姥',
231ea2a3
YCH
45 'uploader_id': '12158353',
46 'categories': ['個人短片'],
47 'description': 'http://download.blender.org/peach/bigbuckbunny_movies/BigBuckBunny_320x180.mp4',
affd04a4 48 },
fe7710cb
YCH
49 }, {
50 # Video with two formats
51 'url': 'http://vlog.xuite.net/play/bWo1N1pLLTIxMzAxMTcwLmZsdg==',
52 'md5': '1166e0f461efe55b62e26a2d2a68e6de',
53 'info_dict': {
affd04a4 54 'id': '21301170',
fe7710cb 55 'ext': 'mp4',
fe7710cb 56 'title': '暗殺教室 02',
affd04a4 57 'description': '字幕:【極影字幕社】',
ec85ded8 58 'thumbnail': r're:^https?://.*\.jpg$',
affd04a4
S
59 'duration': 1384.907,
60 'timestamp': 1421481240,
61 'upload_date': '20150117',
62 'uploader': '我只是想認真點',
63 'uploader_id': '242127761',
a2838383 64 'categories': ['電玩動漫'],
affd04a4 65 },
506d0e96 66 'skip': 'Video removed',
c6054e32
YCH
67 }, {
68 # Video with encoded media id
69 # from http://forgetfulbc.blogspot.com/2016/06/date.html
70 'url': 'http://vlog.xuite.net/embed/cE1xbENoLTI3NDQ3MzM2LmZsdg==?ar=0&as=0',
71 'info_dict': {
babbc04d 72 'id': '27447336',
c6054e32
YCH
73 'ext': 'mp4',
74 'title': '男女平權只是口號?專家解釋約會時男生是否該幫女生付錢 (中字)',
babbc04d 75 'description': 'md5:1223810fa123b179083a3aed53574706',
c6054e32
YCH
76 'timestamp': 1466160960,
77 'upload_date': '20160617',
78 'uploader': 'B.C. & Lowy',
79 'uploader_id': '232279340',
80 },
affd04a4
S
81 }, {
82 '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',
83 'only_matching': True,
fe7710cb
YCH
84 }]
85
fe7710cb 86 def _real_extract(self, url):
babbc04d
YCH
87 # /play/ URLs provide embedded video URL and more metadata
88 url = url.replace('/embed/', '/play/')
fe7710cb
YCH
89 video_id = self._match_id(url)
90
affd04a4
S
91 webpage = self._download_webpage(url, video_id)
92
93 error_msg = self._search_regex(
94 r'<div id="error-message-content">([^<]+)',
95 webpage, 'error message', default=None)
96 if error_msg:
97 raise ExtractorError(
98 '%s returned error: %s' % (self.IE_NAME, error_msg),
99 expected=True)
fe7710cb 100
babbc04d
YCH
101 media_info = self._parse_json(self._search_regex(
102 r'var\s+mediaInfo\s*=\s*({.*});', webpage, 'media info'), video_id)
fe7710cb 103
babbc04d 104 video_id = media_info['MEDIA_ID']
affd04a4
S
105
106 formats = []
babbc04d
YCH
107 for key in ('html5Url', 'html5HQUrl'):
108 video_url = media_info.get(key)
affd04a4
S
109 if not video_url:
110 continue
111 format_id = self._search_regex(
babbc04d 112 r'\bq=(.+?)\b', video_url, 'format id', default=None)
affd04a4
S
113 formats.append({
114 'url': video_url,
babbc04d 115 'ext': 'mp4' if format_id.isnumeric() else format_id,
affd04a4
S
116 'format_id': format_id,
117 'height': int(format_id) if format_id.isnumeric() else None,
118 })
affd04a4 119
babbc04d 120 timestamp = media_info.get('PUBLISH_DATETIME')
affd04a4
S
121 if timestamp:
122 timestamp = parse_iso8601(timestamp + ' +0800', ' ')
123
babbc04d 124 category = media_info.get('catName')
affd04a4
S
125 categories = [category] if category else []
126
babbc04d
YCH
127 uploader = media_info.get('NICKNAME')
128 uploader_url = None
129
130 author_div = get_element_by_attribute('itemprop', 'author', webpage)
131 if author_div:
132 uploader = uploader or self._html_search_meta('name', author_div)
133 uploader_url = self._html_search_regex(
134 r'<link[^>]+itemprop="url"[^>]+href="([^"]+)"', author_div,
135 'uploader URL', fatal=False)
136
affd04a4 137 return {
fe7710cb 138 'id': video_id,
babbc04d
YCH
139 'title': media_info['TITLE'],
140 'description': remove_end(media_info.get('metaDesc'), ' (Xuite 影音)'),
141 'thumbnail': media_info.get('ogImageUrl'),
affd04a4 142 'timestamp': timestamp,
babbc04d
YCH
143 'uploader': uploader,
144 'uploader_id': media_info.get('MEMBER_ID'),
145 'uploader_url': uploader_url,
146 'duration': float_or_none(media_info.get('MEDIA_DURATION'), 1000000),
affd04a4
S
147 'categories': categories,
148 'formats': formats,
fe7710cb 149 }