]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/sina.py
Imported Upstream version 2013.10.23
[youtubedl] / youtube_dl / extractor / sina.py
1 # coding: utf-8
2
3 import re
4 import xml.etree.ElementTree
5
6 from .common import InfoExtractor
7 from ..utils import (
8 compat_urllib_request,
9 compat_urllib_parse,
10 )
11
12
13 class SinaIE(InfoExtractor):
14 _VALID_URL = r'''https?://(.*?\.)?video\.sina\.com\.cn/
15 (
16 (.+?/(((?P<pseudo_id>\d+).html)|(.*?(\#|(vid=))(?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 _TEST = {
24 u'url': u'http://video.sina.com.cn/news/vlist/zt/chczlj2013/?opsubject_id=top12#110028898',
25 u'file': u'110028898.flv',
26 u'md5': u'd65dd22ddcf44e38ce2bf58a10c3e71f',
27 u'info_dict': {
28 u'title': u'《中国新闻》 朝鲜要求巴拿马立即释放被扣船员',
29 }
30 }
31
32 @classmethod
33 def suitable(cls, url):
34 return re.match(cls._VALID_URL, url, flags=re.VERBOSE) is not None
35
36 def _extract_video(self, video_id):
37 data = compat_urllib_parse.urlencode({'vid': video_id})
38 url_page = self._download_webpage('http://v.iask.com/v_play.php?%s' % data,
39 video_id, u'Downloading video url')
40 image_page = self._download_webpage(
41 'http://interface.video.sina.com.cn/interface/common/getVideoImage.php?%s' % data,
42 video_id, u'Downloading thumbnail info')
43 url_doc = xml.etree.ElementTree.fromstring(url_page.encode('utf-8'))
44
45 return {'id': video_id,
46 'url': url_doc.find('./durl/url').text,
47 'ext': 'flv',
48 'title': url_doc.find('./vname').text,
49 'thumbnail': image_page.split('=')[1],
50 }
51
52 def _real_extract(self, url):
53 mobj = re.match(self._VALID_URL, url, flags=re.VERBOSE)
54 video_id = mobj.group('id')
55 if mobj.group('token') is not None:
56 # The video id is in the redirected url
57 self.to_screen(u'Getting video id')
58 request = compat_urllib_request.Request(url)
59 request.get_method = lambda: 'HEAD'
60 (_, urlh) = self._download_webpage_handle(request, 'NA', False)
61 return self._real_extract(urlh.geturl())
62 elif video_id is None:
63 pseudo_id = mobj.group('pseudo_id')
64 webpage = self._download_webpage(url, pseudo_id)
65 video_id = self._search_regex(r'vid:\'(\d+?)\'', webpage, u'video id')
66
67 return self._extract_video(video_id)