]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/swearnet.py
[ie/orf:on] Improve extraction (#9677)
[yt-dlp.git] / yt_dlp / extractor / swearnet.py
CommitLineData
049565df 1from .common import InfoExtractor
b05640d5 2from ..utils import ExtractorError, int_or_none, traverse_obj
049565df
H
3
4
5class SwearnetEpisodeIE(InfoExtractor):
6 _VALID_URL = r'https?://www\.swearnet\.com/shows/(?P<id>[\w-]+)/seasons/(?P<season_num>\d+)/episodes/(?P<episode_num>\d+)'
7 _TESTS = [{
8 'url': 'https://www.swearnet.com/shows/gettin-learnt-with-ricky/seasons/1/episodes/1',
9 'info_dict': {
10 'id': '232819',
11 'ext': 'mp4',
12 'episode_number': 1,
13 'episode': 'Episode 1',
14 'duration': 719,
15 'description': 'md5:c48ef71440ce466284c07085cd7bd761',
16 'season': 'Season 1',
17 'title': 'Episode 1 - Grilled Cheese Sammich',
18 'season_number': 1,
19 'thumbnail': 'https://cdn.vidyard.com/thumbnails/232819/_RX04IKIq60a2V6rIRqq_Q_small.jpg',
20 }
21 }]
22
23 def _get_formats_and_subtitle(self, video_source, video_id):
24 video_source = video_source or {}
25 formats, subtitles = [], {}
26 for key, value in video_source.items():
27 if key == 'hls':
28 for video_hls in value:
29 fmts, subs = self._extract_m3u8_formats_and_subtitles(video_hls.get('url'), video_id)
30 formats.extend(fmts)
31 self._merge_subtitles(subs, target=subtitles)
32 else:
33 formats.extend({
34 'url': video_mp4.get('url'),
35 'ext': 'mp4'
36 } for video_mp4 in value)
37
38 return formats, subtitles
39
40 def _get_direct_subtitle(self, caption_json):
41 subs = {}
42 for caption in caption_json:
43 subs.setdefault(caption.get('language') or 'und', []).append({
44 'url': caption.get('vttUrl'),
45 'name': caption.get('name')
46 })
47
48 return subs
49
50 def _real_extract(self, url):
51 display_id, season_number, episode_number = self._match_valid_url(url).group('id', 'season_num', 'episode_num')
52 webpage = self._download_webpage(url, display_id)
53
b05640d5 54 try:
55 external_id = self._search_regex(r'externalid\s*=\s*"([^"]+)', webpage, 'externalid')
56 except ExtractorError:
57 if 'Upgrade Now' in webpage:
58 self.raise_login_required()
59 raise
60
049565df
H
61 json_data = self._download_json(
62 f'https://play.vidyard.com/player/{external_id}.json', display_id)['payload']['chapters'][0]
63
64 formats, subtitles = self._get_formats_and_subtitle(json_data['sources'], display_id)
65 self._merge_subtitles(self._get_direct_subtitle(json_data.get('captions')), target=subtitles)
66
67 return {
68 'id': str(json_data['videoId']),
69 'title': json_data.get('name') or self._html_search_meta(['og:title', 'twitter:title'], webpage),
70 'description': (json_data.get('description')
ddf1e22d 71 or self._html_search_meta(['og:description', 'twitter:description'], webpage)),
049565df
H
72 'duration': int_or_none(json_data.get('seconds')),
73 'formats': formats,
74 'subtitles': subtitles,
75 'season_number': int_or_none(season_number),
76 'episode_number': int_or_none(episode_number),
77 'thumbnails': [{'url': thumbnail_url}
78 for thumbnail_url in traverse_obj(json_data, ('thumbnailUrls', ...))]
79 }