sina.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. compat_urllib_request,
  7. compat_urllib_parse,
  8. )
  9. class SinaIE(InfoExtractor):
  10. _VALID_URL = r'''https?://(.*?\.)?video\.sina\.com\.cn/
  11. (
  12. (.+?/(((?P<pseudo_id>\d+).html)|(.*?(\#|(vid=))(?P<id>\d+?)($|&))))
  13. |
  14. # This is used by external sites like Weibo
  15. (api/sinawebApi/outplay.php/(?P<token>.+?)\.swf)
  16. )
  17. '''
  18. _TEST = {
  19. 'url': 'http://video.sina.com.cn/news/vlist/zt/chczlj2013/?opsubject_id=top12#110028898',
  20. 'file': '110028898.flv',
  21. 'md5': 'd65dd22ddcf44e38ce2bf58a10c3e71f',
  22. 'info_dict': {
  23. 'title': '《中国新闻》 朝鲜要求巴拿马立即释放被扣船员',
  24. }
  25. }
  26. @classmethod
  27. def suitable(cls, url):
  28. return re.match(cls._VALID_URL, url, flags=re.VERBOSE) is not None
  29. def _extract_video(self, video_id):
  30. data = compat_urllib_parse.urlencode({'vid': video_id})
  31. url_doc = self._download_xml('http://v.iask.com/v_play.php?%s' % data,
  32. video_id, 'Downloading video url')
  33. image_page = self._download_webpage(
  34. 'http://interface.video.sina.com.cn/interface/common/getVideoImage.php?%s' % data,
  35. video_id, 'Downloading thumbnail info')
  36. return {'id': video_id,
  37. 'url': url_doc.find('./durl/url').text,
  38. 'ext': 'flv',
  39. 'title': url_doc.find('./vname').text,
  40. 'thumbnail': image_page.split('=')[1],
  41. }
  42. def _real_extract(self, url):
  43. mobj = re.match(self._VALID_URL, url, flags=re.VERBOSE)
  44. video_id = mobj.group('id')
  45. if mobj.group('token') is not None:
  46. # The video id is in the redirected url
  47. self.to_screen('Getting video id')
  48. request = compat_urllib_request.Request(url)
  49. request.get_method = lambda: 'HEAD'
  50. (_, urlh) = self._download_webpage_handle(request, 'NA', False)
  51. return self._real_extract(urlh.geturl())
  52. elif video_id is None:
  53. pseudo_id = mobj.group('pseudo_id')
  54. webpage = self._download_webpage(url, pseudo_id)
  55. video_id = self._search_regex(r'vid:\'(\d+?)\'', webpage, 'video id')
  56. return self._extract_video(video_id)