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.

76 lines
2.8 KiB

  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=)|b/)(?P<id>\d+?)($|&|\-))))
  13. |
  14. # This is used by external sites like Weibo
  15. (api/sinawebApi/outplay.php/(?P<token>.+?)\.swf)
  16. )
  17. '''
  18. _TESTS = [
  19. {
  20. 'url': 'http://video.sina.com.cn/news/vlist/zt/chczlj2013/?opsubject_id=top12#110028898',
  21. 'file': '110028898.flv',
  22. 'md5': 'd65dd22ddcf44e38ce2bf58a10c3e71f',
  23. 'info_dict': {
  24. 'title': '《中国新闻》 朝鲜要求巴拿马立即释放被扣船员',
  25. }
  26. },
  27. {
  28. 'url': 'http://video.sina.com.cn/v/b/101314253-1290078633.html',
  29. 'info_dict': {
  30. 'id': '101314253',
  31. 'ext': 'flv',
  32. 'title': '军方提高对朝情报监视级别',
  33. },
  34. },
  35. ]
  36. @classmethod
  37. def suitable(cls, url):
  38. return re.match(cls._VALID_URL, url, flags=re.VERBOSE) is not None
  39. def _extract_video(self, video_id):
  40. data = compat_urllib_parse.urlencode({'vid': video_id})
  41. url_doc = self._download_xml('http://v.iask.com/v_play.php?%s' % data,
  42. video_id, 'Downloading video url')
  43. image_page = self._download_webpage(
  44. 'http://interface.video.sina.com.cn/interface/common/getVideoImage.php?%s' % data,
  45. video_id, 'Downloading thumbnail info')
  46. return {'id': video_id,
  47. 'url': url_doc.find('./durl/url').text,
  48. 'ext': 'flv',
  49. 'title': url_doc.find('./vname').text,
  50. 'thumbnail': image_page.split('=')[1],
  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('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, 'video id')
  66. return self._extract_video(video_id)