]> asedeno.scripts.mit.edu Git - youtube-dl.git/blob - youtube_dl/extractor/nhk.py
[NHK] Use new API URL
[youtube-dl.git] / youtube_dl / extractor / nhk.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import urljoin
8
9
10 class NhkBaseIE(InfoExtractor):
11     _API_URL_TEMPLATE = 'https://nwapi.nhk.jp/nhkworld/%sod%slist/v7b/%s/%s/%s/all%s.json'
12     _BASE_URL_REGEX = r'https?://www3\.nhk\.or\.jp/nhkworld/(?P<lang>[a-z]{2})/ondemand'
13     _TYPE_REGEX = r'/(?P<type>video|audio)/'
14
15     def _call_api(self, m_id, lang, is_video, is_episode, is_clip):
16         return self._download_json(
17             self._API_URL_TEMPLATE % (
18                 'v' if is_video else 'r',
19                 'clip' if is_clip else 'esd',
20                 'episode' if is_episode else 'program',
21                 m_id, lang, '/all' if is_video else ''),
22             m_id, query={'apikey': 'EJfK8jdS57GqlupFgAfAAwr573q01y6k'})['data']['episodes'] or []
23
24     def _extract_episode_info(self, url, episode=None):
25         fetch_episode = episode is None
26         lang, m_type, episode_id = re.match(NhkVodIE._VALID_URL, url).groups()
27         if len(episode_id) == 7:
28             episode_id = episode_id[:4] + '-' + episode_id[4:]
29
30         is_video = m_type == 'video'
31         if fetch_episode:
32             episode = self._call_api(
33                 episode_id, lang, is_video, True, episode_id[:4] == '9999')[0]
34         title = episode.get('sub_title_clean') or episode['sub_title']
35
36         def get_clean_field(key):
37             return episode.get(key + '_clean') or episode.get(key)
38
39         series = get_clean_field('title')
40
41         thumbnails = []
42         for s, w, h in [('', 640, 360), ('_l', 1280, 720)]:
43             img_path = episode.get('image' + s)
44             if not img_path:
45                 continue
46             thumbnails.append({
47                 'id': '%dp' % h,
48                 'height': h,
49                 'width': w,
50                 'url': 'https://www3.nhk.or.jp' + img_path,
51             })
52
53         info = {
54             'id': episode_id + '-' + lang,
55             'title': '%s - %s' % (series, title) if series and title else title,
56             'description': get_clean_field('description'),
57             'thumbnails': thumbnails,
58             'series': series,
59             'episode': title,
60         }
61         if is_video:
62             vod_id = episode['vod_id']
63             info.update({
64                 '_type': 'url_transparent',
65                 'ie_key': 'Piksel',
66                 'url': 'https://player.piksel.com/v/refid/nhkworld/prefid/' + vod_id,
67                 'id': vod_id,
68             })
69         else:
70             if fetch_episode:
71                 audio_path = episode['audio']['audio']
72                 info['formats'] = self._extract_m3u8_formats(
73                     'https://nhkworld-vh.akamaihd.net/i%s/master.m3u8' % audio_path,
74                     episode_id, 'm4a', entry_protocol='m3u8_native',
75                     m3u8_id='hls', fatal=False)
76                 for f in info['formats']:
77                     f['language'] = lang
78             else:
79                 info.update({
80                     '_type': 'url_transparent',
81                     'ie_key': NhkVodIE.ie_key(),
82                     'url': url,
83                 })
84         return info
85
86
87 class NhkVodIE(NhkBaseIE):
88     # the 7-character IDs can have alphabetic chars too: assume [a-z] rather than just [a-f], eg
89     _VALID_URL = r'%s%s(?P<id>[0-9a-z]{7}|[^/]+?-\d{8}-[0-9a-z]+)' % (NhkBaseIE._BASE_URL_REGEX, NhkBaseIE._TYPE_REGEX)
90     # Content available only for a limited period of time. Visit
91     # https://www3.nhk.or.jp/nhkworld/en/ondemand/ for working samples.
92     _TESTS = [{
93         # video clip
94         'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/video/9999011/',
95         'md5': '7a90abcfe610ec22a6bfe15bd46b30ca',
96         'info_dict': {
97             'id': 'a95j5iza',
98             'ext': 'mp4',
99             'title': "Dining with the Chef - Chef Saito's Family recipe: MENCHI-KATSU",
100             'description': 'md5:5aee4a9f9d81c26281862382103b0ea5',
101             'timestamp': 1565965194,
102             'upload_date': '20190816',
103         },
104     }, {
105         # audio clip
106         'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/audio/r_inventions-20201104-1/',
107         'info_dict': {
108             'id': 'r_inventions-20201104-1-en',
109             'ext': 'm4a',
110             'title': "Japan's Top Inventions - Miniature Video Cameras",
111             'description': 'md5:07ea722bdbbb4936fdd360b6a480c25b',
112         },
113         'params': {
114             # m3u8 download
115             'skip_download': True,
116         },
117     }, {
118         'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/video/2015173/',
119         'only_matching': True,
120     }, {
121         'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/audio/plugin-20190404-1/',
122         'only_matching': True,
123     }, {
124         'url': 'https://www3.nhk.or.jp/nhkworld/fr/ondemand/audio/plugin-20190404-1/',
125         'only_matching': True,
126     }, {
127         'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/audio/j_art-20150903-1/',
128         'only_matching': True,
129     }, {
130         # video, alphabetic character in ID #29670
131         'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/video/9999a34/',
132         'only_matching': True,
133         'info_dict': {
134             'id': 'qfjay6cg',
135             'ext': 'mp4',
136             'title': 'DESIGN TALKS plus - Fishermen’s Finery',
137             'description': 'md5:8a8f958aaafb0d7cb59d38de53f1e448',
138             'thumbnail': r're:^https?:/(/[a-z0-9.-]+)+\.jpg\?w=1920&h=1080$',
139             'upload_date': '20210615',
140             'timestamp': 1623722008,
141         }
142     }]
143
144     def _real_extract(self, url):
145         return self._extract_episode_info(url)
146
147
148 class NhkVodProgramIE(NhkBaseIE):
149     _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)
150     _TESTS = [{
151         # video program episodes
152         'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/program/video/japanrailway',
153         'info_dict': {
154             'id': 'japanrailway',
155             'title': 'Japan Railway Journal',
156         },
157         'playlist_mincount': 1,
158     }, {
159         # video program clips
160         'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/program/video/japanrailway/?type=clip',
161         'info_dict': {
162             'id': 'japanrailway',
163             'title': 'Japan Railway Journal',
164         },
165         'playlist_mincount': 5,
166     }, {
167         'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/program/video/10yearshayaomiyazaki/',
168         'only_matching': True,
169     }, {
170         # audio program
171         'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/program/audio/listener/',
172         'only_matching': True,
173     }]
174
175     def _real_extract(self, url):
176         lang, m_type, program_id, episode_type = re.match(self._VALID_URL, url).groups()
177
178         episodes = self._call_api(
179             program_id, lang, m_type == 'video', False, episode_type == 'clip')
180
181         entries = []
182         for episode in episodes:
183             episode_path = episode.get('url')
184             if not episode_path:
185                 continue
186             entries.append(self._extract_episode_info(
187                 urljoin(url, episode_path), episode))
188
189         program_title = None
190         if entries:
191             program_title = entries[0].get('series')
192
193         return self.playlist_result(entries, program_id, program_title)