[youtube] Fix extraction.
[youtube-dl] / 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}|[^/]+?-\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         'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/audio/j_art-20150903-1/',
35         'only_matching': True,
36     }]
37     _API_URL_TEMPLATE = 'https://api.nhk.or.jp/nhkworld/%sod%slist/v7a/episode/%s/%s/all%s.json'
38
39     def _real_extract(self, url):
40         lang, m_type, episode_id = re.match(self._VALID_URL, url).groups()
41         if episode_id.isdigit():
42             episode_id = episode_id[:4] + '-' + episode_id[4:]
43
44         is_video = m_type == 'video'
45         episode = self._download_json(
46             self._API_URL_TEMPLATE % (
47                 'v' if is_video else 'r',
48                 'clip' if episode_id[:4] == '9999' else 'esd',
49                 episode_id, lang, '/all' if is_video else ''),
50             episode_id, query={'apikey': 'EJfK8jdS57GqlupFgAfAAwr573q01y6k'})['data']['episodes'][0]
51         title = episode.get('sub_title_clean') or episode['sub_title']
52
53         def get_clean_field(key):
54             return episode.get(key + '_clean') or episode.get(key)
55
56         series = get_clean_field('title')
57
58         thumbnails = []
59         for s, w, h in [('', 640, 360), ('_l', 1280, 720)]:
60             img_path = episode.get('image' + s)
61             if not img_path:
62                 continue
63             thumbnails.append({
64                 'id': '%dp' % h,
65                 'height': h,
66                 'width': w,
67                 'url': 'https://www3.nhk.or.jp' + img_path,
68             })
69
70         info = {
71             'id': episode_id + '-' + lang,
72             'title': '%s - %s' % (series, title) if series and title else title,
73             'description': get_clean_field('description'),
74             'thumbnails': thumbnails,
75             'series': series,
76             'episode': title,
77         }
78         if is_video:
79             info.update({
80                 '_type': 'url_transparent',
81                 'ie_key': 'Piksel',
82                 'url': 'https://player.piksel.com/v/refid/nhkworld/prefid/' + episode['vod_id'],
83             })
84         else:
85             audio = episode['audio']
86             audio_path = audio['audio']
87             info['formats'] = self._extract_m3u8_formats(
88                 'https://nhkworld-vh.akamaihd.net/i%s/master.m3u8' % audio_path,
89                 episode_id, 'm4a', entry_protocol='m3u8_native',
90                 m3u8_id='hls', fatal=False)
91             for f in info['formats']:
92                 f['language'] = lang
93         return info