]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/sina.py
[cleanup] Misc
[yt-dlp.git] / yt_dlp / extractor / sina.py
CommitLineData
0932300e 1# coding: utf-8
76f270a4 2from __future__ import unicode_literals
0932300e 3
0932300e
JMF
4
5from .common import InfoExtractor
0730be90 6from ..utils import (
7 HEADRequest,
8 ExtractorError,
9 int_or_none,
10 update_url_query,
11 qualities,
12 get_element_by_attribute,
13 clean_html,
14)
0932300e
JMF
15
16
17class SinaIE(InfoExtractor):
0730be90 18 _VALID_URL = r'''(?x)https?://(?:.*?\.)?video\.sina\.com\.cn/
19 (?:
d3d8d818 20 (?:view/|.*\#)(?P<id>\d+)|
0730be90 21 .+?/(?P<pseudo_id>[^/?#]+)(?:\.s?html)|
0932300e 22 # This is used by external sites like Weibo
0730be90 23 api/sinawebApi/outplay.php/(?P<token>.+?)\.swf
0932300e
JMF
24 )
25 '''
26
8b769664
JMF
27 _TESTS = [
28 {
0730be90 29 'url': 'http://video.sina.com.cn/news/spj/topvideoes20160504/?opsubject_id=top1#250576622',
30 'md5': 'd38433e2fc886007729735650ae4b3e9',
8b769664 31 'info_dict': {
0730be90 32 'id': '250576622',
33 'ext': 'mp4',
34 'title': '现场:克鲁兹宣布退选 特朗普将稳获提名',
8b769664
JMF
35 }
36 },
37 {
38 'url': 'http://video.sina.com.cn/v/b/101314253-1290078633.html',
39 'info_dict': {
40 'id': '101314253',
41 'ext': 'flv',
42 'title': '军方提高对朝情报监视级别',
43 },
0730be90 44 'skip': 'the page does not exist or has been deleted',
45 },
46 {
47 'url': 'http://video.sina.com.cn/view/250587748.html',
48 'md5': '3d1807a25c775092aab3bc157fff49b4',
49 'info_dict': {
50 'id': '250587748',
51 'ext': 'mp4',
52 'title': '瞬间泪目:8年前汶川地震珍贵视频首曝光',
53 },
8b769664
JMF
54 },
55 ]
0932300e 56
0932300e 57 def _real_extract(self, url):
5ad28e7f 58 mobj = self._match_valid_url(url)
0932300e 59
d3d8d818 60 video_id = mobj.group('id')
0730be90 61 if not video_id:
62 if mobj.group('token') is not None:
63 # The video id is in the redirected url
64 self.to_screen('Getting video id')
65 request = HEADRequest(url)
68217024 66 _, urlh = self._download_webpage_handle(request, 'NA', False)
0730be90 67 return self._real_extract(urlh.geturl())
68 else:
69 pseudo_id = mobj.group('pseudo_id')
70 webpage = self._download_webpage(url, pseudo_id)
71 error = get_element_by_attribute('class', 'errtitle', webpage)
72 if error:
73 raise ExtractorError('%s said: %s' % (
74 self.IE_NAME, clean_html(error)), expected=True)
75 video_id = self._search_regex(
76 r"video_id\s*:\s*'(\d+)'", webpage, 'video id')
77
78 video_data = self._download_json(
79 'http://s.video.sina.com.cn/video/h5play',
80 video_id, query={'video_id': video_id})
81 if video_data['code'] != 1:
82 raise ExtractorError('%s said: %s' % (
83 self.IE_NAME, video_data['message']), expected=True)
84 else:
85 video_data = video_data['data']
86 title = video_data['title']
87 description = video_data.get('description')
88 if description:
89 description = description.strip()
90
91 preference = qualities(['cif', 'sd', 'hd', 'fhd', 'ffd'])
92 formats = []
93 for quality_id, quality in video_data.get('videos', {}).get('mp4', {}).items():
94 file_api = quality.get('file_api')
95 file_id = quality.get('file_id')
96 if not file_api or not file_id:
97 continue
98 formats.append({
99 'format_id': quality_id,
100 'url': update_url_query(file_api, {'vid': file_id}),
f983b875 101 'quality': preference(quality_id),
0730be90 102 'ext': 'mp4',
103 })
104 self._sort_formats(formats)
105
106 return {
107 'id': video_id,
108 'title': title,
109 'description': description,
110 'thumbnail': video_data.get('image'),
111 'duration': int_or_none(video_data.get('length')),
112 'timestamp': int_or_none(video_data.get('create_time')),
113 'formats': formats,
114 }