logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

zhihu.py (2626B)


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