plutotv.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import re
  2. import urllib.parse
  3. import uuid
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. ExtractorError,
  7. float_or_none,
  8. int_or_none,
  9. try_get,
  10. url_or_none,
  11. )
  12. class PlutoTVIE(InfoExtractor):
  13. _WORKING = False
  14. _VALID_URL = r'''(?x)
  15. https?://(?:www\.)?pluto\.tv(?:/[^/]+)?/on-demand
  16. /(?P<video_type>movies|series)
  17. /(?P<series_or_movie_slug>[^/]+)
  18. (?:
  19. (?:/seasons?/(?P<season_no>\d+))?
  20. (?:/episode/(?P<episode_slug>[^/]+))?
  21. )?
  22. /?(?:$|[#?])'''
  23. _INFO_URL = 'https://service-vod.clusters.pluto.tv/v3/vod/slugs/'
  24. _INFO_QUERY_PARAMS = {
  25. 'appName': 'web',
  26. 'appVersion': 'na',
  27. 'clientID': str(uuid.uuid1()),
  28. 'clientModelNumber': 'na',
  29. 'serverSideAds': 'false',
  30. 'deviceMake': 'unknown',
  31. 'deviceModel': 'web',
  32. 'deviceType': 'web',
  33. 'deviceVersion': 'unknown',
  34. 'sid': str(uuid.uuid1()),
  35. }
  36. _TESTS = [
  37. {
  38. 'url': 'https://pluto.tv/on-demand/series/i-love-money/season/2/episode/its-in-the-cards-2009-2-3',
  39. 'md5': 'ebcdd8ed89aaace9df37924f722fd9bd',
  40. 'info_dict': {
  41. 'id': '5de6c598e9379ae4912df0a8',
  42. 'ext': 'mp4',
  43. 'title': 'It\'s In The Cards',
  44. 'episode': 'It\'s In The Cards',
  45. 'description': 'The teams face off against each other in a 3-on-2 soccer showdown. Strategy comes into play, though, as each team gets to select their opposing teams’ two defenders.',
  46. 'series': 'I Love Money',
  47. 'season_number': 2,
  48. 'episode_number': 3,
  49. 'duration': 3600,
  50. },
  51. }, {
  52. 'url': 'https://pluto.tv/on-demand/series/i-love-money/season/1/',
  53. 'playlist_count': 11,
  54. 'info_dict': {
  55. 'id': '5de6c582e9379ae4912dedbd',
  56. 'title': 'I Love Money - Season 1',
  57. },
  58. }, {
  59. 'url': 'https://pluto.tv/on-demand/series/i-love-money/',
  60. 'playlist_count': 26,
  61. 'info_dict': {
  62. 'id': '5de6c582e9379ae4912dedbd',
  63. 'title': 'I Love Money',
  64. },
  65. }, {
  66. 'url': 'https://pluto.tv/on-demand/movies/arrival-2015-1-1',
  67. 'md5': '3cead001d317a018bf856a896dee1762',
  68. 'info_dict': {
  69. 'id': '5e83ac701fa6a9001bb9df24',
  70. 'ext': 'mp4',
  71. 'title': 'Arrival',
  72. 'description': 'When mysterious spacecraft touch down across the globe, an elite team - led by expert translator Louise Banks (Academy Award® nominee Amy Adams) – races against time to decipher their intent.',
  73. 'duration': 9000,
  74. },
  75. }, {
  76. 'url': 'https://pluto.tv/en/on-demand/series/manhunters-fugitive-task-force/seasons/1/episode/third-times-the-charm-1-1',
  77. 'only_matching': True,
  78. }, {
  79. 'url': 'https://pluto.tv/it/on-demand/series/csi-vegas/episode/legacy-2021-1-1',
  80. 'only_matching': True,
  81. },
  82. {
  83. 'url': 'https://pluto.tv/en/on-demand/movies/attack-of-the-killer-tomatoes-1977-1-1-ptv1',
  84. 'md5': '7db56369c0da626a32d505ec6eb3f89f',
  85. 'info_dict': {
  86. 'id': '5b190c7bb0875c36c90c29c4',
  87. 'ext': 'mp4',
  88. 'title': 'Attack of the Killer Tomatoes',
  89. 'description': 'A group of scientists band together to save the world from mutated tomatoes that KILL! (1978)',
  90. 'duration': 5700,
  91. },
  92. },
  93. ]
  94. def _to_ad_free_formats(self, video_id, formats, subtitles):
  95. ad_free_formats, ad_free_subtitles, m3u8_urls = [], {}, set()
  96. for fmt in formats:
  97. res = self._download_webpage(
  98. fmt.get('url'), video_id, note='Downloading m3u8 playlist',
  99. fatal=False)
  100. if not res:
  101. continue
  102. first_segment_url = re.search(
  103. r'^(https?://.*/)0\-(end|[0-9]+)/[^/]+\.ts$', res,
  104. re.MULTILINE)
  105. if first_segment_url:
  106. m3u8_urls.add(
  107. urllib.parse.urljoin(first_segment_url.group(1), '0-end/master.m3u8'))
  108. continue
  109. first_segment_url = re.search(
  110. r'^(https?://.*/).+\-0+[0-1]0\.ts$', res,
  111. re.MULTILINE)
  112. if first_segment_url:
  113. m3u8_urls.add(
  114. urllib.parse.urljoin(first_segment_url.group(1), 'master.m3u8'))
  115. continue
  116. for m3u8_url in m3u8_urls:
  117. fmts, subs = self._extract_m3u8_formats_and_subtitles(
  118. m3u8_url, video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False)
  119. ad_free_formats.extend(fmts)
  120. ad_free_subtitles = self._merge_subtitles(ad_free_subtitles, subs)
  121. if ad_free_formats:
  122. formats, subtitles = ad_free_formats, ad_free_subtitles
  123. else:
  124. self.report_warning('Unable to find ad-free formats')
  125. return formats, subtitles
  126. def _get_video_info(self, video_json, slug, series_name=None):
  127. video_id = video_json.get('_id', slug)
  128. formats, subtitles = [], {}
  129. for video_url in try_get(video_json, lambda x: x['stitched']['urls'], list) or []:
  130. if video_url.get('type') != 'hls':
  131. continue
  132. url = url_or_none(video_url.get('url'))
  133. fmts, subs = self._extract_m3u8_formats_and_subtitles(
  134. url, video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False)
  135. formats.extend(fmts)
  136. subtitles = self._merge_subtitles(subtitles, subs)
  137. formats, subtitles = self._to_ad_free_formats(video_id, formats, subtitles)
  138. info = {
  139. 'id': video_id,
  140. 'formats': formats,
  141. 'subtitles': subtitles,
  142. 'title': video_json.get('name'),
  143. 'description': video_json.get('description'),
  144. 'duration': float_or_none(video_json.get('duration'), scale=1000),
  145. }
  146. if series_name:
  147. info.update({
  148. 'series': series_name,
  149. 'episode': video_json.get('name'),
  150. 'season_number': int_or_none(video_json.get('season')),
  151. 'episode_number': int_or_none(video_json.get('number')),
  152. })
  153. return info
  154. def _real_extract(self, url):
  155. mobj = self._match_valid_url(url).groupdict()
  156. info_slug = mobj['series_or_movie_slug']
  157. video_json = self._download_json(self._INFO_URL + info_slug, info_slug, query=self._INFO_QUERY_PARAMS)
  158. if mobj['video_type'] == 'series':
  159. series_name = video_json.get('name', info_slug)
  160. season_number, episode_slug = mobj.get('season_number'), mobj.get('episode_slug')
  161. videos = []
  162. for season in video_json['seasons']:
  163. if season_number is not None and season_number != int_or_none(season.get('number')):
  164. continue
  165. for episode in season['episodes']:
  166. if episode_slug is not None and episode_slug != episode.get('slug'):
  167. continue
  168. videos.append(self._get_video_info(episode, episode_slug, series_name))
  169. if not videos:
  170. raise ExtractorError('Failed to find any videos to extract')
  171. if episode_slug is not None and len(videos) == 1:
  172. return videos[0]
  173. playlist_title = series_name
  174. if season_number is not None:
  175. playlist_title += ' - Season %d' % season_number
  176. return self.playlist_result(videos,
  177. playlist_id=video_json.get('_id', info_slug),
  178. playlist_title=playlist_title)
  179. return self._get_video_info(video_json, info_slug)