You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
6.1 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import json
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. ExtractorError,
  7. int_or_none,
  8. smuggle_url,
  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?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': 50,
  21. }, {
  22. 'url': 'https://www.litv.tv/vod/drama/content.do?brc_id=root&id=VOD00041610&isUHEnabled=true&autoPlay=1',
  23. 'md5': '969e343d9244778cb29acec608e53640',
  24. 'info_dict': {
  25. 'id': 'VOD00041610',
  26. 'ext': 'mp4',
  27. 'title': '花千骨第1集',
  28. 'thumbnail': r're:https?://.*\.jpg$',
  29. 'description': 'md5:c7017aa144c87467c4fb2909c4b05d6f',
  30. 'episode_number': 1,
  31. },
  32. 'params': {
  33. 'noplaylist': True,
  34. },
  35. 'skip': 'Georestricted to Taiwan',
  36. }, {
  37. 'url': 'https://www.litv.tv/promo/miyuezhuan/?content_id=VOD00044841&',
  38. 'md5': '88322ea132f848d6e3e18b32a832b918',
  39. 'info_dict': {
  40. 'id': 'VOD00044841',
  41. 'ext': 'mp4',
  42. 'title': '芈月傳第1集 霸星芈月降世楚國',
  43. 'description': '楚威王二年,太史令唐昧夜觀星象,發現霸星即將現世。王后得知霸星的預言後,想盡辦法不讓孩子順利出生,幸得莒姬相護化解危機。沒想到眾人期待下出生的霸星卻是位公主,楚威王對此失望至極。楚王后命人將女嬰丟棄河中,居然奇蹟似的被少司命像攔下,楚威王認為此女非同凡響,為她取名芈月。',
  44. },
  45. 'skip': 'Georestricted to Taiwan',
  46. }]
  47. def _extract_playlist(self, season_list, video_id, program_info, prompt=True):
  48. episode_title = program_info['title']
  49. content_id = season_list['contentId']
  50. if prompt:
  51. self.to_screen('Downloading playlist %s - add --no-playlist to just download video %s' % (content_id, video_id))
  52. all_episodes = [
  53. self.url_result(smuggle_url(
  54. self._URL_TEMPLATE % (program_info['contentType'], episode['contentId']),
  55. {'force_noplaylist': True})) # To prevent infinite recursion
  56. for episode in season_list['episode']]
  57. return self.playlist_result(all_episodes, content_id, episode_title)
  58. def _real_extract(self, url):
  59. url, data = unsmuggle_url(url, {})
  60. video_id = self._match_id(url)
  61. noplaylist = self._downloader.params.get('noplaylist')
  62. noplaylist_prompt = True
  63. if 'force_noplaylist' in data:
  64. noplaylist = data['force_noplaylist']
  65. noplaylist_prompt = False
  66. webpage = self._download_webpage(url, video_id)
  67. program_info = self._parse_json(self._search_regex(
  68. r'var\s+programInfo\s*=\s*([^;]+)', webpage, 'VOD data', default='{}'),
  69. video_id)
  70. season_list = list(program_info.get('seasonList', {}).values())
  71. if season_list:
  72. if not noplaylist:
  73. return self._extract_playlist(
  74. season_list[0], video_id, program_info,
  75. prompt=noplaylist_prompt)
  76. if noplaylist_prompt:
  77. self.to_screen('Downloading just video %s because of --no-playlist' % video_id)
  78. # In browsers `getMainUrl` request is always issued. Usually this
  79. # endpoint gives the same result as the data embedded in the webpage.
  80. # If georestricted, there are no embedded data, so an extra request is
  81. # necessary to get the error code
  82. if 'assetId' not in program_info:
  83. program_info = self._download_json(
  84. 'https://www.litv.tv/vod/ajax/getProgramInfo', video_id,
  85. query={'contentId': video_id},
  86. headers={'Accept': 'application/json'})
  87. video_data = self._parse_json(self._search_regex(
  88. r'uiHlsUrl\s*=\s*testBackendData\(([^;]+)\);',
  89. webpage, 'video data', default='{}'), video_id)
  90. if not video_data:
  91. payload = {
  92. 'assetId': program_info['assetId'],
  93. 'watchDevices': program_info['watchDevices'],
  94. 'contentType': program_info['contentType'],
  95. }
  96. video_data = self._download_json(
  97. 'https://www.litv.tv/vod/getMainUrl', video_id,
  98. data=json.dumps(payload).encode('utf-8'),
  99. headers={'Content-Type': 'application/json'})
  100. if not video_data.get('fullpath'):
  101. error_msg = video_data.get('errorMessage')
  102. if error_msg == 'vod.error.outsideregionerror':
  103. self.raise_geo_restricted('This video is available in Taiwan only')
  104. if error_msg:
  105. raise ExtractorError('%s said: %s' % (self.IE_NAME, error_msg), expected=True)
  106. raise ExtractorError('Unexpected result from %s' % self.IE_NAME)
  107. formats = self._extract_m3u8_formats(
  108. video_data['fullpath'], video_id, ext='mp4',
  109. entry_protocol='m3u8_native', m3u8_id='hls')
  110. for a_format in formats:
  111. # LiTV HLS segments doesn't like compressions
  112. a_format.setdefault('http_headers', {})['Youtubedl-no-compression'] = True
  113. title = program_info['title'] + program_info.get('secondaryMark', '')
  114. description = program_info.get('description')
  115. thumbnail = program_info.get('imageFile')
  116. categories = [item['name'] for item in program_info.get('category', [])]
  117. episode = int_or_none(program_info.get('episode'))
  118. return {
  119. 'id': video_id,
  120. 'formats': formats,
  121. 'title': title,
  122. 'description': description,
  123. 'thumbnail': thumbnail,
  124. 'categories': categories,
  125. 'episode_number': episode,
  126. }