]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/cnn.py
[cleanup] Add more ruff rules (#10149)
[yt-dlp.git] / yt_dlp / extractor / cnn.py
1 from .common import InfoExtractor
2 from .turner import TurnerBaseIE
3 from ..utils import merge_dicts, try_call, url_basename
4
5
6 class CNNIE(TurnerBaseIE):
7 _VALID_URL = r'''(?x)https?://(?:(?P<sub_domain>edition|www|money)\.)?cnn\.com/(?:video/(?:data/.+?|\?)/)?videos?/
8 (?P<path>.+?/(?P<title>[^/]+?)(?:\.(?:[a-z\-]+)|(?=&)))'''
9
10 _TESTS = [{
11 'url': 'http://edition.cnn.com/video/?/video/sports/2013/06/09/nadal-1-on-1.cnn',
12 'md5': '3e6121ea48df7e2259fe73a0628605c4',
13 'info_dict': {
14 'id': 'sports/2013/06/09/nadal-1-on-1.cnn',
15 'ext': 'mp4',
16 'title': 'Nadal wins 8th French Open title',
17 'description': 'World Sport\'s Amanda Davies chats with 2013 French Open champion Rafael Nadal.',
18 'duration': 135,
19 'upload_date': '20130609',
20 },
21 'expected_warnings': ['Failed to download m3u8 information'],
22 }, {
23 'url': 'http://edition.cnn.com/video/?/video/us/2013/08/21/sot-student-gives-epic-speech.georgia-institute-of-technology&utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+rss%2Fcnn_topstories+%28RSS%3A+Top+Stories%29',
24 'md5': 'b5cc60c60a3477d185af8f19a2a26f4e',
25 'info_dict': {
26 'id': 'us/2013/08/21/sot-student-gives-epic-speech.georgia-institute-of-technology',
27 'ext': 'mp4',
28 'title': "Student's epic speech stuns new freshmen",
29 'description': 'A Georgia Tech student welcomes the incoming freshmen with an epic speech backed by music from "2001: A Space Odyssey."',
30 'upload_date': '20130821',
31 },
32 'expected_warnings': ['Failed to download m3u8 information'],
33 }, {
34 'url': 'http://www.cnn.com/video/data/2.0/video/living/2014/12/22/growing-america-nashville-salemtown-board-episode-1.hln.html',
35 'md5': 'f14d02ebd264df951feb2400e2c25a1b',
36 'info_dict': {
37 'id': 'living/2014/12/22/growing-america-nashville-salemtown-board-episode-1.hln',
38 'ext': 'mp4',
39 'title': 'Nashville Ep. 1: Hand crafted skateboards',
40 'description': 'md5:e7223a503315c9f150acac52e76de086',
41 'upload_date': '20141222',
42 },
43 'expected_warnings': ['Failed to download m3u8 information'],
44 }, {
45 'url': 'http://money.cnn.com/video/news/2016/08/19/netflix-stunning-stats.cnnmoney/index.html',
46 'md5': '52a515dc1b0f001cd82e4ceda32be9d1',
47 'info_dict': {
48 'id': '/video/news/2016/08/19/netflix-stunning-stats.cnnmoney',
49 'ext': 'mp4',
50 'title': '5 stunning stats about Netflix',
51 'description': 'Did you know that Netflix has more than 80 million members? Here are five facts about the online video distributor that you probably didn\'t know.',
52 'upload_date': '20160819',
53 },
54 'params': {
55 # m3u8 download
56 'skip_download': True,
57 },
58 }, {
59 'url': 'http://cnn.com/video/?/video/politics/2015/03/27/pkg-arizona-senator-church-attendance-mandatory.ktvk',
60 'only_matching': True,
61 }, {
62 'url': 'http://cnn.com/video/?/video/us/2015/04/06/dnt-baker-refuses-anti-gay-order.wkmg',
63 'only_matching': True,
64 }, {
65 'url': 'http://edition.cnn.com/videos/arts/2016/04/21/olympic-games-cultural-a-z-brazil.cnn',
66 'only_matching': True,
67 }]
68
69 _CONFIG = {
70 # http://edition.cnn.com/.element/apps/cvp/3.0/cfg/spider/cnn/expansion/config.xml
71 'edition': {
72 'data_src': 'http://edition.cnn.com/video/data/3.0/video/%s/index.xml',
73 'media_src': 'http://pmd.cdn.turner.com/cnn/big',
74 },
75 # http://money.cnn.com/.element/apps/cvp2/cfg/config.xml
76 'money': {
77 'data_src': 'http://money.cnn.com/video/data/4.0/video/%s.xml',
78 'media_src': 'http://ht3.cdn.turner.com/money/big',
79 },
80 }
81
82 def _extract_timestamp(self, video_data):
83 # TODO: fix timestamp extraction
84 return None
85
86 def _real_extract(self, url):
87 sub_domain, path, page_title = self._match_valid_url(url).groups()
88 if sub_domain not in ('money', 'edition'):
89 sub_domain = 'edition'
90 config = self._CONFIG[sub_domain]
91 return self._extract_cvp_info(
92 config['data_src'] % path, page_title, {
93 'default': {
94 'media_src': config['media_src'],
95 },
96 'f4m': {
97 'host': 'cnn-vh.akamaihd.net',
98 },
99 })
100
101
102 class CNNBlogsIE(InfoExtractor):
103 _VALID_URL = r'https?://[^\.]+\.blogs\.cnn\.com/.+'
104 _TEST = {
105 'url': 'http://reliablesources.blogs.cnn.com/2014/02/09/criminalizing-journalism/',
106 'md5': '3e56f97b0b6ffb4b79f4ea0749551084',
107 'info_dict': {
108 'id': 'bestoftv/2014/02/09/criminalizing-journalism.cnn',
109 'ext': 'mp4',
110 'title': 'Criminalizing journalism?',
111 'description': 'Glenn Greenwald responds to comments made this week on Capitol Hill that journalists could be criminal accessories.',
112 'upload_date': '20140209',
113 },
114 'expected_warnings': ['Failed to download m3u8 information'],
115 'add_ie': ['CNN'],
116 }
117
118 def _real_extract(self, url):
119 webpage = self._download_webpage(url, url_basename(url))
120 cnn_url = self._html_search_regex(r'data-url="(.+?)"', webpage, 'cnn url')
121 return self.url_result(cnn_url, CNNIE.ie_key())
122
123
124 class CNNArticleIE(InfoExtractor):
125 _VALID_URL = r'https?://(?:(?:edition|www)\.)?cnn\.com/(?!videos?/)'
126 _TEST = {
127 'url': 'http://www.cnn.com/2014/12/21/politics/obama-north-koreas-hack-not-war-but-cyber-vandalism/',
128 'md5': '689034c2a3d9c6dc4aa72d65a81efd01',
129 'info_dict': {
130 'id': 'bestoftv/2014/12/21/ip-north-korea-obama.cnn',
131 'ext': 'mp4',
132 'title': 'Obama: Cyberattack not an act of war',
133 'description': 'md5:0a802a40d2376f60e6b04c8d5bcebc4b',
134 'upload_date': '20141221',
135 },
136 'expected_warnings': ['Failed to download m3u8 information'],
137 'add_ie': ['CNN'],
138 }
139
140 def _real_extract(self, url):
141 webpage = self._download_webpage(url, url_basename(url))
142 cnn_url = self._html_search_regex(r"video:\s*'([^']+)'", webpage, 'cnn url')
143 return self.url_result('http://cnn.com/video/?/video/' + cnn_url, CNNIE.ie_key())
144
145
146 class CNNIndonesiaIE(InfoExtractor):
147 _VALID_URL = r'https?://www\.cnnindonesia\.com/[\w-]+/(?P<upload_date>\d{8})\d+-\d+-(?P<id>\d+)/(?P<display_id>[\w-]+)'
148 _TESTS = [{
149 'url': 'https://www.cnnindonesia.com/ekonomi/20220909212635-89-845885/alasan-harga-bbm-di-indonesia-masih-disubsidi',
150 'info_dict': {
151 'id': '845885',
152 'ext': 'mp4',
153 'description': 'md5:e7954bfa6f1749bc9ef0c079a719c347',
154 'upload_date': '20220909',
155 'title': 'Alasan Harga BBM di Indonesia Masih Disubsidi',
156 'timestamp': 1662859088,
157 'duration': 120.0,
158 'thumbnail': r're:https://akcdn\.detik\.net\.id/visual/2022/09/09/thumbnail-ekopedia-alasan-harga-bbm-disubsidi_169\.jpeg',
159 'tags': ['ekopedia', 'subsidi bbm', 'subsidi', 'bbm', 'bbm subsidi', 'harga pertalite naik'],
160 'age_limit': 0,
161 'release_timestamp': 1662859088,
162 'release_date': '20220911',
163 'uploader': 'Asfahan Yahsyi',
164 },
165 }, {
166 'url': 'https://www.cnnindonesia.com/internasional/20220911104341-139-846189/video-momen-charles-disambut-meriah-usai-dilantik-jadi-raja-inggris',
167 'info_dict': {
168 'id': '846189',
169 'ext': 'mp4',
170 'upload_date': '20220911',
171 'duration': 76.0,
172 'timestamp': 1662869995,
173 'description': 'md5:ece7b003b3ee7d81c6a5cfede7d5397d',
174 'thumbnail': r're:https://akcdn\.detik\.net\.id/visual/2022/09/11/thumbnail-video-1_169\.jpeg',
175 'title': 'VIDEO: Momen Charles Disambut Meriah usai Dilantik jadi Raja Inggris',
176 'tags': ['raja charles', 'raja charles iii', 'ratu elizabeth', 'ratu elizabeth meninggal dunia', 'raja inggris', 'inggris'],
177 'age_limit': 0,
178 'release_date': '20220911',
179 'uploader': 'REUTERS',
180 'release_timestamp': 1662869995,
181 },
182 }]
183
184 def _real_extract(self, url):
185 upload_date, video_id, display_id = self._match_valid_url(url).group('upload_date', 'id', 'display_id')
186 webpage = self._download_webpage(url, display_id)
187
188 json_ld_list = list(self._yield_json_ld(webpage, display_id))
189 json_ld_data = self._json_ld(json_ld_list, display_id)
190 embed_url = next(
191 json_ld.get('embedUrl') for json_ld in json_ld_list if json_ld.get('@type') == 'VideoObject')
192
193 return merge_dicts(json_ld_data, {
194 '_type': 'url_transparent',
195 'url': embed_url,
196 'upload_date': upload_date,
197 'tags': try_call(lambda: self._html_search_meta('keywords', webpage).split(', ')),
198 })