2013-07-18 07:31:50 -06:00
|
|
|
|
from .common import InfoExtractor
|
2023-07-09 01:53:02 -06:00
|
|
|
|
from ..networking import HEADRequest
|
2016-05-13 13:24:36 -06:00
|
|
|
|
from ..utils import (
|
|
|
|
|
ExtractorError,
|
2023-07-09 01:53:02 -06:00
|
|
|
|
clean_html,
|
|
|
|
|
get_element_by_attribute,
|
2016-05-13 13:24:36 -06:00
|
|
|
|
int_or_none,
|
|
|
|
|
qualities,
|
2023-07-09 01:53:02 -06:00
|
|
|
|
update_url_query,
|
2016-05-13 13:24:36 -06:00
|
|
|
|
)
|
2013-07-18 07:31:50 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SinaIE(InfoExtractor):
|
2023-10-13 16:15:35 -06:00
|
|
|
|
_VALID_URL = r'''(?x)https?://(?:[^/?#]+\.)?video\.sina\.com\.cn/
|
2016-05-13 13:24:36 -06:00
|
|
|
|
(?:
|
2021-06-06 03:35:07 -06:00
|
|
|
|
(?:view/|.*\#)(?P<id>\d+)|
|
2016-05-13 13:24:36 -06:00
|
|
|
|
.+?/(?P<pseudo_id>[^/?#]+)(?:\.s?html)|
|
2013-07-18 07:31:50 -06:00
|
|
|
|
# This is used by external sites like Weibo
|
2016-05-13 13:24:36 -06:00
|
|
|
|
api/sinawebApi/outplay.php/(?P<token>.+?)\.swf
|
2013-07-18 07:31:50 -06:00
|
|
|
|
)
|
|
|
|
|
'''
|
|
|
|
|
|
2014-01-23 06:03:14 -07:00
|
|
|
|
_TESTS = [
|
|
|
|
|
{
|
2016-05-13 13:24:36 -06:00
|
|
|
|
'url': 'http://video.sina.com.cn/news/spj/topvideoes20160504/?opsubject_id=top1#250576622',
|
|
|
|
|
'md5': 'd38433e2fc886007729735650ae4b3e9',
|
2014-01-23 06:03:14 -07:00
|
|
|
|
'info_dict': {
|
2016-05-13 13:24:36 -06:00
|
|
|
|
'id': '250576622',
|
|
|
|
|
'ext': 'mp4',
|
|
|
|
|
'title': '现场:克鲁兹宣布退选 特朗普将稳获提名',
|
2024-06-11 17:09:58 -06:00
|
|
|
|
},
|
2014-01-23 06:03:14 -07:00
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
'url': 'http://video.sina.com.cn/v/b/101314253-1290078633.html',
|
|
|
|
|
'info_dict': {
|
|
|
|
|
'id': '101314253',
|
|
|
|
|
'ext': 'flv',
|
|
|
|
|
'title': '军方提高对朝情报监视级别',
|
|
|
|
|
},
|
2016-05-13 13:24:36 -06:00
|
|
|
|
'skip': 'the page does not exist or has been deleted',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
'url': 'http://video.sina.com.cn/view/250587748.html',
|
|
|
|
|
'md5': '3d1807a25c775092aab3bc157fff49b4',
|
|
|
|
|
'info_dict': {
|
|
|
|
|
'id': '250587748',
|
|
|
|
|
'ext': 'mp4',
|
|
|
|
|
'title': '瞬间泪目:8年前汶川地震珍贵视频首曝光',
|
|
|
|
|
},
|
2014-01-23 06:03:14 -07:00
|
|
|
|
},
|
|
|
|
|
]
|
2013-07-18 07:31:50 -06:00
|
|
|
|
|
|
|
|
|
def _real_extract(self, url):
|
2021-08-18 19:41:24 -06:00
|
|
|
|
mobj = self._match_valid_url(url)
|
2013-07-18 07:31:50 -06:00
|
|
|
|
|
2021-06-06 03:35:07 -06:00
|
|
|
|
video_id = mobj.group('id')
|
2016-05-13 13:24:36 -06:00
|
|
|
|
if not video_id:
|
|
|
|
|
if mobj.group('token') is not None:
|
|
|
|
|
# The video id is in the redirected url
|
|
|
|
|
self.to_screen('Getting video id')
|
|
|
|
|
request = HEADRequest(url)
|
2018-05-26 09:12:44 -06:00
|
|
|
|
_, urlh = self._download_webpage_handle(request, 'NA', False)
|
2023-07-09 01:53:02 -06:00
|
|
|
|
return self._real_extract(urlh.url)
|
2016-05-13 13:24:36 -06:00
|
|
|
|
else:
|
|
|
|
|
pseudo_id = mobj.group('pseudo_id')
|
|
|
|
|
webpage = self._download_webpage(url, pseudo_id)
|
|
|
|
|
error = get_element_by_attribute('class', 'errtitle', webpage)
|
|
|
|
|
if error:
|
2024-06-11 17:09:58 -06:00
|
|
|
|
raise ExtractorError(f'{self.IE_NAME} said: {clean_html(error)}', expected=True)
|
2016-05-13 13:24:36 -06:00
|
|
|
|
video_id = self._search_regex(
|
|
|
|
|
r"video_id\s*:\s*'(\d+)'", webpage, 'video id')
|
|
|
|
|
|
|
|
|
|
video_data = self._download_json(
|
|
|
|
|
'http://s.video.sina.com.cn/video/h5play',
|
|
|
|
|
video_id, query={'video_id': video_id})
|
|
|
|
|
if video_data['code'] != 1:
|
2024-06-11 17:09:58 -06:00
|
|
|
|
raise ExtractorError('{} said: {}'.format(
|
2016-05-13 13:24:36 -06:00
|
|
|
|
self.IE_NAME, video_data['message']), expected=True)
|
|
|
|
|
else:
|
|
|
|
|
video_data = video_data['data']
|
|
|
|
|
title = video_data['title']
|
|
|
|
|
description = video_data.get('description')
|
|
|
|
|
if description:
|
|
|
|
|
description = description.strip()
|
|
|
|
|
|
|
|
|
|
preference = qualities(['cif', 'sd', 'hd', 'fhd', 'ffd'])
|
|
|
|
|
formats = []
|
|
|
|
|
for quality_id, quality in video_data.get('videos', {}).get('mp4', {}).items():
|
|
|
|
|
file_api = quality.get('file_api')
|
|
|
|
|
file_id = quality.get('file_id')
|
|
|
|
|
if not file_api or not file_id:
|
|
|
|
|
continue
|
|
|
|
|
formats.append({
|
|
|
|
|
'format_id': quality_id,
|
|
|
|
|
'url': update_url_query(file_api, {'vid': file_id}),
|
2021-02-18 15:03:16 -07:00
|
|
|
|
'quality': preference(quality_id),
|
2016-05-13 13:24:36 -06:00
|
|
|
|
'ext': 'mp4',
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
'id': video_id,
|
|
|
|
|
'title': title,
|
|
|
|
|
'description': description,
|
|
|
|
|
'thumbnail': video_data.get('image'),
|
|
|
|
|
'duration': int_or_none(video_data.get('length')),
|
|
|
|
|
'timestamp': int_or_none(video_data.get('create_time')),
|
|
|
|
|
'formats': formats,
|
|
|
|
|
}
|