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.

153 lines
6.2 KiB

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