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.

67 lines
2.5 KiB

  1. # coding: utf-8
  2. import re
  3. import xml.etree.ElementTree
  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. u'url': u'http://video.sina.com.cn/news/vlist/zt/chczlj2013/?opsubject_id=top12#110028898',
  20. u'file': u'110028898.flv',
  21. u'md5': u'd65dd22ddcf44e38ce2bf58a10c3e71f',
  22. u'info_dict': {
  23. u'title': u'《中国新闻》 朝鲜要求巴拿马立即释放被扣船员',
  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_page = self._download_webpage('http://v.iask.com/v_play.php?%s' % data,
  32. video_id, u'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, u'Downloading thumbnail info')
  36. url_doc = xml.etree.ElementTree.fromstring(url_page.encode('utf-8'))
  37. return {'id': video_id,
  38. 'url': url_doc.find('./durl/url').text,
  39. 'ext': 'flv',
  40. 'title': url_doc.find('./vname').text,
  41. 'thumbnail': image_page.split('=')[1],
  42. }
  43. def _real_extract(self, url):
  44. mobj = re.match(self._VALID_URL, url, flags=re.VERBOSE)
  45. video_id = mobj.group('id')
  46. if mobj.group('token') is not None:
  47. # The video id is in the redirected url
  48. self.to_screen(u'Getting video id')
  49. request = compat_urllib_request.Request(url)
  50. request.get_method = lambda: 'HEAD'
  51. (_, urlh) = self._download_webpage_handle(request, 'NA', False)
  52. return self._real_extract(urlh.geturl())
  53. elif video_id is None:
  54. pseudo_id = mobj.group('pseudo_id')
  55. webpage = self._download_webpage(url, pseudo_id)
  56. video_id = self._search_regex(r'vid:\'(\d+?)\'', webpage, u'video id')
  57. return self._extract_video(video_id)