cce4bb47283960878ab2200e036f61c976c4d85a
[ytdl] / youtube_dl / extractor / nhk.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6
7
8 class NhkVodIE(InfoExtractor):
9     _VALID_URL = r'https?://www3\.nhk\.or\.jp/nhkworld/(?P<lang>[a-z]{2})/ondemand/(?P<type>video|audio)/(?P<id>\d{7}|[a-z]+-\d{8}-\d+)'
10     # Content available only for a limited period of time. Visit
11     # https://www3.nhk.or.jp/nhkworld/en/ondemand/ for working samples.
12     _TESTS = [{
13         'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/video/2015173/',
14         'only_matching': True,
15     }, {
16         'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/audio/plugin-20190404-1/',
17         'only_matching': True,
18     }, {
19         'url': 'https://www3.nhk.or.jp/nhkworld/fr/ondemand/audio/plugin-20190404-1/',
20         'only_matching': True,
21     }]
22     _API_URL_TEMPLATE = 'https://api.nhk.or.jp/nhkworld/%sodesdlist/v7/episode/%s/%s/all%s.json'
23
24     def _real_extract(self, url):
25         lang, m_type, episode_id = re.match(self._VALID_URL, url).groups()
26         if episode_id.isdigit():
27             episode_id = episode_id[:4] + '-' + episode_id[4:]
28
29         is_video = m_type == 'video'
30         episode = self._download_json(
31             self._API_URL_TEMPLATE % ('v' if is_video else 'r', episode_id, lang, '/all' if is_video else ''),
32             episode_id, query={'apikey': 'EJfK8jdS57GqlupFgAfAAwr573q01y6k'})['data']['episodes'][0]
33         title = episode.get('sub_title_clean') or episode['sub_title']
34
35         def get_clean_field(key):
36             return episode.get(key + '_clean') or episode.get(key)
37
38         series = get_clean_field('title')
39
40         thumbnails = []
41         for s, w, h in [('', 640, 360), ('_l', 1280, 720)]:
42             img_path = episode.get('image' + s)
43             if not img_path:
44                 continue
45             thumbnails.append({
46                 'id': '%dp' % h,
47                 'height': h,
48                 'width': w,
49                 'url': 'https://www3.nhk.or.jp' + img_path,
50             })
51
52         info = {
53             'id': episode_id + '-' + lang,
54             'title': '%s - %s' % (series, title) if series and title else title,
55             'description': get_clean_field('description'),
56             'thumbnails': thumbnails,
57             'series': series,
58             'episode': title,
59         }
60         if is_video:
61             info.update({
62                 '_type': 'url_transparent',
63                 'ie_key': 'Piksel',
64                 'url': 'https://player.piksel.com/v/refid/nhkworld/prefid/' + episode['vod_id'],
65             })
66         else:
67             audio = episode['audio']
68             audio_path = audio['audio']
69             info['formats'] = self._extract_m3u8_formats(
70                 'https://nhks-vh.akamaihd.net/i%s/master.m3u8' % audio_path,
71                 episode_id, 'm4a', m3u8_id='hls', fatal=False)
72             for proto in ('rtmpt', 'rtmp'):
73                 info['formats'].append({
74                     'ext': 'flv',
75                     'format_id': proto,
76                     'url': '%s://flv.nhk.or.jp/ondemand/mp4:flv%s' % (proto, audio_path),
77                     'vcodec': 'none',
78                 })
79             for f in info['formats']:
80                 f['language'] = lang
81         return info