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.

115 lines
4.2 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. HEADRequest,
  7. ExtractorError,
  8. int_or_none,
  9. update_url_query,
  10. qualities,
  11. get_element_by_attribute,
  12. clean_html,
  13. )
  14. class SinaIE(InfoExtractor):
  15. _VALID_URL = r'''(?x)https?://(?:.*?\.)?video\.sina\.com\.cn/
  16. (?:
  17. (?:view/|.*\#)(?P<video_id>\d+)|
  18. .+?/(?P<pseudo_id>[^/?#]+)(?:\.s?html)|
  19. # This is used by external sites like Weibo
  20. api/sinawebApi/outplay.php/(?P<token>.+?)\.swf
  21. )
  22. '''
  23. _TESTS = [
  24. {
  25. 'url': 'http://video.sina.com.cn/news/spj/topvideoes20160504/?opsubject_id=top1#250576622',
  26. 'md5': 'd38433e2fc886007729735650ae4b3e9',
  27. 'info_dict': {
  28. 'id': '250576622',
  29. 'ext': 'mp4',
  30. 'title': '现场:克鲁兹宣布退选 特朗普将稳获提名',
  31. }
  32. },
  33. {
  34. 'url': 'http://video.sina.com.cn/v/b/101314253-1290078633.html',
  35. 'info_dict': {
  36. 'id': '101314253',
  37. 'ext': 'flv',
  38. 'title': '军方提高对朝情报监视级别',
  39. },
  40. 'skip': 'the page does not exist or has been deleted',
  41. },
  42. {
  43. 'url': 'http://video.sina.com.cn/view/250587748.html',
  44. 'md5': '3d1807a25c775092aab3bc157fff49b4',
  45. 'info_dict': {
  46. 'id': '250587748',
  47. 'ext': 'mp4',
  48. 'title': '瞬间泪目:8年前汶川地震珍贵视频首曝光',
  49. },
  50. },
  51. ]
  52. def _real_extract(self, url):
  53. mobj = re.match(self._VALID_URL, url)
  54. video_id = mobj.group('video_id')
  55. if not video_id:
  56. if mobj.group('token') is not None:
  57. # The video id is in the redirected url
  58. self.to_screen('Getting video id')
  59. request = HEADRequest(url)
  60. (_, urlh) = self._download_webpage_handle(request, 'NA', False)
  61. return self._real_extract(urlh.geturl())
  62. else:
  63. pseudo_id = mobj.group('pseudo_id')
  64. webpage = self._download_webpage(url, pseudo_id)
  65. error = get_element_by_attribute('class', 'errtitle', webpage)
  66. if error:
  67. raise ExtractorError('%s said: %s' % (
  68. self.IE_NAME, clean_html(error)), expected=True)
  69. video_id = self._search_regex(
  70. r"video_id\s*:\s*'(\d+)'", webpage, 'video id')
  71. video_data = self._download_json(
  72. 'http://s.video.sina.com.cn/video/h5play',
  73. video_id, query={'video_id': video_id})
  74. if video_data['code'] != 1:
  75. raise ExtractorError('%s said: %s' % (
  76. self.IE_NAME, video_data['message']), expected=True)
  77. else:
  78. video_data = video_data['data']
  79. title = video_data['title']
  80. description = video_data.get('description')
  81. if description:
  82. description = description.strip()
  83. preference = qualities(['cif', 'sd', 'hd', 'fhd', 'ffd'])
  84. formats = []
  85. for quality_id, quality in video_data.get('videos', {}).get('mp4', {}).items():
  86. file_api = quality.get('file_api')
  87. file_id = quality.get('file_id')
  88. if not file_api or not file_id:
  89. continue
  90. formats.append({
  91. 'format_id': quality_id,
  92. 'url': update_url_query(file_api, {'vid': file_id}),
  93. 'preference': preference(quality_id),
  94. 'ext': 'mp4',
  95. })
  96. self._sort_formats(formats)
  97. return {
  98. 'id': video_id,
  99. 'title': title,
  100. 'description': description,
  101. 'thumbnail': video_data.get('image'),
  102. 'duration': int_or_none(video_data.get('length')),
  103. 'timestamp': int_or_none(video_data.get('create_time')),
  104. 'formats': formats,
  105. }