tubitv.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import re
  2. from .common import InfoExtractor
  3. from ..networking import Request
  4. from ..utils import (
  5. ExtractorError,
  6. int_or_none,
  7. js_to_json,
  8. traverse_obj,
  9. url_or_none,
  10. urlencode_postdata,
  11. )
  12. class TubiTvIE(InfoExtractor):
  13. IE_NAME = 'tubitv'
  14. _VALID_URL = r'https?://(?:www\.)?tubitv\.com/(?P<type>video|movies|tv-shows)/(?P<id>\d+)'
  15. _LOGIN_URL = 'http://tubitv.com/login'
  16. _NETRC_MACHINE = 'tubitv'
  17. _TESTS = [{
  18. 'url': 'https://tubitv.com/movies/100004539/the-39-steps',
  19. 'info_dict': {
  20. 'id': '100004539',
  21. 'ext': 'mp4',
  22. 'title': 'The 39 Steps',
  23. 'description': 'md5:bb2f2dd337f0dc58c06cb509943f54c8',
  24. 'uploader_id': 'abc2558d54505d4f0f32be94f2e7108c',
  25. 'release_year': 1935,
  26. 'thumbnail': r're:^https?://.+\.(jpe?g|png)$',
  27. 'duration': 5187,
  28. },
  29. 'params': {'skip_download': 'm3u8'},
  30. }, {
  31. 'url': 'https://tubitv.com/tv-shows/554628/s01-e01-rise-of-the-snakes',
  32. 'info_dict': {
  33. 'id': '554628',
  34. 'ext': 'mp4',
  35. 'title': 'S01:E01 - Rise of the Snakes',
  36. 'description': 'md5:ba136f586de53af0372811e783a3f57d',
  37. 'episode': 'Rise of the Snakes',
  38. 'episode_number': 1,
  39. 'season': 'Season 1',
  40. 'season_number': 1,
  41. 'uploader_id': '2a9273e728c510d22aa5c57d0646810b',
  42. 'release_year': 2011,
  43. 'thumbnail': r're:^https?://.+\.(jpe?g|png)$',
  44. 'duration': 1376,
  45. },
  46. 'params': {'skip_download': 'm3u8'},
  47. }, {
  48. 'url': 'http://tubitv.com/video/283829/the_comedian_at_the_friday',
  49. 'md5': '43ac06be9326f41912dc64ccf7a80320',
  50. 'info_dict': {
  51. 'id': '283829',
  52. 'ext': 'mp4',
  53. 'title': 'The Comedian at The Friday',
  54. 'description': 'A stand up comedian is forced to look at the decisions in his life while on a one week trip to the west coast.',
  55. 'uploader_id': 'bc168bee0d18dd1cb3b86c68706ab434',
  56. },
  57. 'skip': 'Content Unavailable',
  58. }, {
  59. 'url': 'http://tubitv.com/tv-shows/321886/s01_e01_on_nom_stories',
  60. 'only_matching': True,
  61. }, {
  62. 'url': 'https://tubitv.com/movies/560057/penitentiary?start=true',
  63. 'info_dict': {
  64. 'id': '560057',
  65. 'ext': 'mp4',
  66. 'title': 'Penitentiary',
  67. 'description': 'md5:8d2fc793a93cc1575ff426fdcb8dd3f9',
  68. 'uploader_id': 'd8fed30d4f24fcb22ec294421b9defc2',
  69. 'release_year': 1979,
  70. },
  71. 'skip': 'Content Unavailable',
  72. }]
  73. # DRM formats are included only to raise appropriate error
  74. _UNPLAYABLE_FORMATS = ('hlsv6_widevine', 'hlsv6_widevine_nonclearlead', 'hlsv6_playready_psshv0',
  75. 'hlsv6_fairplay', 'dash_widevine', 'dash_widevine_nonclearlead')
  76. def _perform_login(self, username, password):
  77. self.report_login()
  78. form_data = {
  79. 'username': username,
  80. 'password': password,
  81. }
  82. payload = urlencode_postdata(form_data)
  83. request = Request(self._LOGIN_URL, payload)
  84. request.headers['Content-Type'] = 'application/x-www-form-urlencoded'
  85. login_page = self._download_webpage(
  86. request, None, False, 'Wrong login info')
  87. if not re.search(r'id="tubi-logout"', login_page):
  88. raise ExtractorError(
  89. 'Login failed (invalid username/password)', expected=True)
  90. def _real_extract(self, url):
  91. video_id, video_type = self._match_valid_url(url).group('id', 'type')
  92. webpage = self._download_webpage(f'https://tubitv.com/{video_type}/{video_id}/', video_id)
  93. video_data = self._search_json(
  94. r'window\.__data\s*=', webpage, 'data', video_id,
  95. transform_source=js_to_json)['video']['byId'][video_id]
  96. formats = []
  97. drm_formats = False
  98. for resource in traverse_obj(video_data, ('video_resources', lambda _, v: url_or_none(v['manifest']['url']))):
  99. resource_type = resource.get('type')
  100. manifest_url = resource['manifest']['url']
  101. if resource_type == 'dash':
  102. formats.extend(self._extract_mpd_formats(manifest_url, video_id, mpd_id=resource_type, fatal=False))
  103. elif resource_type in ('hlsv3', 'hlsv6'):
  104. formats.extend(self._extract_m3u8_formats(manifest_url, video_id, 'mp4', m3u8_id=resource_type, fatal=False))
  105. elif resource_type in self._UNPLAYABLE_FORMATS:
  106. drm_formats = True
  107. else:
  108. self.report_warning(f'Skipping unknown resource type "{resource_type}"')
  109. if not formats and drm_formats:
  110. self.report_drm(video_id)
  111. elif not formats and not video_data.get('policy_match'): # policy_match is False if content was removed
  112. raise ExtractorError('This content is currently unavailable', expected=True)
  113. subtitles = {}
  114. for sub in traverse_obj(video_data, ('subtitles', lambda _, v: url_or_none(v['url']))):
  115. subtitles.setdefault(sub.get('lang', 'English'), []).append({
  116. 'url': self._proto_relative_url(sub['url']),
  117. })
  118. title = traverse_obj(video_data, ('title', {str}))
  119. season_number, episode_number, episode_title = self._search_regex(
  120. r'^S(\d+):E(\d+) - (.+)', title, 'episode info', fatal=False, group=(1, 2, 3), default=(None, None, None))
  121. return {
  122. 'id': video_id,
  123. 'title': title,
  124. 'formats': formats,
  125. 'subtitles': subtitles,
  126. 'season_number': int_or_none(season_number),
  127. 'episode_number': int_or_none(episode_number),
  128. 'episode': episode_title,
  129. **traverse_obj(video_data, {
  130. 'description': ('description', {str}),
  131. 'duration': ('duration', {int_or_none}),
  132. 'uploader_id': ('publisher_id', {str}),
  133. 'release_year': ('year', {int_or_none}),
  134. 'thumbnails': ('thumbnails', ..., {url_or_none}, {'url': {self._proto_relative_url}}),
  135. }),
  136. }
  137. class TubiTvShowIE(InfoExtractor):
  138. IE_NAME = 'tubitv:series'
  139. _VALID_URL = r'https?://(?:www\.)?tubitv\.com/series/\d+/(?P<show_name>[^/?#]+)(?:/season-(?P<season>\d+))?'
  140. _TESTS = [{
  141. 'url': 'https://tubitv.com/series/3936/the-joy-of-painting-with-bob-ross?start=true',
  142. 'playlist_mincount': 389,
  143. 'info_dict': {
  144. 'id': 'the-joy-of-painting-with-bob-ross',
  145. },
  146. }, {
  147. 'url': 'https://tubitv.com/series/2311/the-saddle-club/season-1',
  148. 'playlist_count': 26,
  149. 'info_dict': {
  150. 'id': 'the-saddle-club-season-1',
  151. },
  152. }, {
  153. 'url': 'https://tubitv.com/series/2311/the-saddle-club/season-3',
  154. 'playlist_count': 19,
  155. 'info_dict': {
  156. 'id': 'the-saddle-club-season-3',
  157. },
  158. }, {
  159. 'url': 'https://tubitv.com/series/2311/the-saddle-club/',
  160. 'playlist_mincount': 71,
  161. 'info_dict': {
  162. 'id': 'the-saddle-club',
  163. },
  164. }]
  165. def _entries(self, show_url, playlist_id, selected_season):
  166. webpage = self._download_webpage(show_url, playlist_id)
  167. data = self._search_json(
  168. r'window\.__data\s*=', webpage, 'data', playlist_id,
  169. transform_source=js_to_json)['video']
  170. # v['number'] is already a decimal string, but stringify to protect against API changes
  171. path = [lambda _, v: str(v['number']) == selected_season] if selected_season else [..., {dict}]
  172. for season in traverse_obj(data, ('byId', lambda _, v: v['type'] == 's', 'seasons', *path)):
  173. season_number = int_or_none(season.get('number'))
  174. for episode in traverse_obj(season, ('episodes', lambda _, v: v['id'])):
  175. episode_id = episode['id']
  176. yield self.url_result(
  177. f'https://tubitv.com/tv-shows/{episode_id}/', TubiTvIE, episode_id,
  178. season_number=season_number, episode_number=int_or_none(episode.get('num')))
  179. def _real_extract(self, url):
  180. playlist_id, selected_season = self._match_valid_url(url).group('show_name', 'season')
  181. if selected_season:
  182. playlist_id = f'{playlist_id}-season-{selected_season}'
  183. return self.playlist_result(self._entries(url, playlist_id, selected_season), playlist_id)