zhihu.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. from .common import InfoExtractor
  2. from ..utils import float_or_none, format_field, int_or_none
  3. class ZhihuIE(InfoExtractor):
  4. _VALID_URL = r'https?://(?:www\.)?zhihu\.com/zvideo/(?P<id>[0-9]+)'
  5. _TEST = {
  6. 'url': 'https://www.zhihu.com/zvideo/1342930761977176064',
  7. 'md5': 'c8d4c9cd72dd58e6f9bc9c2c84266464',
  8. 'info_dict': {
  9. 'id': '1342930761977176064',
  10. 'ext': 'mp4',
  11. 'title': '写春联也太难了吧!',
  12. 'thumbnail': r're:^https?://.*\.jpg',
  13. 'uploader': '桥半舫',
  14. 'timestamp': 1612959715,
  15. 'upload_date': '20210210',
  16. 'uploader_id': '244ecb13b0fd7daf92235288c8ca3365',
  17. 'duration': 146.333,
  18. 'view_count': int,
  19. 'like_count': int,
  20. 'comment_count': int,
  21. },
  22. }
  23. def _real_extract(self, url):
  24. video_id = self._match_id(url)
  25. zvideo = self._download_json(
  26. 'https://www.zhihu.com/api/v4/zvideos/' + video_id, video_id)
  27. title = zvideo['title']
  28. video = zvideo.get('video') or {}
  29. formats = []
  30. for format_id, q in (video.get('playlist') or {}).items():
  31. play_url = q.get('url') or q.get('play_url')
  32. if not play_url:
  33. continue
  34. formats.append({
  35. 'asr': int_or_none(q.get('sample_rate')),
  36. 'filesize': int_or_none(q.get('size')),
  37. 'format_id': format_id,
  38. 'fps': int_or_none(q.get('fps')),
  39. 'height': int_or_none(q.get('height')),
  40. 'tbr': float_or_none(q.get('bitrate')),
  41. 'url': play_url,
  42. 'width': int_or_none(q.get('width')),
  43. })
  44. author = zvideo.get('author') or {}
  45. url_token = author.get('url_token')
  46. return {
  47. 'id': video_id,
  48. 'title': title,
  49. 'formats': formats,
  50. 'thumbnail': video.get('thumbnail') or zvideo.get('image_url'),
  51. 'uploader': author.get('name'),
  52. 'timestamp': int_or_none(zvideo.get('published_at')),
  53. 'uploader_id': author.get('id'),
  54. 'uploader_url': format_field(url_token, None, 'https://www.zhihu.com/people/%s'),
  55. 'duration': float_or_none(video.get('duration')),
  56. 'view_count': int_or_none(zvideo.get('play_count')),
  57. 'like_count': int_or_none(zvideo.get('liked_count')),
  58. 'comment_count': int_or_none(zvideo.get('comment_count')),
  59. }