logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

nhk.py (7360B)


  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import urljoin
  6. class NhkBaseIE(InfoExtractor):
  7. _API_URL_TEMPLATE = 'https://nwapi.nhk.jp/nhkworld/%sod%slist/v7b/%s/%s/%s/all%s.json'
  8. _BASE_URL_REGEX = r'https?://www3\.nhk\.or\.jp/nhkworld/(?P<lang>[a-z]{2})/ondemand'
  9. _TYPE_REGEX = r'/(?P<type>video|audio)/'
  10. def _call_api(self, m_id, lang, is_video, is_episode, is_clip):
  11. return self._download_json(
  12. self._API_URL_TEMPLATE % (
  13. 'v' if is_video else 'r',
  14. 'clip' if is_clip else 'esd',
  15. 'episode' if is_episode else 'program',
  16. m_id, lang, '/all' if is_video else ''),
  17. m_id, query={'apikey': 'EJfK8jdS57GqlupFgAfAAwr573q01y6k'})['data']['episodes'] or []
  18. def _extract_episode_info(self, url, episode=None):
  19. fetch_episode = episode is None
  20. lang, m_type, episode_id = re.match(NhkVodIE._VALID_URL, url).groups()
  21. if len(episode_id) == 7:
  22. episode_id = episode_id[:4] + '-' + episode_id[4:]
  23. is_video = m_type == 'video'
  24. if fetch_episode:
  25. episode = self._call_api(
  26. episode_id, lang, is_video, True, episode_id[:4] == '9999')[0]
  27. title = episode.get('sub_title_clean') or episode['sub_title']
  28. def get_clean_field(key):
  29. return episode.get(key + '_clean') or episode.get(key)
  30. series = get_clean_field('title')
  31. thumbnails = []
  32. for s, w, h in [('', 640, 360), ('_l', 1280, 720)]:
  33. img_path = episode.get('image' + s)
  34. if not img_path:
  35. continue
  36. thumbnails.append({
  37. 'id': '%dp' % h,
  38. 'height': h,
  39. 'width': w,
  40. 'url': 'https://www3.nhk.or.jp' + img_path,
  41. })
  42. info = {
  43. 'id': episode_id + '-' + lang,
  44. 'title': '%s - %s' % (series, title) if series and title else title,
  45. 'description': get_clean_field('description'),
  46. 'thumbnails': thumbnails,
  47. 'series': series,
  48. 'episode': title,
  49. }
  50. if is_video:
  51. vod_id = episode['vod_id']
  52. info.update({
  53. '_type': 'url_transparent',
  54. 'ie_key': 'Piksel',
  55. 'url': 'https://player.piksel.com/v/refid/nhkworld/prefid/' + vod_id,
  56. 'id': vod_id,
  57. })
  58. else:
  59. if fetch_episode:
  60. audio_path = episode['audio']['audio']
  61. info['formats'] = self._extract_m3u8_formats(
  62. 'https://nhkworld-vh.akamaihd.net/i%s/master.m3u8' % audio_path,
  63. episode_id, 'm4a', entry_protocol='m3u8_native',
  64. m3u8_id='hls', fatal=False)
  65. for f in info['formats']:
  66. f['language'] = lang
  67. else:
  68. info.update({
  69. '_type': 'url_transparent',
  70. 'ie_key': NhkVodIE.ie_key(),
  71. 'url': url,
  72. })
  73. return info
  74. class NhkVodIE(NhkBaseIE):
  75. # the 7-character IDs can have alphabetic chars too: assume [a-z] rather than just [a-f], eg
  76. _VALID_URL = r'%s%s(?P<id>[0-9a-z]{7}|[^/]+?-\d{8}-[0-9a-z]+)' % (NhkBaseIE._BASE_URL_REGEX, NhkBaseIE._TYPE_REGEX)
  77. # Content available only for a limited period of time. Visit
  78. # https://www3.nhk.or.jp/nhkworld/en/ondemand/ for working samples.
  79. _TESTS = [{
  80. # video clip
  81. 'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/video/9999011/',
  82. 'md5': '7a90abcfe610ec22a6bfe15bd46b30ca',
  83. 'info_dict': {
  84. 'id': 'a95j5iza',
  85. 'ext': 'mp4',
  86. 'title': "Dining with the Chef - Chef Saito's Family recipe: MENCHI-KATSU",
  87. 'description': 'md5:5aee4a9f9d81c26281862382103b0ea5',
  88. 'timestamp': 1565965194,
  89. 'upload_date': '20190816',
  90. },
  91. }, {
  92. # audio clip
  93. 'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/audio/r_inventions-20201104-1/',
  94. 'info_dict': {
  95. 'id': 'r_inventions-20201104-1-en',
  96. 'ext': 'm4a',
  97. 'title': "Japan's Top Inventions - Miniature Video Cameras",
  98. 'description': 'md5:07ea722bdbbb4936fdd360b6a480c25b',
  99. },
  100. 'params': {
  101. # m3u8 download
  102. 'skip_download': True,
  103. },
  104. }, {
  105. 'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/video/2015173/',
  106. 'only_matching': True,
  107. }, {
  108. 'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/audio/plugin-20190404-1/',
  109. 'only_matching': True,
  110. }, {
  111. 'url': 'https://www3.nhk.or.jp/nhkworld/fr/ondemand/audio/plugin-20190404-1/',
  112. 'only_matching': True,
  113. }, {
  114. 'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/audio/j_art-20150903-1/',
  115. 'only_matching': True,
  116. }, {
  117. # video, alphabetic character in ID #29670
  118. 'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/video/9999a34/',
  119. 'only_matching': True,
  120. 'info_dict': {
  121. 'id': 'qfjay6cg',
  122. 'ext': 'mp4',
  123. 'title': 'DESIGN TALKS plus - Fishermen’s Finery',
  124. 'description': 'md5:8a8f958aaafb0d7cb59d38de53f1e448',
  125. 'thumbnail': r're:^https?:/(/[a-z0-9.-]+)+\.jpg\?w=1920&h=1080$',
  126. 'upload_date': '20210615',
  127. 'timestamp': 1623722008,
  128. }
  129. }]
  130. def _real_extract(self, url):
  131. return self._extract_episode_info(url)
  132. class NhkVodProgramIE(NhkBaseIE):
  133. _VALID_URL = r'%s/program%s(?P<id>[0-9a-z]+)(?:.+?\btype=(?P<episode_type>clip|(?:radio|tv)Episode))?' % (NhkBaseIE._BASE_URL_REGEX, NhkBaseIE._TYPE_REGEX)
  134. _TESTS = [{
  135. # video program episodes
  136. 'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/program/video/japanrailway',
  137. 'info_dict': {
  138. 'id': 'japanrailway',
  139. 'title': 'Japan Railway Journal',
  140. },
  141. 'playlist_mincount': 1,
  142. }, {
  143. # video program clips
  144. 'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/program/video/japanrailway/?type=clip',
  145. 'info_dict': {
  146. 'id': 'japanrailway',
  147. 'title': 'Japan Railway Journal',
  148. },
  149. 'playlist_mincount': 5,
  150. }, {
  151. 'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/program/video/10yearshayaomiyazaki/',
  152. 'only_matching': True,
  153. }, {
  154. # audio program
  155. 'url': 'https://www3.nhk.or.jp/nhkworld/en/ondemand/program/audio/listener/',
  156. 'only_matching': True,
  157. }]
  158. def _real_extract(self, url):
  159. lang, m_type, program_id, episode_type = re.match(self._VALID_URL, url).groups()
  160. episodes = self._call_api(
  161. program_id, lang, m_type == 'video', False, episode_type == 'clip')
  162. entries = []
  163. for episode in episodes:
  164. episode_path = episode.get('url')
  165. if not episode_path:
  166. continue
  167. entries.append(self._extract_episode_info(
  168. urljoin(url, episode_path), episode))
  169. program_title = None
  170. if entries:
  171. program_title = entries[0].get('series')
  172. return self.playlist_result(entries, program_id, program_title)