]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/zhihu.py
[misc] Add `hatch`, `ruff`, `pre-commit` and improve dev docs (#7409)
[yt-dlp.git] / yt_dlp / extractor / zhihu.py
1 from .common import InfoExtractor
2 from ..utils import float_or_none, format_field, int_or_none
3
4
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
26 def _real_extract(self, url):
27 video_id = self._match_id(url)
28 zvideo = self._download_json(
29 'https://www.zhihu.com/api/v4/zvideos/' + video_id, video_id)
30 title = zvideo['title']
31 video = zvideo.get('video') or {}
32
33 formats = []
34 for format_id, q in (video.get('playlist') or {}).items():
35 play_url = q.get('url') or q.get('play_url')
36 if not play_url:
37 continue
38 formats.append({
39 'asr': int_or_none(q.get('sample_rate')),
40 'filesize': int_or_none(q.get('size')),
41 'format_id': format_id,
42 'fps': int_or_none(q.get('fps')),
43 'height': int_or_none(q.get('height')),
44 'tbr': float_or_none(q.get('bitrate')),
45 'url': play_url,
46 'width': int_or_none(q.get('width')),
47 })
48
49 author = zvideo.get('author') or {}
50 url_token = author.get('url_token')
51
52 return {
53 'id': video_id,
54 'title': title,
55 'formats': formats,
56 'thumbnail': video.get('thumbnail') or zvideo.get('image_url'),
57 'uploader': author.get('name'),
58 'timestamp': int_or_none(zvideo.get('published_at')),
59 'uploader_id': author.get('id'),
60 'uploader_url': format_field(url_token, None, 'https://www.zhihu.com/people/%s'),
61 'duration': float_or_none(video.get('duration')),
62 'view_count': int_or_none(zvideo.get('play_count')),
63 'like_count': int_or_none(zvideo.get('liked_count')),
64 'comment_count': int_or_none(zvideo.get('comment_count')),
65 }