]>
Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/sina.py
   2 from __future__ 
import unicode_literals
 
   6 from .common 
import InfoExtractor
 
  13     get_element_by_attribute
, 
  18 class SinaIE(InfoExtractor
): 
  19     _VALID_URL 
= r
'''(?x)https?://(?:.*?\.)?video\.sina\.com\.cn/ 
  21                             (?:view/|.*\#)(?P<video_id>\d+)| 
  22                             .+?/(?P<pseudo_id>[^/?#]+)(?:\.s?html)| 
  23                             # This is used by external sites like Weibo 
  24                             api/sinawebApi/outplay.php/(?P<token>.+?)\.swf 
  30             'url': 'http://video.sina.com.cn/news/spj/topvideoes20160504/?opsubject_id=top1#250576622', 
  31             'md5': 'd38433e2fc886007729735650ae4b3e9', 
  35                 'title': '现场:克鲁兹宣布退选 特朗普将稳获提名', 
  39             'url': 'http://video.sina.com.cn/v/b/101314253-1290078633.html', 
  43                 'title': '军方提高对朝情报监视级别', 
  45             'skip': 'the page does not exist or has been deleted', 
  48             'url': 'http://video.sina.com.cn/view/250587748.html', 
  49             'md5': '3d1807a25c775092aab3bc157fff49b4', 
  53                 'title': '瞬间泪目:8年前汶川地震珍贵视频首曝光', 
  58     def _real_extract(self
, url
): 
  59         mobj 
= re
.match(self
._VALID
_URL
, url
) 
  61         video_id 
= mobj
.group('video_id') 
  63             if mobj
.group('token') is not None: 
  64                 # The video id is in the redirected url 
  65                 self
.to_screen('Getting video id') 
  66                 request 
= HEADRequest(url
) 
  67                 _
, urlh 
= self
._download
_webpage
_handle
(request
, 'NA', False) 
  68                 return self
._real
_extract
(urlh
.geturl()) 
  70                 pseudo_id 
= mobj
.group('pseudo_id') 
  71                 webpage 
= self
._download
_webpage
(url
, pseudo_id
) 
  72                 error 
= get_element_by_attribute('class', 'errtitle', webpage
) 
  74                     raise ExtractorError('%s said: %s' % ( 
  75                         self
.IE_NAME
, clean_html(error
)), expected
=True) 
  76                 video_id 
= self
._search
_regex
( 
  77                     r
"video_id\s*:\s*'(\d+)'", webpage
, 'video id') 
  79         video_data 
= self
._download
_json
( 
  80             'http://s.video.sina.com.cn/video/h5play', 
  81             video_id
, query
={'video_id': video_id
}) 
  82         if video_data
['code'] != 1: 
  83             raise ExtractorError('%s said: %s' % ( 
  84                 self
.IE_NAME
, video_data
['message']), expected
=True) 
  86             video_data 
= video_data
['data'] 
  87             title 
= video_data
['title'] 
  88             description 
= video_data
.get('description') 
  90                 description 
= description
.strip() 
  92             preference 
= qualities(['cif', 'sd', 'hd', 'fhd', 'ffd']) 
  94             for quality_id
, quality 
in video_data
.get('videos', {}).get('mp4', {}).items(): 
  95                 file_api 
= quality
.get('file_api') 
  96                 file_id 
= quality
.get('file_id') 
  97                 if not file_api 
or not file_id
: 
 100                     'format_id': quality_id
, 
 101                     'url': update_url_query(file_api
, {'vid': file_id
}), 
 102                     'preference': preference(quality_id
), 
 105             self
._sort
_formats
(formats
) 
 110                 'description': description
, 
 111                 'thumbnail': video_data
.get('image'), 
 112                 'duration': int_or_none(video_data
.get('length')), 
 113                 'timestamp': int_or_none(video_data
.get('create_time')),