Release 2024.12.03
[yt-dlp3.git] / yt_dlp / extractor / trtworld.py
blobd33b9aae0c46d21bc4b41994ed6a8f1de79c096e
1 from .common import InfoExtractor
2 from ..utils import ExtractorError, determine_ext, parse_iso8601, url_or_none
3 from ..utils.traversal import traverse_obj
6 class TrtWorldIE(InfoExtractor):
7 _VALID_URL = r'https?://www\.trtworld\.com/video/[\w-]+/[\w-]+-(?P<id>\d+)'
9 _TESTS = [{
10 'url': 'https://www.trtworld.com/video/news/turkiye-switches-to-sustainable-tourism-16067690',
11 'info_dict': {
12 'id': '16067690',
13 'ext': 'mp4',
14 'title': 'Türkiye switches to sustainable tourism',
15 'release_timestamp': 1701529569,
16 'release_date': '20231202',
17 'thumbnail': 'https://cdn-i.pr.trt.com.tr/trtworld/17647563_0-0-1920-1080.jpeg',
18 'description': 'md5:0a975c04257fb529c8f99c7b76a2cf12',
20 }, {
21 'url': 'https://www.trtworld.com/video/one-offs/frames-from-anatolia-recreating-a-james-bond-scene-in-istanbuls-grand-bazaar-14541780',
22 'info_dict': {
23 'id': '14541780',
24 'ext': 'mp4',
25 'title': 'Frames From Anatolia: Recreating a ‘James Bond’ Scene in Istanbul’s Grand Bazaar',
26 'release_timestamp': 1692440844,
27 'release_date': '20230819',
28 'thumbnail': 'https://cdn-i.pr.trt.com.tr/trtworld/16939810_0-0-1920-1080.jpeg',
29 'description': 'md5:4050e21570cc3c40b6c9badae800a94f',
31 }, {
32 'url': 'https://www.trtworld.com/video/the-newsmakers/can-sudan-find-peace-amidst-failed-transition-to-democracy-12904760',
33 'info_dict': {
34 'id': '12904760',
35 'ext': 'mp4',
36 'title': 'Can Sudan find peace amidst failed transition to democracy?',
37 'release_timestamp': 1681972747,
38 'release_date': '20230420',
39 'thumbnail': 'http://cdni0.trtworld.com/w768/q70/154214_NMYOUTUBETEMPLATE1_1681833018736.jpg',
41 }, {
42 'url': 'https://www.trtworld.com/video/africa-matters/locals-learning-to-cope-with-rising-tides-of-kenyas-great-lakes-16059545',
43 'info_dict': {
44 'id': 'zEns2dWl00w',
45 'ext': 'mp4',
46 'title': "Locals learning to cope with rising tides of Kenya's Great Lakes",
47 'thumbnail': 'https://i.ytimg.com/vi/zEns2dWl00w/maxresdefault.jpg',
48 'description': 'md5:3ad9d7c5234d752a4ead4340c79c6b8d',
49 'channel_id': 'UC7fWeaHhqgM4Ry-RMpM2YYw',
50 'channel_url': 'https://www.youtube.com/channel/UC7fWeaHhqgM4Ry-RMpM2YYw',
51 'duration': 210,
52 'view_count': int,
53 'age_limit': 0,
54 'webpage_url': 'https://www.youtube.com/watch?v=zEns2dWl00w',
55 'categories': ['News & Politics'],
56 'channel': 'TRT World',
57 'channel_follower_count': int,
58 'channel_is_verified': True,
59 'uploader': 'TRT World',
60 'uploader_id': '@trtworld',
61 'uploader_url': 'https://www.youtube.com/@trtworld',
62 'upload_date': '20231202',
63 'availability': 'public',
64 'comment_count': int,
65 'playable_in_embed': True,
66 'tags': [],
67 'live_status': 'not_live',
68 'like_count': int,
72 def _real_extract(self, url):
73 display_id = self._match_id(url)
74 webpage = self._download_webpage(url, display_id)
75 nuxtjs_data = self._search_nuxt_data(webpage, display_id)['videoData']['content']['platforms']
76 formats = []
77 for media_url in traverse_obj(nuxtjs_data, (
78 ('website', 'ott'), 'metadata', ('hls_url', 'url'), {url_or_none})):
79 # NB: Website sometimes serves mp4 files under `hls_url` key
80 if determine_ext(media_url) == 'm3u8':
81 formats.extend(self._extract_m3u8_formats(media_url, display_id, fatal=False))
82 else:
83 formats.append({
84 'format_id': 'http',
85 'url': media_url,
87 if not formats:
88 if youtube_id := traverse_obj(nuxtjs_data, ('youtube', 'metadata', 'youtubeId')):
89 return self.url_result(youtube_id, 'Youtube')
90 raise ExtractorError('No video found', expected=True)
92 return {
93 'id': display_id,
94 'formats': formats,
95 **traverse_obj(nuxtjs_data, (('website', 'ott'), {
96 'title': ('fields', 'title', 'text', {str}),
97 'description': ('fields', 'description', 'text', {str}),
98 'thumbnail': ('fields', 'thumbnail', 'url', {url_or_none}),
99 'release_timestamp': ('published', 'date', {parse_iso8601}),
100 }), get_all=False),