[nhk] Add support for clips
[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         # clip
14         'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/video/9999011/',
15         'md5': '256a1be14f48d960a7e61e2532d95ec3',
16         'info_dict': {
17             'id': 'a95j5iza',
18             'ext': 'mp4',
19             'title': "Dining with the Chef - Chef Saito's Family recipe: MENCHI-KATSU",
20             'description': 'md5:5aee4a9f9d81c26281862382103b0ea5',
21             'timestamp': 1565965194,
22             'upload_date': '20190816',
23         },
24     }, {
25         'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/video/2015173/',
26         'only_matching': True,
27     }, {
28         'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/audio/plugin-20190404-1/',
29         'only_matching': True,
30     }, {
31         'url': 'https://www3.nhk.or.jp/nhkworld/fr/ondemand/audio/plugin-20190404-1/',
32         'only_matching': True,
33     }]
34     _API_URL_TEMPLATE = 'https://api.nhk.or.jp/nhkworld/%sod%slist/v7/episode/%s/%s/all%s.json'
35
36     def _real_extract(self, url):
37         lang, m_type, episode_id = re.match(self._VALID_URL, url).groups()
38         if episode_id.isdigit():
39             episode_id = episode_id[:4] + '-' + episode_id[4:]
40
41         is_video = m_type == 'video'
42         episode = self._download_json(
43             self._API_URL_TEMPLATE % (
44                 'v' if is_video else 'r',
45                 'clip' if episode_id[:4] == '9999' else 'esd',
46                 episode_id, lang, '/all' if is_video else ''),
47             episode_id, query={'apikey': 'EJfK8jdS57GqlupFgAfAAwr573q01y6k'})['data']['episodes'][0]
48         title = episode.get('sub_title_clean') or episode['sub_title']
49
50         def get_clean_field(key):
51             return episode.get(key + '_clean') or episode.get(key)
52
53         series = get_clean_field('title')
54
55         thumbnails = []
56         for s, w, h in [('', 640, 360), ('_l', 1280, 720)]:
57             img_path = episode.get('image' + s)
58             if not img_path:
59                 continue
60             thumbnails.append({
61                 'id': '%dp' % h,
62                 'height': h,
63                 'width': w,
64                 'url': 'https://www3.nhk.or.jp' + img_path,
65             })
66
67         info = {
68             'id': episode_id + '-' + lang,
69             'title': '%s - %s' % (series, title) if series and title else title,
70             'description': get_clean_field('description'),
71             'thumbnails': thumbnails,
72             'series': series,
73             'episode': title,
74         }
75         if is_video:
76             info.update({
77                 '_type': 'url_transparent',
78                 'ie_key': 'Piksel',
79                 'url': 'https://player.piksel.com/v/refid/nhkworld/prefid/' + episode['vod_id'],
80             })
81         else:
82             audio = episode['audio']
83             audio_path = audio['audio']
84             info['formats'] = self._extract_m3u8_formats(
85                 'https://nhks-vh.akamaihd.net/i%s/master.m3u8' % audio_path,
86                 episode_id, 'm4a', m3u8_id='hls', fatal=False)
87             for proto in ('rtmpt', 'rtmp'):
88                 info['formats'].append({
89                     'ext': 'flv',
90                     'format_id': proto,
91                     'url': '%s://flv.nhk.or.jp/ondemand/mp4:flv%s' % (proto, audio_path),
92                     'vcodec': 'none',
93                 })
94             for f in info['formats']:
95                 f['language'] = lang
96         return info