tbsjp.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. from .common import InfoExtractor
  2. from ..networking.exceptions import HTTPError
  3. from ..utils import (
  4. ExtractorError,
  5. clean_html,
  6. get_element_text_and_html_by_tag,
  7. int_or_none,
  8. str_or_none,
  9. traverse_obj,
  10. try_call,
  11. unified_timestamp,
  12. urljoin,
  13. )
  14. class TBSJPEpisodeIE(InfoExtractor):
  15. _VALID_URL = r'https?://cu\.tbs\.co\.jp/episode/(?P<id>[\d_]+)'
  16. _GEO_BYPASS = False
  17. _TESTS = [{
  18. 'url': 'https://cu.tbs.co.jp/episode/23613_2044134_1000049010',
  19. 'skip': 'streams geo-restricted, Japan only. Also, will likely expire eventually',
  20. 'info_dict': {
  21. 'title': 'VIVANT 第三話 誤送金完結へ!絶体絶命の反撃開始',
  22. 'id': '23613_2044134_1000049010',
  23. 'ext': 'mp4',
  24. 'upload_date': '20230728',
  25. 'duration': 3517,
  26. 'release_timestamp': 1691118230,
  27. 'episode': '第三話 誤送金完結へ!絶体絶命の反撃開始',
  28. 'release_date': '20230804',
  29. 'categories': 'count:11',
  30. 'episode_number': 3,
  31. 'timestamp': 1690522538,
  32. 'description': 'md5:2b796341af1ef772034133174ba4a895',
  33. 'series': 'VIVANT',
  34. },
  35. }]
  36. def _real_extract(self, url):
  37. video_id = self._match_id(url)
  38. webpage = self._download_webpage(url, video_id)
  39. meta = self._search_json(r'window\.app\s*=', webpage, 'episode info', video_id, fatal=False)
  40. episode = traverse_obj(meta, ('falcorCache', 'catalog', 'episode', video_id, 'value'))
  41. tf_path = self._search_regex(
  42. r'<script[^>]+src=["\'](/assets/tf\.[^"\']+\.js)["\']', webpage, 'stream API config')
  43. tf_js = self._download_webpage(urljoin(url, tf_path), video_id, note='Downloading stream API config')
  44. video_url = self._search_regex(r'videoPlaybackUrl:\s*[\'"]([^\'"]+)[\'"]', tf_js, 'stream API url')
  45. api_key = self._search_regex(r'api_key:\s*[\'"]([^\'"]+)[\'"]', tf_js, 'stream API key')
  46. try:
  47. source_meta = self._download_json(f'{video_url}ref:{video_id}', video_id,
  48. headers={'X-Streaks-Api-Key': api_key},
  49. note='Downloading stream metadata')
  50. except ExtractorError as e:
  51. if isinstance(e.cause, HTTPError) and e.cause.status == 403:
  52. self.raise_geo_restricted(countries=['JP'])
  53. raise
  54. formats, subtitles = [], {}
  55. for src in traverse_obj(source_meta, ('sources', ..., 'src')):
  56. fmts, subs = self._extract_m3u8_formats_and_subtitles(src, video_id, fatal=False)
  57. formats.extend(fmts)
  58. self._merge_subtitles(subs, target=subtitles)
  59. return {
  60. 'title': try_call(lambda: clean_html(get_element_text_and_html_by_tag('h3', webpage)[0])),
  61. 'id': video_id,
  62. **traverse_obj(episode, {
  63. 'categories': ('keywords', {list}),
  64. 'id': ('content_id', {str}),
  65. 'description': ('description', 0, 'value'),
  66. 'timestamp': ('created_at', {unified_timestamp}),
  67. 'release_timestamp': ('pub_date', {unified_timestamp}),
  68. 'duration': ('tv_episode_info', 'duration', {int_or_none}),
  69. 'episode_number': ('tv_episode_info', 'episode_number', {int_or_none}),
  70. 'episode': ('title', lambda _, v: not v.get('is_phonetic'), 'value'),
  71. 'series': ('custom_data', 'program_name'),
  72. }, get_all=False),
  73. 'formats': formats,
  74. 'subtitles': subtitles,
  75. }
  76. class TBSJPProgramIE(InfoExtractor):
  77. _VALID_URL = r'https?://cu\.tbs\.co\.jp/program/(?P<id>\d+)'
  78. _TESTS = [{
  79. 'url': 'https://cu.tbs.co.jp/program/23601',
  80. 'playlist_mincount': 4,
  81. 'info_dict': {
  82. 'id': '23601',
  83. 'categories': ['エンタメ', 'ミライカプセル', '会社', '働く', 'バラエティ', '動画'],
  84. 'description': '幼少期の夢は大人になって、どう成長したのだろうか?\nそしてその夢は今後、どのように広がっていくのか?\nいま話題の会社で働く人の「夢の成長」を描く',
  85. 'series': 'ミライカプセル -I have a dream-',
  86. 'title': 'ミライカプセル -I have a dream-',
  87. },
  88. }]
  89. def _real_extract(self, url):
  90. programme_id = self._match_id(url)
  91. webpage = self._download_webpage(url, programme_id)
  92. meta = self._search_json(r'window\.app\s*=', webpage, 'programme info', programme_id)
  93. programme = traverse_obj(meta, ('falcorCache', 'catalog', 'program', programme_id, 'false', 'value'))
  94. return {
  95. '_type': 'playlist',
  96. 'entries': [self.url_result(f'https://cu.tbs.co.jp/episode/{video_id}', TBSJPEpisodeIE, video_id)
  97. for video_id in traverse_obj(programme, ('custom_data', 'seriesList', 'episodeCode', ...))],
  98. 'id': programme_id,
  99. **traverse_obj(programme, {
  100. 'categories': ('keywords', ...),
  101. 'id': ('tv_episode_info', 'show_content_id', {str_or_none}),
  102. 'description': ('custom_data', 'program_description'),
  103. 'series': ('custom_data', 'program_name'),
  104. 'title': ('custom_data', 'program_name'),
  105. }),
  106. }
  107. class TBSJPPlaylistIE(InfoExtractor):
  108. _VALID_URL = r'https?://cu\.tbs\.co\.jp/playlist/(?P<id>[\da-f]+)'
  109. _TESTS = [{
  110. 'url': 'https://cu.tbs.co.jp/playlist/184f9970e7ba48e4915f1b252c55015e',
  111. 'playlist_mincount': 4,
  112. 'info_dict': {
  113. 'title': 'まもなく配信終了',
  114. 'id': '184f9970e7ba48e4915f1b252c55015e',
  115. },
  116. }]
  117. def _real_extract(self, url):
  118. playlist_id = self._match_id(url)
  119. page = self._download_webpage(url, playlist_id)
  120. meta = self._search_json(r'window\.app\s*=', page, 'playlist info', playlist_id)
  121. playlist = traverse_obj(meta, ('falcorCache', 'playList', playlist_id))
  122. def entries():
  123. for entry in traverse_obj(playlist, ('catalogs', 'value', lambda _, v: v['content_id'])):
  124. # TODO: it's likely possible to get all metadata from the playlist page json instead
  125. content_id = entry['content_id']
  126. content_type = entry.get('content_type')
  127. if content_type == 'tv_show':
  128. yield self.url_result(
  129. f'https://cu.tbs.co.jp/program/{content_id}', TBSJPProgramIE, content_id)
  130. elif content_type == 'tv_episode':
  131. yield self.url_result(
  132. f'https://cu.tbs.co.jp/episode/{content_id}', TBSJPEpisodeIE, content_id)
  133. else:
  134. self.report_warning(f'Skipping "{content_id}" with unsupported content_type "{content_type}"')
  135. return self.playlist_result(entries(), playlist_id, traverse_obj(playlist, ('display_name', 'value')))