sina.py 4.1 KB

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