nhk.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. class NhkVodIE(InfoExtractor):
  5. _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+)'
  6. # Content available only for a limited period of time. Visit
  7. # https://www3.nhk.or.jp/nhkworld/en/ondemand/ for working samples.
  8. _TESTS = [{
  9. # video clip
  10. 'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/video/9999011/',
  11. 'md5': '256a1be14f48d960a7e61e2532d95ec3',
  12. 'info_dict': {
  13. 'id': 'a95j5iza',
  14. 'ext': 'mp4',
  15. 'title': "Dining with the Chef - Chef Saito's Family recipe: MENCHI-KATSU",
  16. 'description': 'md5:5aee4a9f9d81c26281862382103b0ea5',
  17. 'timestamp': 1565965194,
  18. 'upload_date': '20190816',
  19. },
  20. }, {
  21. # audio clip
  22. 'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/audio/r_inventions-20201104-1/',
  23. 'info_dict': {
  24. 'id': 'r_inventions-20201104-1-en',
  25. 'ext': 'm4a',
  26. 'title': "Japan's Top Inventions - Miniature Video Cameras",
  27. 'description': 'md5:07ea722bdbbb4936fdd360b6a480c25b',
  28. },
  29. 'params': {
  30. # m3u8 download
  31. 'skip_download': True,
  32. },
  33. }, {
  34. 'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/video/2015173/',
  35. 'only_matching': True,
  36. }, {
  37. 'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/audio/plugin-20190404-1/',
  38. 'only_matching': True,
  39. }, {
  40. 'url': 'https://www3.nhk.or.jp/nhkworld/fr/ondemand/audio/plugin-20190404-1/',
  41. 'only_matching': True,
  42. }, {
  43. 'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/audio/j_art-20150903-1/',
  44. 'only_matching': True,
  45. }]
  46. _API_URL_TEMPLATE = 'https://api.nhk.or.jp/nhkworld/%sod%slist/v7a/episode/%s/%s/all%s.json'
  47. def _real_extract(self, url):
  48. lang, m_type, episode_id = re.match(self._VALID_URL, url).groups()
  49. if episode_id.isdigit():
  50. episode_id = episode_id[:4] + '-' + episode_id[4:]
  51. is_video = m_type == 'video'
  52. episode = self._download_json(
  53. self._API_URL_TEMPLATE % (
  54. 'v' if is_video else 'r',
  55. 'clip' if episode_id[:4] == '9999' else 'esd',
  56. episode_id, lang, '/all' if is_video else ''),
  57. episode_id, query={'apikey': 'EJfK8jdS57GqlupFgAfAAwr573q01y6k'})['data']['episodes'][0]
  58. title = episode.get('sub_title_clean') or episode['sub_title']
  59. def get_clean_field(key):
  60. return episode.get(key + '_clean') or episode.get(key)
  61. series = get_clean_field('title')
  62. thumbnails = []
  63. for s, w, h in [('', 640, 360), ('_l', 1280, 720)]:
  64. img_path = episode.get('image' + s)
  65. if not img_path:
  66. continue
  67. thumbnails.append({
  68. 'id': '%dp' % h,
  69. 'height': h,
  70. 'width': w,
  71. 'url': 'https://www3.nhk.or.jp' + img_path,
  72. })
  73. info = {
  74. 'id': episode_id + '-' + lang,
  75. 'title': '%s - %s' % (series, title) if series and title else title,
  76. 'description': get_clean_field('description'),
  77. 'thumbnails': thumbnails,
  78. 'series': series,
  79. 'episode': title,
  80. }
  81. if is_video:
  82. info.update({
  83. '_type': 'url_transparent',
  84. 'ie_key': 'Piksel',
  85. 'url': 'https://player.piksel.com/v/refid/nhkworld/prefid/' + episode['vod_id'],
  86. })
  87. else:
  88. audio = episode['audio']
  89. audio_path = audio['audio']
  90. info['formats'] = self._extract_m3u8_formats(
  91. 'https://nhkworld-vh.akamaihd.net/i%s/master.m3u8' % audio_path,
  92. episode_id, 'm4a', entry_protocol='m3u8_native',
  93. m3u8_id='hls', fatal=False)
  94. for f in info['formats']:
  95. f['language'] = lang
  96. return info