nhk.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import ExtractorError
  5. class NhkBaseIE(InfoExtractor):
  6. _API_URL_TEMPLATE = 'https://api.nhk.or.jp/nhkworld/%sod%slist/v7a/%s/%s/%s/all%s.json'
  7. def _get_clean_field(self, episode, key):
  8. return episode.get(key + '_clean') or episode.get(key)
  9. def _list_episodes(self, m_id, lang, is_video, is_episode):
  10. return self._download_json(
  11. self._API_URL_TEMPLATE % (
  12. 'v' if is_video else 'r',
  13. 'clip' if m_id[:4] == '9999' else 'esd',
  14. 'episode' if is_episode else 'program',
  15. m_id, lang, '/all' if is_video else ''),
  16. m_id, query={'apikey': 'EJfK8jdS57GqlupFgAfAAwr573q01y6k'})['data']['episodes']
  17. def _parse_episode_json(self, episode, lang, is_video):
  18. title = episode.get('sub_title_clean') or episode['sub_title']
  19. episode_id = None
  20. if is_video:
  21. pgm_id = episode.get('pgm_id')
  22. pgm_no = episode.get('pgm_no')
  23. if not (pgm_id and pgm_no):
  24. missing_field = 'pgm_id' if not pgm_id else 'pgm_no'
  25. raise ExtractorError('Cannot download episode. Field %s is missing from episode JSON.' % missing_field)
  26. episode_id = pgm_id + pgm_no
  27. else:
  28. pgm_gr_id = episode.get('pgm_gr_id')
  29. first_onair_date = episode.get('first_onair_date')
  30. first_onair_no = episode.get('first_onair_no')
  31. if not (pgm_gr_id and first_onair_date and first_onair_no):
  32. missing_field = 'pgm_gr_id' if not pgm_gr_id else 'first_onair_date' if not first_onair_date else 'first_onair_no'
  33. raise ExtractorError('Cannot download episode. Field %s is missing from episode JSON.' % missing_field)
  34. episode_id = pgm_gr_id + '-' + first_onair_date + '-' + first_onair_no
  35. series = self._get_clean_field(episode, 'title')
  36. thumbnails = []
  37. for s, w, h in [('', 640, 360), ('_l', 1280, 720)]:
  38. img_path = episode.get('image' + s)
  39. if not img_path:
  40. continue
  41. thumbnails.append({
  42. 'id': '%dp' % h,
  43. 'height': h,
  44. 'width': w,
  45. 'url': 'https://www3.nhk.or.jp' + img_path,
  46. })
  47. info = {
  48. 'id': episode_id + '-' + lang,
  49. 'title': '%s - %s' % (series, title) if series and title else title,
  50. 'description': self._get_clean_field(episode, 'description'),
  51. 'thumbnails': thumbnails,
  52. 'series': series,
  53. 'episode': title,
  54. }
  55. if is_video:
  56. info.update({
  57. '_type': 'url_transparent',
  58. 'ie_key': 'Piksel',
  59. 'url': 'https://player.piksel.com/v/refid/nhkworld/prefid/' + episode['vod_id'],
  60. })
  61. else:
  62. audio = episode['audio']
  63. audio_path = audio['audio']
  64. info['formats'] = self._extract_m3u8_formats(
  65. 'https://nhkworld-vh.akamaihd.net/i%s/master.m3u8' % audio_path,
  66. episode_id, 'm4a', entry_protocol='m3u8_native',
  67. m3u8_id='hls', fatal=False)
  68. for f in info['formats']:
  69. f['language'] = lang
  70. return info
  71. class NhkVodIE(NhkBaseIE):
  72. _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+)'
  73. # Content available only for a limited period of time. Visit
  74. # https://www3.nhk.or.jp/nhkworld/en/ondemand/ for working samples.
  75. _TESTS = [{
  76. # clip
  77. 'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/video/9999011/',
  78. 'md5': '256a1be14f48d960a7e61e2532d95ec3',
  79. 'info_dict': {
  80. 'id': 'a95j5iza',
  81. 'ext': 'mp4',
  82. 'title': "Dining with the Chef - Chef Saito's Family recipe: MENCHI-KATSU",
  83. 'description': 'md5:5aee4a9f9d81c26281862382103b0ea5',
  84. 'timestamp': 1565965194,
  85. 'upload_date': '20190816',
  86. },
  87. }, {
  88. # audio clip
  89. 'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/audio/r_inventions-20201104-1/',
  90. 'info_dict': {
  91. 'id': 'r_inventions-20201104-1-en',
  92. 'ext': 'm4a',
  93. 'title': "Japan's Top Inventions - Miniature Video Cameras",
  94. 'description': 'md5:07ea722bdbbb4936fdd360b6a480c25b',
  95. },
  96. 'params': {
  97. # m3u8 download
  98. 'skip_download': True,
  99. },
  100. }, {
  101. 'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/video/2015173/',
  102. 'only_matching': True,
  103. }, {
  104. 'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/audio/plugin-20190404-1/',
  105. 'only_matching': True,
  106. }, {
  107. 'url': 'https://www3.nhk.or.jp/nhkworld/fr/ondemand/audio/plugin-20190404-1/',
  108. 'only_matching': True,
  109. }, {
  110. 'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/audio/j_art-20150903-1/',
  111. 'only_matching': True,
  112. }]
  113. def _real_extract(self, url):
  114. lang, m_type, episode_id = re.match(self._VALID_URL, url).groups()
  115. if episode_id.isdigit():
  116. episode_id = episode_id[:4] + '-' + episode_id[4:]
  117. episode = self._list_episodes(episode_id, lang, m_type == 'video', True)[0]
  118. return self._parse_episode_json(episode, lang, m_type == 'video')
  119. class NhkVodProgramIE(NhkBaseIE):
  120. _VALID_URL = r'https?://www3\.nhk\.or\.jp/nhkworld/(?P<lang>[a-z]{2})/ondemand/(program/video)/(?P<id>\w+)'
  121. # Content available only for a limited period of time. Visit
  122. # https://www3.nhk.or.jp/nhkworld/en/ondemand/ for working samples.
  123. _TESTS = [{
  124. # video program
  125. 'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/program/video/japanrailway',
  126. 'info_dict': {
  127. 'id': 'japanrailway',
  128. 'title': 'Japan Railway Journal',
  129. },
  130. 'playlist_mincount': 1,
  131. }, {
  132. 'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/program/video/10yearshayaomiyazaki/',
  133. 'only_matching': True,
  134. }]
  135. def _real_extract(self, url):
  136. lang, m_type, program_id = re.match(self._VALID_URL, url).groups()
  137. episodes = self._list_episodes(program_id, lang, True, False)
  138. if episodes:
  139. return self.playlist_result(
  140. [self._parse_episode_json(episode, lang, True)
  141. for episode in episodes],
  142. self._get_clean_field(episodes[0], 'pgm_gr_id'), self._get_clean_field(episodes[0], 'title'))
  143. else:
  144. raise ExtractorError('No episodes returned for program with ID: %s' % program_id, expected=True)