You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
2.4 KiB

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