]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/nhk.py
[fc2] Fix extraction (#2572)
[yt-dlp.git] / yt_dlp / extractor / nhk.py
CommitLineData
298a120a
AN
1from __future__ import unicode_literals
2
061d1cd9 3
298a120a 4from .common import InfoExtractor
29f7c58a 5from ..utils import urljoin
298a120a
AN
6
7
29f7c58a 8class NhkBaseIE(InfoExtractor):
9 _API_URL_TEMPLATE = 'https://api.nhk.or.jp/nhkworld/%sod%slist/v7a/%s/%s/%s/all%s.json'
10 _BASE_URL_REGEX = r'https?://www3\.nhk\.or\.jp/nhkworld/(?P<lang>[a-z]{2})/ondemand'
11 _TYPE_REGEX = r'/(?P<type>video|audio)/'
298a120a 12
29f7c58a 13 def _call_api(self, m_id, lang, is_video, is_episode, is_clip):
14 return self._download_json(
15 self._API_URL_TEMPLATE % (
16 'v' if is_video else 'r',
17 'clip' if is_clip else 'esd',
18 'episode' if is_episode else 'program',
19 m_id, lang, '/all' if is_video else ''),
20 m_id, query={'apikey': 'EJfK8jdS57GqlupFgAfAAwr573q01y6k'})['data']['episodes'] or []
21
22 def _extract_episode_info(self, url, episode=None):
23 fetch_episode = episode is None
5ad28e7f 24 lang, m_type, episode_id = NhkVodIE._match_valid_url(url).groups()
061d1cd9
RA
25 if episode_id.isdigit():
26 episode_id = episode_id[:4] + '-' + episode_id[4:]
f9b373af 27
061d1cd9 28 is_video = m_type == 'video'
29f7c58a 29 if fetch_episode:
30 episode = self._call_api(
31 episode_id, lang, is_video, True, episode_id[:4] == '9999')[0]
061d1cd9 32 title = episode.get('sub_title_clean') or episode['sub_title']
45396dd2 33
061d1cd9
RA
34 def get_clean_field(key):
35 return episode.get(key + '_clean') or episode.get(key)
45396dd2 36
061d1cd9 37 series = get_clean_field('title')
45396dd2 38
061d1cd9
RA
39 thumbnails = []
40 for s, w, h in [('', 640, 360), ('_l', 1280, 720)]:
41 img_path = episode.get('image' + s)
42 if not img_path:
43 continue
44 thumbnails.append({
45 'id': '%dp' % h,
46 'height': h,
47 'width': w,
48 'url': 'https://www3.nhk.or.jp' + img_path,
49 })
298a120a 50
061d1cd9
RA
51 info = {
52 'id': episode_id + '-' + lang,
f9b373af 53 'title': '%s - %s' % (series, title) if series and title else title,
061d1cd9
RA
54 'description': get_clean_field('description'),
55 'thumbnails': thumbnails,
f9b373af
S
56 'series': series,
57 'episode': title,
58 }
061d1cd9 59 if is_video:
29f7c58a 60 vod_id = episode['vod_id']
061d1cd9
RA
61 info.update({
62 '_type': 'url_transparent',
a373befa 63 'ie_key': 'Piksel',
29f7c58a 64 'url': 'https://player.piksel.com/v/refid/nhkworld/prefid/' + vod_id,
65 'id': vod_id,
061d1cd9
RA
66 })
67 else:
29f7c58a 68 if fetch_episode:
69 audio_path = episode['audio']['audio']
70 info['formats'] = self._extract_m3u8_formats(
71 'https://nhkworld-vh.akamaihd.net/i%s/master.m3u8' % audio_path,
72 episode_id, 'm4a', entry_protocol='m3u8_native',
73 m3u8_id='hls', fatal=False)
74 for f in info['formats']:
75 f['language'] = lang
df6c409d 76 self._sort_formats(info['formats'])
29f7c58a 77 else:
78 info.update({
79 '_type': 'url_transparent',
80 'ie_key': NhkVodIE.ie_key(),
81 'url': url,
82 })
061d1cd9 83 return info
29f7c58a 84
85
86class NhkVodIE(NhkBaseIE):
87 _VALID_URL = r'%s%s(?P<id>\d{7}|[^/]+?-\d{8}-[0-9a-z]+)' % (NhkBaseIE._BASE_URL_REGEX, NhkBaseIE._TYPE_REGEX)
88 # Content available only for a limited period of time. Visit
89 # https://www3.nhk.or.jp/nhkworld/en/ondemand/ for working samples.
90 _TESTS = [{
91 # video clip
92 'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/video/9999011/',
93 'md5': '7a90abcfe610ec22a6bfe15bd46b30ca',
94 'info_dict': {
95 'id': 'a95j5iza',
96 'ext': 'mp4',
97 'title': "Dining with the Chef - Chef Saito's Family recipe: MENCHI-KATSU",
98 'description': 'md5:5aee4a9f9d81c26281862382103b0ea5',
99 'timestamp': 1565965194,
100 'upload_date': '20190816',
101 },
102 }, {
103 # audio clip
104 'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/audio/r_inventions-20201104-1/',
105 'info_dict': {
106 'id': 'r_inventions-20201104-1-en',
107 'ext': 'm4a',
108 'title': "Japan's Top Inventions - Miniature Video Cameras",
109 'description': 'md5:07ea722bdbbb4936fdd360b6a480c25b',
110 },
111 'params': {
112 # m3u8 download
113 'skip_download': True,
114 },
115 }, {
116 'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/video/2015173/',
117 'only_matching': True,
118 }, {
119 'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/audio/plugin-20190404-1/',
120 'only_matching': True,
121 }, {
122 'url': 'https://www3.nhk.or.jp/nhkworld/fr/ondemand/audio/plugin-20190404-1/',
123 'only_matching': True,
124 }, {
125 'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/audio/j_art-20150903-1/',
126 'only_matching': True,
127 }]
128
129 def _real_extract(self, url):
130 return self._extract_episode_info(url)
131
132
133class NhkVodProgramIE(NhkBaseIE):
134 _VALID_URL = r'%s/program%s(?P<id>[0-9a-z]+)(?:.+?\btype=(?P<episode_type>clip|(?:radio|tv)Episode))?' % (NhkBaseIE._BASE_URL_REGEX, NhkBaseIE._TYPE_REGEX)
135 _TESTS = [{
136 # video program episodes
137 'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/program/video/japanrailway',
138 'info_dict': {
139 'id': 'japanrailway',
140 'title': 'Japan Railway Journal',
141 },
142 'playlist_mincount': 1,
143 }, {
144 # video program clips
145 'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/program/video/japanrailway/?type=clip',
146 'info_dict': {
147 'id': 'japanrailway',
148 'title': 'Japan Railway Journal',
149 },
150 'playlist_mincount': 5,
151 }, {
152 'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/program/video/10yearshayaomiyazaki/',
153 'only_matching': True,
154 }, {
155 # audio program
156 'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/program/audio/listener/',
157 'only_matching': True,
158 }]
159
160 def _real_extract(self, url):
5ad28e7f 161 lang, m_type, program_id, episode_type = self._match_valid_url(url).groups()
29f7c58a 162
163 episodes = self._call_api(
164 program_id, lang, m_type == 'video', False, episode_type == 'clip')
165
166 entries = []
167 for episode in episodes:
168 episode_path = episode.get('url')
169 if not episode_path:
170 continue
171 entries.append(self._extract_episode_info(
172 urljoin(url, episode_path), episode))
173
174 program_title = None
175 if entries:
176 program_title = entries[0].get('series')
177
178 return self.playlist_result(entries, program_id, program_title)