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.

137 lines
5.1 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/[^/]+/content\.do\?.*?\bid=(?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. 'info_dict': {
  25. 'id': 'VOD00041610',
  26. 'ext': 'mp4',
  27. 'title': '花千骨第1集',
  28. 'thumbnail': 're:https?://.*\.jpg$',
  29. 'description': 'md5:c7017aa144c87467c4fb2909c4b05d6f',
  30. 'episode_number': 1,
  31. },
  32. 'params': {
  33. 'noplaylist': True,
  34. 'skip_download': True, # m3u8 download
  35. },
  36. 'skip': 'Georestricted to Taiwan',
  37. }]
  38. def _extract_playlist(self, season_list, video_id, vod_data, view_data, prompt=True):
  39. episode_title = view_data['title']
  40. content_id = season_list['contentId']
  41. if prompt:
  42. self.to_screen('Downloading playlist %s - add --no-playlist to just download video %s' % (content_id, video_id))
  43. all_episodes = [
  44. self.url_result(smuggle_url(
  45. self._URL_TEMPLATE % (view_data['contentType'], episode['contentId']),
  46. {'force_noplaylist': True})) # To prevent infinite recursion
  47. for episode in season_list['episode']]
  48. return self.playlist_result(all_episodes, content_id, episode_title)
  49. def _real_extract(self, url):
  50. url, data = unsmuggle_url(url, {})
  51. video_id = self._match_id(url)
  52. noplaylist = self._downloader.params.get('noplaylist')
  53. noplaylist_prompt = True
  54. if 'force_noplaylist' in data:
  55. noplaylist = data['force_noplaylist']
  56. noplaylist_prompt = False
  57. webpage = self._download_webpage(url, video_id)
  58. view_data = dict(map(lambda t: (t[0], t[2]), re.findall(
  59. r'viewData\.([a-zA-Z]+)\s*=\s*(["\'])([^"\']+)\2',
  60. webpage)))
  61. vod_data = self._parse_json(self._search_regex(
  62. 'var\s+vod\s*=\s*([^;]+)', webpage, 'VOD data', default='{}'),
  63. video_id)
  64. season_list = list(vod_data.get('seasonList', {}).values())
  65. if season_list:
  66. if not noplaylist:
  67. return self._extract_playlist(
  68. season_list[0], video_id, vod_data, view_data,
  69. prompt=noplaylist_prompt)
  70. if noplaylist_prompt:
  71. self.to_screen('Downloading just video %s because of --no-playlist' % video_id)
  72. # In browsers `getMainUrl` request is always issued. Usually this
  73. # endpoint gives the same result as the data embedded in the webpage.
  74. # If georestricted, there are no embedded data, so an extra request is
  75. # necessary to get the error code
  76. video_data = self._parse_json(self._search_regex(
  77. r'uiHlsUrl\s*=\s*testBackendData\(([^;]+)\);',
  78. webpage, 'video data', default='{}'), video_id)
  79. if not video_data:
  80. payload = {
  81. 'assetId': view_data['assetId'],
  82. 'watchDevices': vod_data['watchDevices'],
  83. 'contentType': view_data['contentType'],
  84. }
  85. video_data = self._download_json(
  86. 'https://www.litv.tv/vod/getMainUrl', video_id,
  87. data=json.dumps(payload).encode('utf-8'),
  88. headers={'Content-Type': 'application/json'})
  89. if not video_data.get('fullpath'):
  90. error_msg = video_data.get('errorMessage')
  91. if error_msg == 'vod.error.outsideregionerror':
  92. self.raise_geo_restricted('This video is available in Taiwan only')
  93. if error_msg:
  94. raise ExtractorError('%s said: %s' % (self.IE_NAME, error_msg), expected=True)
  95. raise ExtractorError('Unexpected result from %s' % self.IE_NAME)
  96. formats = self._extract_m3u8_formats(
  97. video_data['fullpath'], video_id, ext='mp4', m3u8_id='hls')
  98. for a_format in formats:
  99. # LiTV HLS segments doesn't like compressions
  100. a_format.setdefault('http_headers', {})['Youtubedl-no-compression'] = True
  101. title = view_data['title'] + view_data.get('secondaryMark', '')
  102. description = view_data.get('description')
  103. thumbnail = view_data.get('imageFile')
  104. categories = [item['name'] for item in vod_data.get('category', [])]
  105. episode = int_or_none(view_data.get('episode'))
  106. return {
  107. 'id': video_id,
  108. 'formats': formats,
  109. 'title': title,
  110. 'description': description,
  111. 'thumbnail': thumbnail,
  112. 'categories': categories,
  113. 'episode_number': episode,
  114. }