]> gitweb @ CieloNegro.org - youtube-dl.git/blob - youtube_dl/extractor/sina.py
[nhk] add support for audio URLs
[youtube-dl.git] / youtube_dl / extractor / sina.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8     HEADRequest,
9     ExtractorError,
10     int_or_none,
11     update_url_query,
12     qualities,
13     get_element_by_attribute,
14     clean_html,
15 )
16
17
18 class SinaIE(InfoExtractor):
19     _VALID_URL = r'''(?x)https?://(?:.*?\.)?video\.sina\.com\.cn/
20                         (?:
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
25                         )
26                   '''
27
28     _TESTS = [
29         {
30             'url': 'http://video.sina.com.cn/news/spj/topvideoes20160504/?opsubject_id=top1#250576622',
31             'md5': 'd38433e2fc886007729735650ae4b3e9',
32             'info_dict': {
33                 'id': '250576622',
34                 'ext': 'mp4',
35                 'title': '现场:克鲁兹宣布退选 特朗普将稳获提名',
36             }
37         },
38         {
39             'url': 'http://video.sina.com.cn/v/b/101314253-1290078633.html',
40             'info_dict': {
41                 'id': '101314253',
42                 'ext': 'flv',
43                 'title': '军方提高对朝情报监视级别',
44             },
45             'skip': 'the page does not exist or has been deleted',
46         },
47         {
48             'url': 'http://video.sina.com.cn/view/250587748.html',
49             'md5': '3d1807a25c775092aab3bc157fff49b4',
50             'info_dict': {
51                 'id': '250587748',
52                 'ext': 'mp4',
53                 'title': '瞬间泪目:8年前汶川地震珍贵视频首曝光',
54             },
55         },
56     ]
57
58     def _real_extract(self, url):
59         mobj = re.match(self._VALID_URL, url)
60
61         video_id = mobj.group('video_id')
62         if not 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())
69             else:
70                 pseudo_id = mobj.group('pseudo_id')
71                 webpage = self._download_webpage(url, pseudo_id)
72                 error = get_element_by_attribute('class', 'errtitle', webpage)
73                 if error:
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')
78
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)
85         else:
86             video_data = video_data['data']
87             title = video_data['title']
88             description = video_data.get('description')
89             if description:
90                 description = description.strip()
91
92             preference = qualities(['cif', 'sd', 'hd', 'fhd', 'ffd'])
93             formats = []
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:
98                     continue
99                 formats.append({
100                     'format_id': quality_id,
101                     'url': update_url_query(file_api, {'vid': file_id}),
102                     'preference': preference(quality_id),
103                     'ext': 'mp4',
104                 })
105             self._sort_formats(formats)
106
107             return {
108                 'id': video_id,
109                 'title': title,
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')),
114                 'formats': formats,
115             }