openrec.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. from .common import InfoExtractor
  2. from ..utils import (
  3. ExtractorError,
  4. get_first,
  5. int_or_none,
  6. traverse_obj,
  7. try_get,
  8. unified_strdate,
  9. unified_timestamp,
  10. )
  11. from ..compat import compat_str
  12. class OpenRecBaseIE(InfoExtractor):
  13. def _extract_pagestore(self, webpage, video_id):
  14. return self._parse_json(
  15. self._search_regex(r'(?m)window\.pageStore\s*=\s*(\{.+?\});$', webpage, 'window.pageStore'), video_id)
  16. def _expand_media(self, video_id, media):
  17. for name, m3u8_url in (media or {}).items():
  18. if not m3u8_url:
  19. continue
  20. yield from self._extract_m3u8_formats(
  21. m3u8_url, video_id, ext='mp4', m3u8_id=name)
  22. def _extract_movie(self, webpage, video_id, name, is_live):
  23. window_stores = self._extract_pagestore(webpage, video_id)
  24. movie_stores = [
  25. # extract all three important data (most of data are duplicated each other, but slightly different!)
  26. traverse_obj(window_stores, ('v8', 'state', 'movie'), expected_type=dict),
  27. traverse_obj(window_stores, ('v8', 'movie'), expected_type=dict),
  28. traverse_obj(window_stores, 'movieStore', expected_type=dict),
  29. ]
  30. if not any(movie_stores):
  31. raise ExtractorError(f'Failed to extract {name} info')
  32. formats = list(self._expand_media(video_id, get_first(movie_stores, 'media')))
  33. if not formats:
  34. # archived livestreams or subscriber-only videos
  35. cookies = self._get_cookies('https://www.openrec.tv/')
  36. detail = self._download_json(
  37. f'https://apiv5.openrec.tv/api/v5/movies/{video_id}/detail', video_id,
  38. headers={
  39. 'Origin': 'https://www.openrec.tv',
  40. 'Referer': 'https://www.openrec.tv/',
  41. 'access-token': try_get(cookies, lambda x: x.get('access_token').value),
  42. 'uuid': try_get(cookies, lambda x: x.get('uuid').value),
  43. })
  44. new_media = traverse_obj(detail, ('data', 'items', ..., 'media'), get_all=False)
  45. formats = list(self._expand_media(video_id, new_media))
  46. is_live = False
  47. return {
  48. 'id': video_id,
  49. 'title': get_first(movie_stores, 'title'),
  50. 'description': get_first(movie_stores, 'introduction'),
  51. 'thumbnail': get_first(movie_stores, 'thumbnailUrl'),
  52. 'formats': formats,
  53. 'uploader': get_first(movie_stores, ('channel', 'user', 'name')),
  54. 'uploader_id': get_first(movie_stores, ('channel', 'user', 'id')),
  55. 'timestamp': int_or_none(get_first(movie_stores, ['publishedAt', 'time']), scale=1000) or unified_timestamp(get_first(movie_stores, 'publishedAt')),
  56. 'is_live': is_live,
  57. }
  58. class OpenRecIE(OpenRecBaseIE):
  59. IE_NAME = 'openrec'
  60. _VALID_URL = r'https?://(?:www\.)?openrec\.tv/live/(?P<id>[^/]+)'
  61. _TESTS = [{
  62. 'url': 'https://www.openrec.tv/live/2p8v31qe4zy',
  63. 'only_matching': True,
  64. }, {
  65. 'url': 'https://www.openrec.tv/live/wez93eqvjzl',
  66. 'only_matching': True,
  67. }]
  68. def _real_extract(self, url):
  69. video_id = self._match_id(url)
  70. webpage = self._download_webpage(f'https://www.openrec.tv/live/{video_id}', video_id)
  71. return self._extract_movie(webpage, video_id, 'live', True)
  72. class OpenRecCaptureIE(OpenRecBaseIE):
  73. IE_NAME = 'openrec:capture'
  74. _VALID_URL = r'https?://(?:www\.)?openrec\.tv/capture/(?P<id>[^/]+)'
  75. _TESTS = [{
  76. 'url': 'https://www.openrec.tv/capture/l9nk2x4gn14',
  77. 'only_matching': True,
  78. }, {
  79. 'url': 'https://www.openrec.tv/capture/mldjr82p7qk',
  80. 'info_dict': {
  81. 'id': 'mldjr82p7qk',
  82. 'title': 'たいじの恥ずかしい英語力',
  83. 'uploader': 'たいちゃんねる',
  84. 'uploader_id': 'Yaritaiji',
  85. 'upload_date': '20210803',
  86. },
  87. }]
  88. def _real_extract(self, url):
  89. video_id = self._match_id(url)
  90. webpage = self._download_webpage(f'https://www.openrec.tv/capture/{video_id}', video_id)
  91. window_stores = self._extract_pagestore(webpage, video_id)
  92. movie_store = window_stores.get('movie')
  93. capture_data = window_stores.get('capture')
  94. if not capture_data:
  95. raise ExtractorError('Cannot extract title')
  96. formats = self._extract_m3u8_formats(
  97. capture_data.get('source'), video_id, ext='mp4')
  98. return {
  99. 'id': video_id,
  100. 'title': capture_data.get('title'),
  101. 'thumbnail': capture_data.get('thumbnailUrl'),
  102. 'formats': formats,
  103. 'timestamp': unified_timestamp(traverse_obj(movie_store, 'createdAt', expected_type=compat_str)),
  104. 'uploader': traverse_obj(movie_store, ('channel', 'name'), expected_type=compat_str),
  105. 'uploader_id': traverse_obj(movie_store, ('channel', 'id'), expected_type=compat_str),
  106. 'upload_date': unified_strdate(capture_data.get('createdAt')),
  107. }
  108. class OpenRecMovieIE(OpenRecBaseIE):
  109. IE_NAME = 'openrec:movie'
  110. _VALID_URL = r'https?://(?:www\.)?openrec\.tv/movie/(?P<id>[^/]+)'
  111. _TESTS = [{
  112. 'url': 'https://www.openrec.tv/movie/nqz5xl5km8v',
  113. 'info_dict': {
  114. 'id': 'nqz5xl5km8v',
  115. 'title': '限定コミュニティ(Discord)参加方法ご説明動画',
  116. 'description': 'md5:ebd563e5f5b060cda2f02bf26b14d87f',
  117. 'thumbnail': r're:https://.+',
  118. 'uploader': 'タイキとカズヒロ',
  119. 'uploader_id': 'taiki_to_kazuhiro',
  120. 'timestamp': 1638856800,
  121. },
  122. }]
  123. def _real_extract(self, url):
  124. video_id = self._match_id(url)
  125. webpage = self._download_webpage(f'https://www.openrec.tv/movie/{video_id}', video_id)
  126. return self._extract_movie(webpage, video_id, 'movie', False)