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