litv.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import json
  2. from .common import InfoExtractor
  3. from ..utils import (
  4. ExtractorError,
  5. int_or_none,
  6. smuggle_url,
  7. traverse_obj,
  8. try_call,
  9. unsmuggle_url,
  10. )
  11. class LiTVIE(InfoExtractor):
  12. _VALID_URL = r'https?://(?:www\.)?litv\.tv/(?:vod|promo)/[^/]+/(?:content\.do)?\?.*?\b(?:content_)?id=(?P<id>[^&]+)'
  13. _URL_TEMPLATE = 'https://www.litv.tv/vod/%s/content.do?content_id=%s'
  14. _TESTS = [{
  15. 'url': 'https://www.litv.tv/vod/drama/content.do?brc_id=root&id=VOD00041610&isUHEnabled=true&autoPlay=1',
  16. 'info_dict': {
  17. 'id': 'VOD00041606',
  18. 'title': '花千骨',
  19. },
  20. 'playlist_count': 51, # 50 episodes + 1 trailer
  21. }, {
  22. 'url': 'https://www.litv.tv/vod/drama/content.do?brc_id=root&id=VOD00041610&isUHEnabled=true&autoPlay=1',
  23. 'md5': 'b90ff1e9f1d8f5cfcd0a44c3e2b34c7a',
  24. 'info_dict': {
  25. 'id': 'VOD00041610',
  26. 'ext': 'mp4',
  27. 'title': '花千骨第1集',
  28. 'thumbnail': r're:https?://.*\.jpg$',
  29. 'description': '《花千骨》陸劇線上看。十六年前,平靜的村莊內,一名女嬰隨異相出生,途徑此地的蜀山掌門清虛道長算出此女命運非同一般,她體內散發的異香易招惹妖魔。一念慈悲下,他在村莊周邊設下結界阻擋妖魔入侵,讓其年滿十六後去蜀山,並賜名花千骨。',
  30. 'categories': ['奇幻', '愛情', '中國', '仙俠'],
  31. 'episode': 'Episode 1',
  32. 'episode_number': 1,
  33. },
  34. 'params': {
  35. 'noplaylist': True,
  36. },
  37. 'skip': 'Georestricted to Taiwan',
  38. }, {
  39. 'url': 'https://www.litv.tv/promo/miyuezhuan/?content_id=VOD00044841&',
  40. 'md5': '88322ea132f848d6e3e18b32a832b918',
  41. 'info_dict': {
  42. 'id': 'VOD00044841',
  43. 'ext': 'mp4',
  44. 'title': '芈月傳第1集 霸星芈月降世楚國',
  45. 'description': '楚威王二年,太史令唐昧夜觀星象,發現霸星即將現世。王后得知霸星的預言後,想盡辦法不讓孩子順利出生,幸得莒姬相護化解危機。沒想到眾人期待下出生的霸星卻是位公主,楚威王對此失望至極。楚王后命人將女嬰丟棄河中,居然奇蹟似的被少司命像攔下,楚威王認為此女非同凡響,為她取名芈月。',
  46. },
  47. 'skip': 'No longer exists',
  48. }]
  49. def _extract_playlist(self, playlist_data, content_type):
  50. all_episodes = [
  51. self.url_result(smuggle_url(
  52. self._URL_TEMPLATE % (content_type, episode['contentId']),
  53. {'force_noplaylist': True})) # To prevent infinite recursion
  54. for episode in traverse_obj(playlist_data, ('seasons', ..., 'episode', lambda _, v: v['contentId']))]
  55. return self.playlist_result(all_episodes, playlist_data['contentId'], playlist_data.get('title'))
  56. def _real_extract(self, url):
  57. url, smuggled_data = unsmuggle_url(url, {})
  58. video_id = self._match_id(url)
  59. webpage = self._download_webpage(url, video_id)
  60. if self._search_regex(
  61. r'(?i)<meta\s[^>]*http-equiv="refresh"\s[^>]*content="[0-9]+;\s*url=https://www\.litv\.tv/"',
  62. webpage, 'meta refresh redirect', default=False, group=0):
  63. raise ExtractorError('No such content found', expected=True)
  64. program_info = self._parse_json(self._search_regex(
  65. r'var\s+programInfo\s*=\s*([^;]+)', webpage, 'VOD data', default='{}'),
  66. video_id)
  67. # In browsers `getProgramInfo` request is always issued. Usually this
  68. # endpoint gives the same result as the data embedded in the webpage.
  69. # If, for some reason, there are no embedded data, we do an extra request.
  70. if 'assetId' not in program_info:
  71. program_info = self._download_json(
  72. 'https://www.litv.tv/vod/ajax/getProgramInfo', video_id,
  73. query={'contentId': video_id},
  74. headers={'Accept': 'application/json'})
  75. series_id = program_info['seriesId']
  76. if self._yes_playlist(series_id, video_id, smuggled_data):
  77. playlist_data = self._download_json(
  78. 'https://www.litv.tv/vod/ajax/getSeriesTree', video_id,
  79. query={'seriesId': series_id}, headers={'Accept': 'application/json'})
  80. return self._extract_playlist(playlist_data, program_info['contentType'])
  81. video_data = self._parse_json(self._search_regex(
  82. r'uiHlsUrl\s*=\s*testBackendData\(([^;]+)\);',
  83. webpage, 'video data', default='{}'), video_id)
  84. if not video_data:
  85. payload = {'assetId': program_info['assetId']}
  86. puid = try_call(lambda: self._get_cookies('https://www.litv.tv/')['PUID'].value)
  87. if puid:
  88. payload.update({
  89. 'type': 'auth',
  90. 'puid': puid,
  91. })
  92. endpoint = 'getUrl'
  93. else:
  94. payload.update({
  95. 'watchDevices': program_info['watchDevices'],
  96. 'contentType': program_info['contentType'],
  97. })
  98. endpoint = 'getMainUrlNoAuth'
  99. video_data = self._download_json(
  100. f'https://www.litv.tv/vod/ajax/{endpoint}', video_id,
  101. data=json.dumps(payload).encode(),
  102. headers={'Content-Type': 'application/json'})
  103. if not video_data.get('fullpath'):
  104. error_msg = video_data.get('errorMessage')
  105. if error_msg == 'vod.error.outsideregionerror':
  106. self.raise_geo_restricted('This video is available in Taiwan only')
  107. if error_msg:
  108. raise ExtractorError(f'{self.IE_NAME} said: {error_msg}', expected=True)
  109. raise ExtractorError(f'Unexpected result from {self.IE_NAME}')
  110. formats = self._extract_m3u8_formats(
  111. video_data['fullpath'], video_id, ext='mp4',
  112. entry_protocol='m3u8_native', m3u8_id='hls')
  113. for a_format in formats:
  114. # LiTV HLS segments doesn't like compressions
  115. a_format.setdefault('http_headers', {})['Accept-Encoding'] = 'identity'
  116. title = program_info['title'] + program_info.get('secondaryMark', '')
  117. description = program_info.get('description')
  118. thumbnail = program_info.get('imageFile')
  119. categories = [item['name'] for item in program_info.get('category', [])]
  120. episode = int_or_none(program_info.get('episode'))
  121. return {
  122. 'id': video_id,
  123. 'formats': formats,
  124. 'title': title,
  125. 'description': description,
  126. 'thumbnail': thumbnail,
  127. 'categories': categories,
  128. 'episode_number': episode,
  129. }