[extractor/kompas] Add extractor (#4562)
[yt-dlp3.git] / yt_dlp / extractor / kompas.py
blobd400c42f371d5f1c6962d7d105cfab7e84d2fde4
1 from .common import InfoExtractor
2 from ..utils import (
3 clean_html,
4 float_or_none,
5 traverse_obj,
6 try_call,
9 # Video from www.kompas.tv and video.kompas.com seems use jixie player
10 # see [1] https://jixie.atlassian.net/servicedesk/customer/portal/2/article/1339654214?src=-1456335525,
11 # [2] https://scripts.jixie.media/jxvideo.3.1.min.js for more info
14 class KompasVideoIE(InfoExtractor):
15 _VALID_URL = r'https?://video\.kompas\.com/\w+/(?P<id>\d+)/(?P<slug>[\w-]+)'
16 _TESTS = [{
17 'url': 'https://video.kompas.com/watch/164474/kim-jong-un-siap-kirim-nuklir-lawan-as-dan-korsel',
18 'info_dict': {
19 'id': '164474',
20 'ext': 'mp4',
21 'title': 'Kim Jong Un Siap Kirim Nuklir Lawan AS dan Korsel',
22 'description': 'md5:262530c4fb7462398235f9a5dba92456',
23 'uploader_id': '9262bf2590d558736cac4fff7978fcb1',
24 'display_id': 'kim-jong-un-siap-kirim-nuklir-lawan-as-dan-korsel',
25 'duration': 85.066667,
26 'categories': ['news'],
27 'thumbnail': 'https://video.jixie.media/1001/164474/164474_1280x720.jpg',
28 'tags': 'count:9',
32 def _real_extract(self, url):
33 video_id, display_id = self._match_valid_url(url).group('id', 'slug')
34 webpage = self._download_webpage(url, display_id)
36 json_data = self._download_json(
37 'https://apidam.jixie.io/api/public/stream', display_id,
38 query={'metadata': 'full', 'video_id': video_id})['data']
40 formats, subtitles = [], {}
41 for stream in json_data['streams']:
42 if stream.get('type') == 'HLS':
43 fmt, sub = self._extract_m3u8_formats_and_subtitles(stream.get('url'), display_id, ext='mp4')
44 formats.extend(fmt)
45 self._merge_subtitles(sub, target=subtitles)
46 else:
47 formats.append({
48 'url': stream.get('url'),
49 'width': stream.get('width'),
50 'height': stream.get('height'),
51 'ext': 'mp4',
54 self._sort_formats(formats)
55 return {
56 'id': video_id,
57 'display_id': display_id,
58 'formats': formats,
59 'subtitles': subtitles,
60 'title': json_data.get('title') or self._html_search_meta(['og:title', 'twitter:title'], webpage),
61 'description': (clean_html(traverse_obj(json_data, ('metadata', 'description')))
62 or self._html_search_meta(['description', 'og:description', 'twitter:description'], webpage)),
63 'thumbnails': traverse_obj(json_data, ('metadata', 'thumbnails')),
64 'duration': float_or_none(traverse_obj(json_data, ('metadata', 'duration'))),
65 'tags': try_call(lambda: json_data['metadata']['keywords'].split(',')),
66 'categories': try_call(lambda: json_data['metadata']['categories'].split(',')),
67 'uploader_id': json_data.get('owner_id'),