[cleanup] Misc (#8968)
[yt-dlp.git] / yt_dlp / extractor / swearnet.py
blobaeaff28f22764727ec33f4af8d8edaa05cdb5065
1 from .common import InfoExtractor
2 from ..utils import ExtractorError, int_or_none, traverse_obj
5 class 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',
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)
38 return formats, subtitles
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')
48 return subs
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)
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
61 json_data = self._download_json(
62 f'https://play.vidyard.com/player/{external_id}.json', display_id)['payload']['chapters'][0]
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)
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')
71 or self._html_search_meta(['og:description', 'twitter:description'], webpage)),
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', ...))]