asobichannel.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. from .common import InfoExtractor
  2. from ..utils import (
  3. ExtractorError,
  4. clean_html,
  5. merge_dicts,
  6. parse_iso8601,
  7. url_or_none,
  8. )
  9. from ..utils.traversal import traverse_obj
  10. class AsobiChannelBaseIE(InfoExtractor):
  11. _MICROCMS_HEADER = {'X-MICROCMS-API-KEY': 'qRaKehul9AHU8KtL0dnq1OCLKnFec6yrbcz3'}
  12. def _extract_info(self, metadata):
  13. return traverse_obj(metadata, {
  14. 'id': ('id', {str}),
  15. 'title': ('title', {str}),
  16. 'description': ('body', {clean_html}),
  17. 'thumbnail': ('contents', 'video_thumb', 'url', {url_or_none}),
  18. 'timestamp': ('publishedAt', {parse_iso8601}),
  19. 'modified_timestamp': ('updatedAt', {parse_iso8601}),
  20. 'channel': ('channel', 'name', {str}),
  21. 'channel_id': ('channel', 'id', {str}),
  22. })
  23. class AsobiChannelIE(AsobiChannelBaseIE):
  24. IE_NAME = 'asobichannel'
  25. IE_DESC = 'ASOBI CHANNEL'
  26. _VALID_URL = r'https?://asobichannel\.asobistore\.jp/watch/(?P<id>[\w-]+)'
  27. _TESTS = [{
  28. 'url': 'https://asobichannel.asobistore.jp/watch/1ypp48qd32p',
  29. 'md5': '39df74e872afe032c4eb27b89144fc92',
  30. 'info_dict': {
  31. 'id': '1ypp48qd32p',
  32. 'ext': 'mp4',
  33. 'title': 'アイドルマスター ミリオンライブ! 765プロch 原っぱ通信 #1',
  34. 'description': 'md5:b930bd2199c9b2fd75951ce4aaa7efd2',
  35. 'thumbnail': 'https://images.microcms-assets.io/assets/d2420de4b9194e11beb164f99edb1f95/a8e6f84119f54eb9ab4ce16729239905/%E3%82%B5%E3%83%A0%E3%83%8D%20(1).png',
  36. 'timestamp': 1697098247,
  37. 'upload_date': '20231012',
  38. 'modified_timestamp': 1698381162,
  39. 'modified_date': '20231027',
  40. 'channel': 'アイドルマスター',
  41. 'channel_id': 'idolmaster',
  42. },
  43. }, {
  44. 'url': 'https://asobichannel.asobistore.jp/watch/redigiwnjzqj',
  45. 'md5': '229fa8fb5c591c75ce8c37a497f113f6',
  46. 'info_dict': {
  47. 'id': 'redigiwnjzqj',
  48. 'ext': 'mp4',
  49. 'title': '【おまけ放送】アイドルマスター ミリオンライブ! 765プロch 原っぱ通信 #1',
  50. 'description': 'md5:7d9cd35fb54425a6967822bd564ea2d9',
  51. 'thumbnail': 'https://images.microcms-assets.io/assets/d2420de4b9194e11beb164f99edb1f95/20e5c1d6184242eebc2512a5dec59bf0/P1_%E5%8E%9F%E3%81%A3%E3%81%B1%E3%82%B5%E3%83%A0%E3%83%8D.png',
  52. 'modified_timestamp': 1697797125,
  53. 'modified_date': '20231020',
  54. 'timestamp': 1697261769,
  55. 'upload_date': '20231014',
  56. 'channel': 'アイドルマスター',
  57. 'channel_id': 'idolmaster',
  58. },
  59. }]
  60. _survapi_header = None
  61. def _real_initialize(self):
  62. token = self._download_json(
  63. 'https://asobichannel-api.asobistore.jp/api/v1/vspf/token', None,
  64. note='Retrieving API token')
  65. self._survapi_header = {'Authorization': f'Bearer {token}'}
  66. def _process_vod(self, video_id, metadata):
  67. content_id = metadata['contents']['video_id']
  68. vod_data = self._download_json(
  69. f'https://survapi.channel.or.jp/proxy/v1/contents/{content_id}/get_by_cuid', video_id,
  70. headers=self._survapi_header, note='Downloading vod data')
  71. return {
  72. 'formats': self._extract_m3u8_formats(vod_data['ex_content']['streaming_url'], video_id),
  73. }
  74. def _process_live(self, video_id, metadata):
  75. content_id = metadata['contents']['video_id']
  76. event_data = self._download_json(
  77. f'https://survapi.channel.or.jp/ex/events/{content_id}?embed=channel', video_id,
  78. headers=self._survapi_header, note='Downloading event data')
  79. player_type = traverse_obj(event_data, ('data', 'Player_type', {str}))
  80. if player_type == 'poster':
  81. self.raise_no_formats('Live event has not yet started', expected=True)
  82. live_status = 'is_upcoming'
  83. formats = []
  84. elif player_type == 'player':
  85. live_status = 'is_live'
  86. formats = self._extract_m3u8_formats(
  87. event_data['data']['Channel']['Custom_live_url'], video_id, live=True)
  88. else:
  89. raise ExtractorError('Unsupported player type {player_type!r}')
  90. return {
  91. 'release_timestamp': traverse_obj(metadata, ('period', 'start', {parse_iso8601})),
  92. 'live_status': live_status,
  93. 'formats': formats,
  94. }
  95. def _real_extract(self, url):
  96. video_id = self._match_id(url)
  97. metadata = self._download_json(
  98. f'https://channel.microcms.io/api/v1/media/{video_id}', video_id,
  99. headers=self._MICROCMS_HEADER)
  100. info = self._extract_info(metadata)
  101. video_type = traverse_obj(metadata, ('contents', 'video_type', 0, {str}))
  102. if video_type == 'VOD':
  103. return merge_dicts(info, self._process_vod(video_id, metadata))
  104. if video_type == 'LIVE':
  105. return merge_dicts(info, self._process_live(video_id, metadata))
  106. raise ExtractorError(f'Unexpected video type {video_type!r}')
  107. class AsobiChannelTagURLIE(AsobiChannelBaseIE):
  108. IE_NAME = 'asobichannel:tag'
  109. IE_DESC = 'ASOBI CHANNEL'
  110. _VALID_URL = r'https?://asobichannel\.asobistore\.jp/tag/(?P<id>[a-z0-9-_]+)'
  111. _TESTS = [{
  112. 'url': 'https://asobichannel.asobistore.jp/tag/bjhh-nbcja',
  113. 'info_dict': {
  114. 'id': 'bjhh-nbcja',
  115. 'title': 'アイドルマスター ミリオンライブ! 765プロch 原っぱ通信',
  116. },
  117. 'playlist_mincount': 16,
  118. }, {
  119. 'url': 'https://asobichannel.asobistore.jp/tag/hvm5qw3c6od',
  120. 'info_dict': {
  121. 'id': 'hvm5qw3c6od',
  122. 'title': 'アイマスMOIW2023ラジオ',
  123. },
  124. 'playlist_mincount': 13,
  125. }]
  126. def _real_extract(self, url):
  127. tag_id = self._match_id(url)
  128. webpage = self._download_webpage(url, tag_id)
  129. title = traverse_obj(self._search_nextjs_data(
  130. webpage, tag_id, fatal=False), ('props', 'pageProps', 'data', 'name', {str}))
  131. media = self._download_json(
  132. f'https://channel.microcms.io/api/v1/media?limit=999&filters=(tag[contains]{tag_id})',
  133. tag_id, headers=self._MICROCMS_HEADER)
  134. def entries():
  135. for metadata in traverse_obj(media, ('contents', lambda _, v: v['id'])):
  136. yield {
  137. '_type': 'url',
  138. 'url': f'https://asobichannel.asobistore.jp/watch/{metadata["id"]}',
  139. 'ie_key': AsobiChannelIE.ie_key(),
  140. **self._extract_info(metadata),
  141. }
  142. return self.playlist_result(entries(), tag_id, title)