]>
Commit | Line | Data |
---|---|---|
77cc7c6e | 1 | import re |
061d1cd9 | 2 | |
298a120a | 3 | from .common import InfoExtractor |
77cc7c6e | 4 | from ..utils import ( |
81c8b9bd | 5 | ExtractorError, |
54579be4 | 6 | clean_html, |
7 | get_element_by_class, | |
81c8b9bd | 8 | int_or_none, |
9 | join_nonempty, | |
77cc7c6e LNO |
10 | parse_duration, |
11 | traverse_obj, | |
12 | unescapeHTML, | |
13 | unified_timestamp, | |
81c8b9bd | 14 | url_or_none, |
8f0be90e | 15 | urljoin, |
77cc7c6e | 16 | ) |
298a120a AN |
17 | |
18 | ||
29f7c58a | 19 | class NhkBaseIE(InfoExtractor): |
6d1b3489 | 20 | _API_URL_TEMPLATE = 'https://nwapi.nhk.jp/nhkworld/%sod%slist/v7b/%s/%s/%s/all%s.json' |
29f7c58a | 21 | _BASE_URL_REGEX = r'https?://www3\.nhk\.or\.jp/nhkworld/(?P<lang>[a-z]{2})/ondemand' |
22 | _TYPE_REGEX = r'/(?P<type>video|audio)/' | |
298a120a | 23 | |
29f7c58a | 24 | def _call_api(self, m_id, lang, is_video, is_episode, is_clip): |
25 | return self._download_json( | |
26 | self._API_URL_TEMPLATE % ( | |
27 | 'v' if is_video else 'r', | |
28 | 'clip' if is_clip else 'esd', | |
29 | 'episode' if is_episode else 'program', | |
30 | m_id, lang, '/all' if is_video else ''), | |
31 | m_id, query={'apikey': 'EJfK8jdS57GqlupFgAfAAwr573q01y6k'})['data']['episodes'] or [] | |
32 | ||
e831c80e | 33 | def _get_api_info(self, refresh=True): |
34 | if not refresh: | |
35 | return self.cache.load('nhk', 'api_info') | |
36 | ||
37 | self.cache.store('nhk', 'api_info', {}) | |
38 | movie_player_js = self._download_webpage( | |
39 | 'https://movie-a.nhk.or.jp/world/player/js/movie-player.js', None, | |
40 | note='Downloading stream API information') | |
41 | api_info = { | |
42 | 'url': self._search_regex( | |
43 | r'prod:[^;]+\bapiUrl:\s*[\'"]([^\'"]+)[\'"]', movie_player_js, None, 'stream API url'), | |
44 | 'token': self._search_regex( | |
45 | r'prod:[^;]+\btoken:\s*[\'"]([^\'"]+)[\'"]', movie_player_js, None, 'stream API token'), | |
46 | } | |
47 | self.cache.store('nhk', 'api_info', api_info) | |
48 | return api_info | |
49 | ||
54579be4 | 50 | def _extract_stream_info(self, vod_id): |
e831c80e | 51 | for refresh in (False, True): |
52 | api_info = self._get_api_info(refresh) | |
53 | if not api_info: | |
54 | continue | |
55 | ||
56 | api_url = api_info.pop('url') | |
54579be4 | 57 | meta = traverse_obj( |
e831c80e | 58 | self._download_json( |
59 | api_url, vod_id, 'Downloading stream url info', fatal=False, query={ | |
60 | **api_info, | |
61 | 'type': 'json', | |
62 | 'optional_id': vod_id, | |
63 | 'active_flg': 1, | |
54579be4 | 64 | }), ('meta', 0)) |
65 | stream_url = traverse_obj( | |
66 | meta, ('movie_url', ('mb_auto', 'auto_sp', 'auto_pc'), {url_or_none}), get_all=False) | |
e831c80e | 67 | |
54579be4 | 68 | if stream_url: |
69 | formats, subtitles = self._extract_m3u8_formats_and_subtitles(stream_url, vod_id) | |
70 | return { | |
71 | **traverse_obj(meta, { | |
72 | 'duration': ('duration', {int_or_none}), | |
73 | 'timestamp': ('publication_date', {unified_timestamp}), | |
74 | 'release_timestamp': ('insert_date', {unified_timestamp}), | |
75 | 'modified_timestamp': ('update_date', {unified_timestamp}), | |
76 | }), | |
77 | 'formats': formats, | |
78 | 'subtitles': subtitles, | |
79 | } | |
e831c80e | 80 | raise ExtractorError('Unable to extract stream url') |
81 | ||
29f7c58a | 82 | def _extract_episode_info(self, url, episode=None): |
83 | fetch_episode = episode is None | |
4de94b9e | 84 | lang, m_type, episode_id = NhkVodIE._match_valid_url(url).group('lang', 'type', 'id') |
85 | is_video = m_type == 'video' | |
86 | ||
87 | if is_video: | |
061d1cd9 | 88 | episode_id = episode_id[:4] + '-' + episode_id[4:] |
f9b373af | 89 | |
29f7c58a | 90 | if fetch_episode: |
91 | episode = self._call_api( | |
92 | episode_id, lang, is_video, True, episode_id[:4] == '9999')[0] | |
45396dd2 | 93 | |
061d1cd9 | 94 | def get_clean_field(key): |
54579be4 | 95 | return clean_html(episode.get(key + '_clean') or episode.get(key)) |
45396dd2 | 96 | |
54579be4 | 97 | title = get_clean_field('sub_title') |
061d1cd9 | 98 | series = get_clean_field('title') |
45396dd2 | 99 | |
061d1cd9 RA |
100 | thumbnails = [] |
101 | for s, w, h in [('', 640, 360), ('_l', 1280, 720)]: | |
102 | img_path = episode.get('image' + s) | |
103 | if not img_path: | |
104 | continue | |
105 | thumbnails.append({ | |
106 | 'id': '%dp' % h, | |
107 | 'height': h, | |
108 | 'width': w, | |
109 | 'url': 'https://www3.nhk.or.jp' + img_path, | |
110 | }) | |
298a120a | 111 | |
54579be4 | 112 | episode_name = title |
113 | if series and title: | |
114 | title = f'{series} - {title}' | |
115 | elif series and not title: | |
116 | title = series | |
117 | series = None | |
118 | episode_name = None | |
119 | else: # title, no series | |
120 | episode_name = None | |
121 | ||
061d1cd9 RA |
122 | info = { |
123 | 'id': episode_id + '-' + lang, | |
54579be4 | 124 | 'title': title, |
061d1cd9 RA |
125 | 'description': get_clean_field('description'), |
126 | 'thumbnails': thumbnails, | |
f9b373af | 127 | 'series': series, |
54579be4 | 128 | 'episode': episode_name, |
f9b373af | 129 | } |
54579be4 | 130 | |
061d1cd9 | 131 | if is_video: |
29f7c58a | 132 | vod_id = episode['vod_id'] |
061d1cd9 | 133 | info.update({ |
54579be4 | 134 | **self._extract_stream_info(vod_id), |
29f7c58a | 135 | 'id': vod_id, |
061d1cd9 | 136 | }) |
e831c80e | 137 | |
061d1cd9 | 138 | else: |
29f7c58a | 139 | if fetch_episode: |
140 | audio_path = episode['audio']['audio'] | |
141 | info['formats'] = self._extract_m3u8_formats( | |
142 | 'https://nhkworld-vh.akamaihd.net/i%s/master.m3u8' % audio_path, | |
143 | episode_id, 'm4a', entry_protocol='m3u8_native', | |
144 | m3u8_id='hls', fatal=False) | |
145 | for f in info['formats']: | |
146 | f['language'] = lang | |
147 | else: | |
148 | info.update({ | |
149 | '_type': 'url_transparent', | |
150 | 'ie_key': NhkVodIE.ie_key(), | |
151 | 'url': url, | |
152 | }) | |
061d1cd9 | 153 | return info |
29f7c58a | 154 | |
155 | ||
156 | class NhkVodIE(NhkBaseIE): | |
6d1b3489 | 157 | # the 7-character IDs can have alphabetic chars too: assume [a-z] rather than just [a-f], eg |
4de94b9e | 158 | _VALID_URL = [rf'{NhkBaseIE._BASE_URL_REGEX}/(?P<type>video)/(?P<id>[0-9a-z]+)', |
159 | rf'{NhkBaseIE._BASE_URL_REGEX}/(?P<type>audio)/(?P<id>[^/?#]+?-\d{{8}}-[0-9a-z]+)'] | |
29f7c58a | 160 | # Content available only for a limited period of time. Visit |
161 | # https://www3.nhk.or.jp/nhkworld/en/ondemand/ for working samples. | |
162 | _TESTS = [{ | |
4de94b9e | 163 | 'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/video/2049126/', |
f41b949a | 164 | 'info_dict': { |
4de94b9e | 165 | 'id': 'nw_vod_v_en_2049_126_20230413233000_01_1681398302', |
f41b949a | 166 | 'ext': 'mp4', |
4de94b9e | 167 | 'title': 'Japan Railway Journal - The Tohoku Shinkansen: Full Speed Ahead', |
168 | 'description': 'md5:49f7c5b206e03868a2fdf0d0814b92f6', | |
169 | 'thumbnail': 'md5:51bcef4a21936e7fea1ff4e06353f463', | |
170 | 'episode': 'The Tohoku Shinkansen: Full Speed Ahead', | |
171 | 'series': 'Japan Railway Journal', | |
54579be4 | 172 | 'modified_timestamp': 1694243656, |
173 | 'timestamp': 1681428600, | |
174 | 'release_timestamp': 1693883728, | |
175 | 'duration': 1679, | |
176 | 'upload_date': '20230413', | |
177 | 'modified_date': '20230909', | |
178 | 'release_date': '20230905', | |
179 | ||
f41b949a DR |
180 | }, |
181 | }, { | |
29f7c58a | 182 | # video clip |
183 | 'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/video/9999011/', | |
4de94b9e | 184 | 'md5': '153c3016dfd252ba09726588149cf0e7', |
29f7c58a | 185 | 'info_dict': { |
4de94b9e | 186 | 'id': 'lpZXIwaDE6_Z-976CPsFdxyICyWUzlT5', |
29f7c58a | 187 | 'ext': 'mp4', |
4de94b9e | 188 | 'title': 'Dining with the Chef - Chef Saito\'s Family recipe: MENCHI-KATSU', |
29f7c58a | 189 | 'description': 'md5:5aee4a9f9d81c26281862382103b0ea5', |
4de94b9e | 190 | 'thumbnail': 'md5:d6a4d9b6e9be90aaadda0bcce89631ed', |
f41b949a DR |
191 | 'series': 'Dining with the Chef', |
192 | 'episode': 'Chef Saito\'s Family recipe: MENCHI-KATSU', | |
54579be4 | 193 | 'duration': 148, |
194 | 'upload_date': '20190816', | |
195 | 'release_date': '20230902', | |
196 | 'release_timestamp': 1693619292, | |
197 | 'modified_timestamp': 1694168033, | |
198 | 'modified_date': '20230908', | |
199 | 'timestamp': 1565997540, | |
29f7c58a | 200 | }, |
201 | }, { | |
4de94b9e | 202 | # radio |
203 | 'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/audio/livinginjapan-20231001-1/', | |
29f7c58a | 204 | 'info_dict': { |
4de94b9e | 205 | 'id': 'livinginjapan-20231001-1-en', |
29f7c58a | 206 | 'ext': 'm4a', |
4de94b9e | 207 | 'title': 'Living in Japan - Tips for Travelers to Japan / Ramen Vending Machines', |
208 | 'series': 'Living in Japan', | |
54579be4 | 209 | 'description': 'md5:0a0e2077d8f07a03071e990a6f51bfab', |
4de94b9e | 210 | 'thumbnail': 'md5:960622fb6e06054a4a1a0c97ea752545', |
211 | 'episode': 'Tips for Travelers to Japan / Ramen Vending Machines' | |
29f7c58a | 212 | }, |
29f7c58a | 213 | }, { |
214 | 'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/video/2015173/', | |
215 | 'only_matching': True, | |
216 | }, { | |
217 | 'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/audio/plugin-20190404-1/', | |
218 | 'only_matching': True, | |
219 | }, { | |
220 | 'url': 'https://www3.nhk.or.jp/nhkworld/fr/ondemand/audio/plugin-20190404-1/', | |
221 | 'only_matching': True, | |
222 | }, { | |
223 | 'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/audio/j_art-20150903-1/', | |
224 | 'only_matching': True, | |
6d1b3489 | 225 | }, { |
226 | # video, alphabetic character in ID #29670 | |
227 | 'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/video/9999a34/', | |
6d1b3489 | 228 | 'info_dict': { |
229 | 'id': 'qfjay6cg', | |
230 | 'ext': 'mp4', | |
231 | 'title': 'DESIGN TALKS plus - Fishermen’s Finery', | |
232 | 'description': 'md5:8a8f958aaafb0d7cb59d38de53f1e448', | |
233 | 'thumbnail': r're:^https?:/(/[a-z0-9.-]+)+\.jpg\?w=1920&h=1080$', | |
234 | 'upload_date': '20210615', | |
235 | 'timestamp': 1623722008, | |
f41b949a DR |
236 | }, |
237 | 'skip': '404 Not Found', | |
4de94b9e | 238 | }, { |
239 | # japanese-language, longer id than english | |
240 | 'url': 'https://www3.nhk.or.jp/nhkworld/ja/ondemand/video/0020271111/', | |
241 | 'info_dict': { | |
242 | 'id': 'nw_ja_v_jvod_ohayou_20231008', | |
243 | 'ext': 'mp4', | |
244 | 'title': 'おはよう日本(7時台) - 10月8日放送', | |
245 | 'series': 'おはよう日本(7時台)', | |
246 | 'episode': '10月8日放送', | |
247 | 'thumbnail': 'md5:d733b1c8e965ab68fb02b2d347d0e9b4', | |
248 | 'description': 'md5:9c1d6cbeadb827b955b20e99ab920ff0', | |
249 | }, | |
250 | 'skip': 'expires 2023-10-15', | |
54579be4 | 251 | }, { |
252 | # a one-off (single-episode series). title from the api is just '<p></p>' | |
253 | 'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/video/3004952/', | |
254 | 'info_dict': { | |
255 | 'id': 'nw_vod_v_en_3004_952_20230723091000_01_1690074552', | |
256 | 'ext': 'mp4', | |
257 | 'title': 'Barakan Discovers AMAMI OSHIMA: Isson\'s Treasure Island', | |
258 | 'description': 'md5:5db620c46a0698451cc59add8816b797', | |
259 | 'thumbnail': 'md5:67d9ff28009ba379bfa85ad1aaa0e2bd', | |
260 | 'release_date': '20230905', | |
261 | 'timestamp': 1690103400, | |
262 | 'duration': 2939, | |
263 | 'release_timestamp': 1693898699, | |
264 | 'modified_timestamp': 1698057495, | |
265 | 'modified_date': '20231023', | |
266 | 'upload_date': '20230723', | |
267 | }, | |
29f7c58a | 268 | }] |
269 | ||
270 | def _real_extract(self, url): | |
271 | return self._extract_episode_info(url) | |
272 | ||
273 | ||
274 | class NhkVodProgramIE(NhkBaseIE): | |
4de94b9e | 275 | _VALID_URL = rf'{NhkBaseIE._BASE_URL_REGEX}/program{NhkBaseIE._TYPE_REGEX}(?P<id>\w+)(?:.+?\btype=(?P<episode_type>clip|(?:radio|tv)Episode))?' |
29f7c58a | 276 | _TESTS = [{ |
277 | # video program episodes | |
f41b949a DR |
278 | 'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/program/video/sumo', |
279 | 'info_dict': { | |
280 | 'id': 'sumo', | |
281 | 'title': 'GRAND SUMO Highlights', | |
54579be4 | 282 | 'description': 'md5:fc20d02dc6ce85e4b72e0273aa52fdbf', |
f41b949a | 283 | }, |
54579be4 | 284 | 'playlist_mincount': 0, |
f41b949a | 285 | }, { |
29f7c58a | 286 | 'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/program/video/japanrailway', |
287 | 'info_dict': { | |
288 | 'id': 'japanrailway', | |
289 | 'title': 'Japan Railway Journal', | |
54579be4 | 290 | 'description': 'md5:ea39d93af7d05835baadf10d1aae0e3f', |
29f7c58a | 291 | }, |
f41b949a | 292 | 'playlist_mincount': 12, |
29f7c58a | 293 | }, { |
294 | # video program clips | |
295 | 'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/program/video/japanrailway/?type=clip', | |
296 | 'info_dict': { | |
297 | 'id': 'japanrailway', | |
298 | 'title': 'Japan Railway Journal', | |
54579be4 | 299 | 'description': 'md5:ea39d93af7d05835baadf10d1aae0e3f', |
29f7c58a | 300 | }, |
301 | 'playlist_mincount': 5, | |
302 | }, { | |
303 | 'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/program/video/10yearshayaomiyazaki/', | |
304 | 'only_matching': True, | |
305 | }, { | |
306 | # audio program | |
307 | 'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/program/audio/listener/', | |
308 | 'only_matching': True, | |
309 | }] | |
310 | ||
311 | def _real_extract(self, url): | |
4de94b9e | 312 | lang, m_type, program_id, episode_type = self._match_valid_url(url).group('lang', 'type', 'id', 'episode_type') |
29f7c58a | 313 | episodes = self._call_api( |
314 | program_id, lang, m_type == 'video', False, episode_type == 'clip') | |
315 | ||
316 | entries = [] | |
317 | for episode in episodes: | |
318 | episode_path = episode.get('url') | |
319 | if not episode_path: | |
320 | continue | |
321 | entries.append(self._extract_episode_info( | |
322 | urljoin(url, episode_path), episode)) | |
323 | ||
54579be4 | 324 | html = self._download_webpage(url, program_id) |
325 | program_title = clean_html(get_element_by_class('p-programDetail__title', html)) | |
326 | program_description = clean_html(get_element_by_class('p-programDetail__text', html)) | |
29f7c58a | 327 | |
54579be4 | 328 | return self.playlist_result(entries, program_id, program_title, program_description) |
77cc7c6e LNO |
329 | |
330 | ||
331 | class NhkForSchoolBangumiIE(InfoExtractor): | |
332 | _VALID_URL = r'https?://www2\.nhk\.or\.jp/school/movie/(?P<type>bangumi|clip)\.cgi\?das_id=(?P<id>[a-zA-Z0-9_-]+)' | |
333 | _TESTS = [{ | |
334 | 'url': 'https://www2.nhk.or.jp/school/movie/bangumi.cgi?das_id=D0005150191_00000', | |
335 | 'info_dict': { | |
336 | 'id': 'D0005150191_00003', | |
337 | 'title': 'にている かな', | |
338 | 'duration': 599.999, | |
339 | 'timestamp': 1396414800, | |
340 | ||
341 | 'upload_date': '20140402', | |
342 | 'ext': 'mp4', | |
343 | ||
344 | 'chapters': 'count:12' | |
345 | }, | |
346 | 'params': { | |
347 | # m3u8 download | |
348 | 'skip_download': True, | |
349 | }, | |
350 | }] | |
351 | ||
352 | def _real_extract(self, url): | |
353 | program_type, video_id = self._match_valid_url(url).groups() | |
354 | ||
355 | webpage = self._download_webpage( | |
356 | f'https://www2.nhk.or.jp/school/movie/{program_type}.cgi?das_id={video_id}', video_id) | |
357 | ||
358 | # searches all variables | |
359 | base_values = {g.group(1): g.group(2) for g in re.finditer(r'var\s+([a-zA-Z_]+)\s*=\s*"([^"]+?)";', webpage)} | |
360 | # and programObj values too | |
361 | program_values = {g.group(1): g.group(3) for g in re.finditer(r'(?:program|clip)Obj\.([a-zA-Z_]+)\s*=\s*(["\'])([^"]+?)\2;', webpage)} | |
362 | # extract all chapters | |
363 | chapter_durations = [parse_duration(g.group(1)) for g in re.finditer(r'chapterTime\.push\(\'([0-9:]+?)\'\);', webpage)] | |
364 | chapter_titles = [' '.join([g.group(1) or '', unescapeHTML(g.group(2))]).strip() for g in re.finditer(r'<div class="cpTitle"><span>(scene\s*\d+)?</span>([^<]+?)</div>', webpage)] | |
365 | ||
366 | # this is how player_core.js is actually doing (!) | |
367 | version = base_values.get('r_version') or program_values.get('version') | |
368 | if version: | |
369 | video_id = f'{video_id.split("_")[0]}_{version}' | |
370 | ||
371 | formats = self._extract_m3u8_formats( | |
372 | f'https://nhks-vh.akamaihd.net/i/das/{video_id[0:8]}/{video_id}_V_000.f4v/master.m3u8', | |
373 | video_id, ext='mp4', m3u8_id='hls') | |
77cc7c6e LNO |
374 | |
375 | duration = parse_duration(base_values.get('r_duration')) | |
376 | ||
377 | chapters = None | |
378 | if chapter_durations and chapter_titles and len(chapter_durations) == len(chapter_titles): | |
379 | start_time = chapter_durations | |
380 | end_time = chapter_durations[1:] + [duration] | |
381 | chapters = [{ | |
382 | 'start_time': s, | |
383 | 'end_time': e, | |
384 | 'title': t, | |
385 | } for s, e, t in zip(start_time, end_time, chapter_titles)] | |
386 | ||
387 | return { | |
388 | 'id': video_id, | |
389 | 'title': program_values.get('name'), | |
390 | 'duration': parse_duration(base_values.get('r_duration')), | |
391 | 'timestamp': unified_timestamp(base_values['r_upload']), | |
392 | 'formats': formats, | |
393 | 'chapters': chapters, | |
394 | } | |
395 | ||
396 | ||
397 | class NhkForSchoolSubjectIE(InfoExtractor): | |
398 | IE_DESC = 'Portal page for each school subjects, like Japanese (kokugo, 国語) or math (sansuu/suugaku or 算数・数学)' | |
399 | KNOWN_SUBJECTS = ( | |
400 | 'rika', 'syakai', 'kokugo', | |
401 | 'sansuu', 'seikatsu', 'doutoku', | |
402 | 'ongaku', 'taiiku', 'zukou', | |
403 | 'gijutsu', 'katei', 'sougou', | |
404 | 'eigo', 'tokkatsu', | |
405 | 'tokushi', 'sonota', | |
406 | ) | |
407 | _VALID_URL = r'https?://www\.nhk\.or\.jp/school/(?P<id>%s)/?(?:[\?#].*)?$' % '|'.join(re.escape(s) for s in KNOWN_SUBJECTS) | |
408 | ||
409 | _TESTS = [{ | |
410 | 'url': 'https://www.nhk.or.jp/school/sougou/', | |
411 | 'info_dict': { | |
412 | 'id': 'sougou', | |
413 | 'title': '総合的な学習の時間', | |
414 | }, | |
415 | 'playlist_mincount': 16, | |
416 | }, { | |
417 | 'url': 'https://www.nhk.or.jp/school/rika/', | |
418 | 'info_dict': { | |
419 | 'id': 'rika', | |
420 | 'title': '理科', | |
421 | }, | |
422 | 'playlist_mincount': 15, | |
423 | }] | |
424 | ||
425 | def _real_extract(self, url): | |
426 | subject_id = self._match_id(url) | |
427 | webpage = self._download_webpage(url, subject_id) | |
428 | ||
429 | return self.playlist_from_matches( | |
430 | re.finditer(rf'href="((?:https?://www\.nhk\.or\.jp)?/school/{re.escape(subject_id)}/[^/]+/)"', webpage), | |
431 | subject_id, | |
432 | self._html_search_regex(r'(?s)<span\s+class="subjectName">\s*<img\s*[^<]+>\s*([^<]+?)</span>', webpage, 'title', fatal=False), | |
433 | lambda g: urljoin(url, g.group(1))) | |
434 | ||
435 | ||
436 | class NhkForSchoolProgramListIE(InfoExtractor): | |
437 | _VALID_URL = r'https?://www\.nhk\.or\.jp/school/(?P<id>(?:%s)/[a-zA-Z0-9_-]+)' % ( | |
438 | '|'.join(re.escape(s) for s in NhkForSchoolSubjectIE.KNOWN_SUBJECTS) | |
439 | ) | |
440 | _TESTS = [{ | |
441 | 'url': 'https://www.nhk.or.jp/school/sougou/q/', | |
442 | 'info_dict': { | |
443 | 'id': 'sougou/q', | |
444 | 'title': 'Q~こどものための哲学', | |
445 | }, | |
446 | 'playlist_mincount': 20, | |
447 | }] | |
448 | ||
449 | def _real_extract(self, url): | |
450 | program_id = self._match_id(url) | |
451 | ||
452 | webpage = self._download_webpage(f'https://www.nhk.or.jp/school/{program_id}/', program_id) | |
453 | ||
62b8dac4 | 454 | title = (self._generic_title('', webpage) |
04f3fd2c | 455 | or self._html_search_regex(r'<h3>([^<]+?)とは?\s*</h3>', webpage, 'title', fatal=False)) |
77cc7c6e LNO |
456 | title = re.sub(r'\s*\|\s*NHK\s+for\s+School\s*$', '', title) if title else None |
457 | description = self._html_search_regex( | |
458 | r'(?s)<div\s+class="programDetail\s*">\s*<p>[^<]+</p>', | |
459 | webpage, 'description', fatal=False, group=0) | |
460 | ||
461 | bangumi_list = self._download_json( | |
462 | f'https://www.nhk.or.jp/school/{program_id}/meta/program.json', program_id) | |
463 | # they're always bangumi | |
464 | bangumis = [ | |
465 | self.url_result(f'https://www2.nhk.or.jp/school/movie/bangumi.cgi?das_id={x}') | |
466 | for x in traverse_obj(bangumi_list, ('part', ..., 'part-video-dasid')) or []] | |
467 | ||
468 | return self.playlist_result(bangumis, program_id, title, description) | |
8f0be90e | 469 | |
470 | ||
471 | class NhkRadiruIE(InfoExtractor): | |
472 | _GEO_COUNTRIES = ['JP'] | |
473 | IE_DESC = 'NHK らじる (Radiru/Rajiru)' | |
474 | _VALID_URL = r'https?://www\.nhk\.or\.jp/radio/(?:player/ondemand|ondemand/detail)\.html\?p=(?P<site>[\da-zA-Z]+)_(?P<corner>[\da-zA-Z]+)(?:_(?P<headline>[\da-zA-Z]+))?' | |
475 | _TESTS = [{ | |
476 | 'url': 'https://www.nhk.or.jp/radio/player/ondemand.html?p=0449_01_3853544', | |
477 | 'skip': 'Episode expired on 2023-04-16', | |
478 | 'info_dict': { | |
479 | 'channel': 'NHK-FM', | |
54579be4 | 480 | 'uploader': 'NHK-FM', |
8f0be90e | 481 | 'description': 'md5:94b08bdeadde81a97df4ec882acce3e9', |
482 | 'ext': 'm4a', | |
483 | 'id': '0449_01_3853544', | |
484 | 'series': 'ジャズ・トゥナイト', | |
485 | 'thumbnail': 'https://www.nhk.or.jp/prog/img/449/g449.jpg', | |
486 | 'timestamp': 1680969600, | |
487 | 'title': 'ジャズ・トゥナイト NEWジャズ特集', | |
488 | 'upload_date': '20230408', | |
489 | 'release_timestamp': 1680962400, | |
490 | 'release_date': '20230408', | |
491 | 'was_live': True, | |
492 | }, | |
493 | }, { | |
494 | # playlist, airs every weekday so it should _hopefully_ be okay forever | |
495 | 'url': 'https://www.nhk.or.jp/radio/ondemand/detail.html?p=0458_01', | |
496 | 'info_dict': { | |
497 | 'id': '0458_01', | |
498 | 'title': 'ベストオブクラシック', | |
499 | 'description': '世界中の上質な演奏会をじっくり堪能する本格派クラシック番組。', | |
500 | 'channel': 'NHK-FM', | |
54579be4 | 501 | 'uploader': 'NHK-FM', |
8f0be90e | 502 | 'thumbnail': 'https://www.nhk.or.jp/prog/img/458/g458.jpg', |
503 | }, | |
504 | 'playlist_mincount': 3, | |
505 | }, { | |
506 | # one with letters in the id | |
507 | 'url': 'https://www.nhk.or.jp/radio/player/ondemand.html?p=F300_06_3738470', | |
508 | 'note': 'Expires on 2024-03-31', | |
509 | 'info_dict': { | |
510 | 'id': 'F300_06_3738470', | |
511 | 'ext': 'm4a', | |
512 | 'title': '有島武郎「一房のぶどう」', | |
513 | 'description': '朗読:川野一宇(ラジオ深夜便アンカー)\r\n\r\n(2016年12月8日放送「ラジオ深夜便『アンカー朗読シリーズ』」より)', | |
514 | 'channel': 'NHKラジオ第1、NHK-FM', | |
54579be4 | 515 | 'uploader': 'NHKラジオ第1、NHK-FM', |
8f0be90e | 516 | 'timestamp': 1635757200, |
517 | 'thumbnail': 'https://www.nhk.or.jp/radioondemand/json/F300/img/corner/box_109_thumbnail.jpg', | |
518 | 'release_date': '20161207', | |
519 | 'series': 'らじる文庫 by ラジオ深夜便 ', | |
520 | 'release_timestamp': 1481126700, | |
521 | 'upload_date': '20211101', | |
522 | } | |
523 | }, { | |
524 | # news | |
525 | 'url': 'https://www.nhk.or.jp/radio/player/ondemand.html?p=F261_01_3855109', | |
526 | 'skip': 'Expires on 2023-04-17', | |
527 | 'info_dict': { | |
528 | 'id': 'F261_01_3855109', | |
529 | 'ext': 'm4a', | |
530 | 'channel': 'NHKラジオ第1', | |
54579be4 | 531 | 'uploader': 'NHKラジオ第1', |
8f0be90e | 532 | 'timestamp': 1681635900, |
533 | 'release_date': '20230416', | |
534 | 'series': 'NHKラジオニュース', | |
535 | 'title': '午後6時のNHKニュース', | |
536 | 'thumbnail': 'https://www.nhk.or.jp/radioondemand/json/F261/img/RADIONEWS_640.jpg', | |
537 | 'upload_date': '20230416', | |
538 | 'release_timestamp': 1681635600, | |
539 | }, | |
540 | }] | |
541 | ||
542 | def _extract_episode_info(self, headline, programme_id, series_meta): | |
543 | episode_id = f'{programme_id}_{headline["headline_id"]}' | |
544 | episode = traverse_obj(headline, ('file_list', 0, {dict})) | |
545 | ||
546 | return { | |
547 | **series_meta, | |
548 | 'id': episode_id, | |
549 | 'formats': self._extract_m3u8_formats(episode.get('file_name'), episode_id, fatal=False), | |
550 | 'container': 'm4a_dash', # force fixup, AAC-only HLS | |
551 | 'was_live': True, | |
552 | 'series': series_meta.get('title'), | |
553 | 'thumbnail': url_or_none(headline.get('headline_image')) or series_meta.get('thumbnail'), | |
554 | **traverse_obj(episode, { | |
555 | 'title': 'file_title', | |
556 | 'description': 'file_title_sub', | |
557 | 'timestamp': ('open_time', {unified_timestamp}), | |
558 | 'release_timestamp': ('aa_vinfo4', {lambda x: x.split('_')[0]}, {unified_timestamp}), | |
559 | }), | |
560 | } | |
561 | ||
562 | def _real_extract(self, url): | |
563 | site_id, corner_id, headline_id = self._match_valid_url(url).group('site', 'corner', 'headline') | |
564 | programme_id = f'{site_id}_{corner_id}' | |
565 | ||
566 | if site_id == 'F261': | |
567 | json_url = 'https://www.nhk.or.jp/s-media/news/news-site/list/v1/all.json' | |
568 | else: | |
569 | json_url = f'https://www.nhk.or.jp/radioondemand/json/{site_id}/bangumi_{programme_id}.json' | |
570 | ||
571 | meta = self._download_json(json_url, programme_id)['main'] | |
572 | ||
573 | series_meta = traverse_obj(meta, { | |
574 | 'title': 'program_name', | |
575 | 'channel': 'media_name', | |
54579be4 | 576 | 'uploader': 'media_name', |
8f0be90e | 577 | 'thumbnail': (('thumbnail_c', 'thumbnail_p'), {url_or_none}), |
578 | }, get_all=False) | |
579 | ||
580 | if headline_id: | |
581 | return self._extract_episode_info( | |
582 | traverse_obj(meta, ( | |
583 | 'detail_list', lambda _, v: v['headline_id'] == headline_id), get_all=False), | |
584 | programme_id, series_meta) | |
585 | ||
586 | def entries(): | |
587 | for headline in traverse_obj(meta, ('detail_list', ..., {dict})): | |
588 | yield self._extract_episode_info(headline, programme_id, series_meta) | |
589 | ||
590 | return self.playlist_result( | |
591 | entries(), programme_id, playlist_description=meta.get('site_detail'), **series_meta) | |
592 | ||
593 | ||
594 | class NhkRadioNewsPageIE(InfoExtractor): | |
595 | _VALID_URL = r'https?://www\.nhk\.or\.jp/radionews/?(?:$|[?#])' | |
596 | _TESTS = [{ | |
597 | # airs daily, on-the-hour most hours | |
598 | 'url': 'https://www.nhk.or.jp/radionews/', | |
599 | 'playlist_mincount': 5, | |
600 | 'info_dict': { | |
601 | 'id': 'F261_01', | |
602 | 'thumbnail': 'https://www.nhk.or.jp/radioondemand/json/F261/img/RADIONEWS_640.jpg', | |
603 | 'description': 'md5:bf2c5b397e44bc7eb26de98d8f15d79d', | |
604 | 'channel': 'NHKラジオ第1', | |
54579be4 | 605 | 'uploader': 'NHKラジオ第1', |
8f0be90e | 606 | 'title': 'NHKラジオニュース', |
607 | } | |
608 | }] | |
609 | ||
610 | def _real_extract(self, url): | |
611 | return self.url_result('https://www.nhk.or.jp/radio/ondemand/detail.html?p=F261_01', NhkRadiruIE) | |
81c8b9bd | 612 | |
613 | ||
614 | class NhkRadiruLiveIE(InfoExtractor): | |
615 | _GEO_COUNTRIES = ['JP'] | |
616 | _VALID_URL = r'https?://www\.nhk\.or\.jp/radio/player/\?ch=(?P<id>r[12]|fm)' | |
617 | _TESTS = [{ | |
618 | # radio 1, no area specified | |
619 | 'url': 'https://www.nhk.or.jp/radio/player/?ch=r1', | |
620 | 'info_dict': { | |
621 | 'id': 'r1-tokyo', | |
622 | 'title': 're:^NHKネットラジオ第1 東京.+$', | |
623 | 'ext': 'm4a', | |
624 | 'thumbnail': 'https://www.nhk.or.jp/common/img/media/r1-200x200.png', | |
625 | 'live_status': 'is_live', | |
626 | }, | |
627 | }, { | |
628 | # radio 2, area specified | |
629 | # (the area doesnt actually matter, r2 is national) | |
630 | 'url': 'https://www.nhk.or.jp/radio/player/?ch=r2', | |
631 | 'params': {'extractor_args': {'nhkradirulive': {'area': ['fukuoka']}}}, | |
632 | 'info_dict': { | |
633 | 'id': 'r2-fukuoka', | |
634 | 'title': 're:^NHKネットラジオ第2 福岡.+$', | |
635 | 'ext': 'm4a', | |
636 | 'thumbnail': 'https://www.nhk.or.jp/common/img/media/r2-200x200.png', | |
637 | 'live_status': 'is_live', | |
638 | }, | |
639 | }, { | |
640 | # fm, area specified | |
641 | 'url': 'https://www.nhk.or.jp/radio/player/?ch=fm', | |
642 | 'params': {'extractor_args': {'nhkradirulive': {'area': ['sapporo']}}}, | |
643 | 'info_dict': { | |
644 | 'id': 'fm-sapporo', | |
645 | 'title': 're:^NHKネットラジオFM 札幌.+$', | |
646 | 'ext': 'm4a', | |
647 | 'thumbnail': 'https://www.nhk.or.jp/common/img/media/fm-200x200.png', | |
648 | 'live_status': 'is_live', | |
649 | } | |
650 | }] | |
651 | ||
652 | _NOA_STATION_IDS = {'r1': 'n1', 'r2': 'n2', 'fm': 'n3'} | |
653 | ||
654 | def _real_extract(self, url): | |
655 | station = self._match_id(url) | |
656 | area = self._configuration_arg('area', ['tokyo'])[0] | |
657 | ||
658 | config = self._download_xml( | |
659 | 'https://www.nhk.or.jp/radio/config/config_web.xml', station, 'Downloading area information') | |
660 | data = config.find(f'.//data//area[.="{area}"]/..') | |
661 | ||
662 | if not data: | |
663 | raise ExtractorError('Invalid area. Valid areas are: %s' % ', '.join( | |
664 | [i.text for i in config.findall('.//data//area')]), expected=True) | |
665 | ||
666 | noa_info = self._download_json( | |
667 | f'https:{config.find(".//url_program_noa").text}'.format(area=data.find('areakey').text), | |
5af1f197 | 668 | station, note=f'Downloading {area} station metadata', fatal=False) |
81c8b9bd | 669 | present_info = traverse_obj(noa_info, ('nowonair_list', self._NOA_STATION_IDS.get(station), 'present')) |
670 | ||
671 | return { | |
672 | 'title': ' '.join(traverse_obj(present_info, (('service', 'area',), 'name', {str}))), | |
673 | 'id': join_nonempty(station, area), | |
674 | 'thumbnails': traverse_obj(present_info, ('service', 'images', ..., { | |
675 | 'url': 'url', | |
676 | 'width': ('width', {int_or_none}), | |
677 | 'height': ('height', {int_or_none}), | |
678 | })), | |
679 | 'formats': self._extract_m3u8_formats(data.find(f'{station}hls').text, station), | |
680 | 'is_live': True, | |
681 | } |