0891a441f85f42b75d91f1d267fabdd1b5e952ce
[youtube-dl] / 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 ..compat import (
8     compat_urllib_request,
9     compat_urllib_parse,
10 )
11
12
13 class SinaIE(InfoExtractor):
14     _VALID_URL = r'''(?x)https?://(.*?\.)?video\.sina\.com\.cn/
15                         (
16                             (.+?/(((?P<pseudo_id>\d+).html)|(.*?(\#|(vid=)|b/)(?P<id>\d+?)($|&|\-))))
17                             |
18                             # This is used by external sites like Weibo
19                             (api/sinawebApi/outplay.php/(?P<token>.+?)\.swf)
20                         )
21                   '''
22
23     _TESTS = [
24         {
25             'url': 'http://video.sina.com.cn/news/vlist/zt/chczlj2013/?opsubject_id=top12#110028898',
26             'md5': 'd65dd22ddcf44e38ce2bf58a10c3e71f',
27             'info_dict': {
28                 'id': '110028898',
29                 'ext': 'flv',
30                 'title': '《中国新闻》 朝鲜要求巴拿马立即释放被扣船员',
31             }
32         },
33         {
34             'url': 'http://video.sina.com.cn/v/b/101314253-1290078633.html',
35             'info_dict': {
36                 'id': '101314253',
37                 'ext': 'flv',
38                 'title': '军方提高对朝情报监视级别',
39             },
40         },
41     ]
42
43     def _extract_video(self, video_id):
44         data = compat_urllib_parse.urlencode({'vid': video_id})
45         url_doc = self._download_xml('http://v.iask.com/v_play.php?%s' % data,
46                                      video_id, 'Downloading video url')
47         image_page = self._download_webpage(
48             'http://interface.video.sina.com.cn/interface/common/getVideoImage.php?%s' % data,
49             video_id, 'Downloading thumbnail info')
50
51         return {'id': video_id,
52                 'url': url_doc.find('./durl/url').text,
53                 'ext': 'flv',
54                 'title': url_doc.find('./vname').text,
55                 'thumbnail': image_page.split('=')[1],
56                 }
57
58     def _real_extract(self, url):
59         mobj = re.match(self._VALID_URL, url)
60         video_id = mobj.group('id')
61         if mobj.group('token') is not None:
62             # The video id is in the redirected url
63             self.to_screen('Getting video id')
64             request = compat_urllib_request.Request(url)
65             request.get_method = lambda: 'HEAD'
66             (_, urlh) = self._download_webpage_handle(request, 'NA', False)
67             return self._real_extract(urlh.geturl())
68         elif video_id is None:
69             pseudo_id = mobj.group('pseudo_id')
70             webpage = self._download_webpage(url, pseudo_id)
71             video_id = self._search_regex(r'vid:\'(\d+?)\'', webpage, 'video id')
72
73         return self._extract_video(video_id)