]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/xinpianchang.py
[cleanup] Fix infodict returned fields (#8906)
[yt-dlp.git] / yt_dlp / extractor / xinpianchang.py
1 from .common import InfoExtractor
2 from ..utils import (
3 int_or_none,
4 str_or_none,
5 try_get,
6 update_url_query,
7 url_or_none,
8 )
9
10
11 class XinpianchangIE(InfoExtractor):
12 _VALID_URL = r'https?://www\.xinpianchang\.com/(?P<id>[^/]+?)(?:\D|$)'
13 IE_NAME = 'xinpianchang'
14 IE_DESC = 'xinpianchang.com'
15 _TESTS = [{
16 'url': 'https://www.xinpianchang.com/a11766551',
17 'info_dict': {
18 'id': 'a11766551',
19 'ext': 'mp4',
20 'title': '北京2022冬奥会闭幕式再见短片-冰墩墩下班了',
21 'description': 'md5:4a730c10639a82190fabe921c0fa4b87',
22 'duration': 151,
23 'thumbnail': r're:^https?://oss-xpc0\.xpccdn\.com.+/assets/',
24 'uploader': '正时文创',
25 'uploader_id': '10357277',
26 'categories': ['宣传片', '国家城市', '广告', '其他'],
27 'tags': ['北京冬奥会', '冰墩墩', '再见', '告别', '冰墩墩哭了', '感动', '闭幕式', '熄火']
28 },
29 }, {
30 'url': 'https://www.xinpianchang.com/a11762904',
31 'info_dict': {
32 'id': 'a11762904',
33 'ext': 'mp4',
34 'title': '冬奥会决胜时刻《法国派出三只鸡?》',
35 'description': 'md5:55cb139ef8f48f0c877932d1f196df8b',
36 'duration': 136,
37 'thumbnail': r're:^https?://oss-xpc0\.xpccdn\.com.+/assets/',
38 'uploader': '精品动画',
39 'uploader_id': '10858927',
40 'categories': ['动画', '三维CG'],
41 'tags': ['France Télévisions', '法国3台', '蠢萌', '冬奥会']
42 },
43 }, {
44 'url': 'https://www.xinpianchang.com/a11779743?from=IndexPick&part=%E7%BC%96%E8%BE%91%E7%B2%BE%E9%80%89&index=2',
45 'only_matching': True,
46 }]
47
48 def _real_extract(self, url):
49 video_id = self._match_id(url)
50 webpage = self._download_webpage(url, video_id=video_id)
51 domain = self.find_value_with_regex(var='requireNewDomain', webpage=webpage)
52 vid = self.find_value_with_regex(var='vid', webpage=webpage)
53 app_key = self.find_value_with_regex(var='modeServerAppKey', webpage=webpage)
54 api = update_url_query(f'{domain}/mod/api/v2/media/{vid}', {'appKey': app_key})
55 data = self._download_json(api, video_id=video_id)['data']
56 formats, subtitles = [], {}
57 for k, v in data.get('resource').items():
58 if k in ('dash', 'hls'):
59 v_url = v.get('url')
60 if not v_url:
61 continue
62 if k == 'dash':
63 fmts, subs = self._extract_mpd_formats_and_subtitles(v_url, video_id=video_id)
64 elif k == 'hls':
65 fmts, subs = self._extract_m3u8_formats_and_subtitles(v_url, video_id=video_id)
66 formats.extend(fmts)
67 subtitles = self._merge_subtitles(subtitles, subs)
68 elif k == 'progressive':
69 formats.extend([{
70 'url': url_or_none(prog.get('url')),
71 'width': int_or_none(prog.get('width')),
72 'height': int_or_none(prog.get('height')),
73 'ext': 'mp4',
74 } for prog in v if prog.get('url') or []])
75
76 return {
77 'id': video_id,
78 'title': data.get('title'),
79 'description': data.get('description'),
80 'duration': int_or_none(data.get('duration')),
81 'categories': data.get('categories'),
82 'tags': data.get('keywords'),
83 'thumbnail': data.get('cover'),
84 'uploader': try_get(data, lambda x: x['owner']['username']),
85 'uploader_id': str_or_none(try_get(data, lambda x: x['owner']['id'])),
86 'formats': formats,
87 'subtitles': subtitles,
88 }
89
90 def find_value_with_regex(self, var, webpage):
91 return self._search_regex(rf'var\s{var}\s=\s\"(?P<vid>[^\"]+)\"', webpage, name=var)