ondemandkorea.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import functools
  2. import re
  3. import uuid
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. ExtractorError,
  7. OnDemandPagedList,
  8. float_or_none,
  9. int_or_none,
  10. join_nonempty,
  11. parse_age_limit,
  12. parse_qs,
  13. str_or_none,
  14. unified_strdate,
  15. url_or_none,
  16. )
  17. from ..utils.traversal import traverse_obj
  18. class OnDemandKoreaIE(InfoExtractor):
  19. _VALID_URL = r'https?://(?:www\.)?ondemandkorea\.com/(?:en/)?player/vod/[a-z0-9-]+\?(?:[^#]+&)?contentId=(?P<id>\d+)'
  20. _GEO_COUNTRIES = ['US', 'CA']
  21. _TESTS = [{
  22. 'url': 'https://www.ondemandkorea.com/player/vod/ask-us-anything?contentId=686471',
  23. 'md5': 'e2ff77255d989e3135bde0c5889fbce8',
  24. 'info_dict': {
  25. 'id': '686471',
  26. 'ext': 'mp4',
  27. 'title': 'Ask Us Anything: Jung Sung-ho, Park Seul-gi, Kim Bo-min, Yang Seung-won',
  28. 'thumbnail': r're:^https?://.*\.(jpg|jpeg|png)',
  29. 'duration': 5486.955,
  30. 'release_date': '20220924',
  31. 'series': 'Ask Us Anything',
  32. 'series_id': '11790',
  33. 'episode_number': 351,
  34. 'episode': 'Jung Sung-ho, Park Seul-gi, Kim Bo-min, Yang Seung-won',
  35. },
  36. }, {
  37. 'url': 'https://www.ondemandkorea.com/player/vod/breakup-probation-a-week?contentId=1595796',
  38. 'md5': '57266c720006962be7ff415b24775caa',
  39. 'info_dict': {
  40. 'id': '1595796',
  41. 'ext': 'mp4',
  42. 'title': 'Breakup Probation, A Week: E08',
  43. 'thumbnail': r're:^https?://.*\.(jpg|jpeg|png)',
  44. 'duration': 1586.0,
  45. 'release_date': '20231001',
  46. 'series': 'Breakup Probation, A Week',
  47. 'series_id': '22912',
  48. 'episode_number': 8,
  49. 'episode': 'E08',
  50. },
  51. }, {
  52. 'url': 'https://www.ondemandkorea.com/player/vod/the-outlaws?contentId=369531',
  53. 'md5': 'fa5523b87aa1f6d74fc622a97f2b47cd',
  54. 'info_dict': {
  55. 'id': '369531',
  56. 'ext': 'mp4',
  57. 'release_date': '20220519',
  58. 'duration': 7267.0,
  59. 'title': 'The Outlaws: Main Movie',
  60. 'thumbnail': r're:^https?://.*\.(jpg|jpeg|png)',
  61. 'age_limit': 18,
  62. },
  63. }, {
  64. 'url': 'https://www.ondemandkorea.com/en/player/vod/capture-the-moment-how-is-that-possible?contentId=1605006',
  65. 'only_matching': True,
  66. }]
  67. def _real_extract(self, url):
  68. video_id = self._match_id(url)
  69. data = self._download_json(
  70. f'https://odkmedia.io/odx/api/v3/playback/{video_id}/', video_id, fatal=False,
  71. headers={'service-name': 'odk'}, query={'did': str(uuid.uuid4())}, expected_status=(403, 404))
  72. if not traverse_obj(data, ('result', {dict})):
  73. msg = traverse_obj(data, ('messages', '__default'), 'title', expected_type=str)
  74. raise ExtractorError(msg or 'Got empty response from playback API', expected=True)
  75. data = data['result']
  76. def try_geo_bypass(url):
  77. return traverse_obj(url, ({parse_qs}, 'stream_url', 0, {url_or_none})) or url
  78. formats = []
  79. for m3u8_url in traverse_obj(data, (('sources', 'manifest'), ..., 'url', {url_or_none}, {try_geo_bypass})):
  80. mod_url = re.sub(r'_720(p?)\.m3u8', r'_1080\1.m3u8', m3u8_url)
  81. if mod_url != m3u8_url:
  82. mod_format = self._extract_m3u8_formats(
  83. mod_url, video_id, note='Checking for higher quality format',
  84. errnote='No higher quality format found', fatal=False)
  85. if mod_format:
  86. formats.extend(mod_format)
  87. continue
  88. formats.extend(self._extract_m3u8_formats(m3u8_url, video_id, fatal=False))
  89. subtitles = {}
  90. for track in traverse_obj(data, ('text_tracks', lambda _, v: url_or_none(v['url']))):
  91. subtitles.setdefault(track.get('language', 'und'), []).append({
  92. 'url': track['url'],
  93. 'ext': track.get('codec'),
  94. 'name': track.get('label'),
  95. })
  96. def if_series(key=None):
  97. return lambda obj: obj[key] if key and obj['kind'] == 'series' else None
  98. return {
  99. 'id': video_id,
  100. 'title': join_nonempty(
  101. ('episode', 'program', 'title'),
  102. ('episode', 'title'), from_dict=data, delim=': '),
  103. **traverse_obj(data, {
  104. 'thumbnail': ('episode', 'images', 'thumbnail', {url_or_none}),
  105. 'release_date': ('episode', 'release_date', {lambda x: x.replace('-', '')}, {unified_strdate}),
  106. 'duration': ('duration', {functools.partial(float_or_none, scale=1000)}),
  107. 'age_limit': ('age_rating', 'name', {lambda x: x.replace('R', '')}, {parse_age_limit}),
  108. 'series': ('episode', {if_series(key='program')}, 'title'),
  109. 'series_id': ('episode', {if_series(key='program')}, 'id', {str_or_none}),
  110. 'episode': ('episode', {if_series(key='title')}),
  111. 'episode_number': ('episode', {if_series(key='number')}, {int_or_none}),
  112. }, get_all=False),
  113. 'formats': formats,
  114. 'subtitles': subtitles,
  115. }
  116. class OnDemandKoreaProgramIE(InfoExtractor):
  117. _VALID_URL = r'https?://(?:www\.)?ondemandkorea\.com/(?:en/)?player/vod/(?P<id>[a-z0-9-]+)(?:$|#)'
  118. _GEO_COUNTRIES = ['US', 'CA']
  119. _TESTS = [{
  120. 'url': 'https://www.ondemandkorea.com/player/vod/uskn-news',
  121. 'info_dict': {
  122. 'id': 'uskn-news',
  123. },
  124. 'playlist_mincount': 755,
  125. }, {
  126. 'url': 'https://www.ondemandkorea.com/en/player/vod/the-land',
  127. 'info_dict': {
  128. 'id': 'the-land',
  129. },
  130. 'playlist_count': 52,
  131. }]
  132. _PAGE_SIZE = 100
  133. def _fetch_page(self, display_id, page):
  134. page += 1
  135. page_data = self._download_json(
  136. f'https://odkmedia.io/odx/api/v3/program/{display_id}/episodes/', display_id,
  137. headers={'service-name': 'odk'}, query={
  138. 'page': page,
  139. 'page_size': self._PAGE_SIZE,
  140. }, note=f'Downloading page {page}', expected_status=404)
  141. for episode in traverse_obj(page_data, ('result', 'results', ...)):
  142. yield self.url_result(
  143. f'https://www.ondemandkorea.com/player/vod/{display_id}?contentId={episode["id"]}',
  144. ie=OnDemandKoreaIE, video_title=episode.get('title'))
  145. def _real_extract(self, url):
  146. display_id = self._match_id(url)
  147. entries = OnDemandPagedList(functools.partial(
  148. self._fetch_page, display_id), self._PAGE_SIZE)
  149. return self.playlist_result(entries, display_id)