tubitv.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import re
  2. from .common import InfoExtractor
  3. from ..utils import (
  4. ExtractorError,
  5. int_or_none,
  6. js_to_json,
  7. sanitized_Request,
  8. urlencode_postdata,
  9. traverse_obj,
  10. )
  11. class TubiTvIE(InfoExtractor):
  12. _VALID_URL = r'''(?x)
  13. (?:
  14. tubitv:|
  15. https?://(?:www\.)?tubitv\.com/(?:video|movies|tv-shows)/
  16. )
  17. (?P<id>[0-9]+)'''
  18. _LOGIN_URL = 'http://tubitv.com/login'
  19. _NETRC_MACHINE = 'tubitv'
  20. _GEO_COUNTRIES = ['US']
  21. _TESTS = [{
  22. 'url': 'https://tubitv.com/movies/383676/tracker',
  23. 'md5': '566fa0f76870302d11af0de89511d3f0',
  24. 'info_dict': {
  25. 'id': '383676',
  26. 'ext': 'mp4',
  27. 'title': 'Tracker',
  28. 'description': 'md5:ff320baf43d0ad2655e538c1d5cd9706',
  29. 'uploader_id': 'f866e2677ea2f0dff719788e4f7f9195',
  30. 'release_year': 2010,
  31. 'thumbnail': r're:^https?://.+\.(jpe?g|png)$',
  32. 'duration': 6122,
  33. },
  34. }, {
  35. 'url': 'http://tubitv.com/video/283829/the_comedian_at_the_friday',
  36. 'md5': '43ac06be9326f41912dc64ccf7a80320',
  37. 'info_dict': {
  38. 'id': '283829',
  39. 'ext': 'mp4',
  40. 'title': 'The Comedian at The Friday',
  41. '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.',
  42. 'uploader_id': 'bc168bee0d18dd1cb3b86c68706ab434',
  43. },
  44. 'skip': 'Content Unavailable'
  45. }, {
  46. 'url': 'http://tubitv.com/tv-shows/321886/s01_e01_on_nom_stories',
  47. 'only_matching': True,
  48. }, {
  49. 'url': 'https://tubitv.com/movies/560057/penitentiary?start=true',
  50. 'info_dict': {
  51. 'id': '560057',
  52. 'ext': 'mp4',
  53. 'title': 'Penitentiary',
  54. 'description': 'md5:8d2fc793a93cc1575ff426fdcb8dd3f9',
  55. 'uploader_id': 'd8fed30d4f24fcb22ec294421b9defc2',
  56. 'release_year': 1979,
  57. },
  58. 'skip': 'Content Unavailable'
  59. }]
  60. # DRM formats are included only to raise appropriate error
  61. _UNPLAYABLE_FORMATS = ('hlsv6_widevine', 'hlsv6_widevine_nonclearlead', 'hlsv6_playready_psshv0',
  62. 'hlsv6_fairplay', 'dash_widevine', 'dash_widevine_nonclearlead')
  63. def _perform_login(self, username, password):
  64. self.report_login()
  65. form_data = {
  66. 'username': username,
  67. 'password': password,
  68. }
  69. payload = urlencode_postdata(form_data)
  70. request = sanitized_Request(self._LOGIN_URL, payload)
  71. request.add_header('Content-Type', 'application/x-www-form-urlencoded')
  72. login_page = self._download_webpage(
  73. request, None, False, 'Wrong login info')
  74. if not re.search(r'id="tubi-logout"', login_page):
  75. raise ExtractorError(
  76. 'Login failed (invalid username/password)', expected=True)
  77. def _real_extract(self, url):
  78. video_id = self._match_id(url)
  79. video_data = self._download_json(f'https://tubitv.com/oz/videos/{video_id}/content', video_id, query={
  80. 'video_resources': ['dash', 'hlsv3', 'hlsv6', *self._UNPLAYABLE_FORMATS],
  81. })
  82. title = video_data['title']
  83. formats = []
  84. drm_formats = False
  85. for resource in video_data['video_resources']:
  86. if resource['type'] in ('dash', ):
  87. formats += self._extract_mpd_formats(resource['manifest']['url'], video_id, mpd_id=resource['type'], fatal=False)
  88. elif resource['type'] in ('hlsv3', 'hlsv6'):
  89. formats += self._extract_m3u8_formats(resource['manifest']['url'], video_id, 'mp4', m3u8_id=resource['type'], fatal=False)
  90. elif resource['type'] in self._UNPLAYABLE_FORMATS:
  91. drm_formats = True
  92. if not formats and drm_formats:
  93. self.report_drm(video_id)
  94. elif not formats and not video_data.get('policy_match'): # policy_match is False if content was removed
  95. raise ExtractorError('This content is currently unavailable', expected=True)
  96. thumbnails = []
  97. for thumbnail_url in video_data.get('thumbnails', []):
  98. if not thumbnail_url:
  99. continue
  100. thumbnails.append({
  101. 'url': self._proto_relative_url(thumbnail_url),
  102. })
  103. subtitles = {}
  104. for sub in video_data.get('subtitles', []):
  105. sub_url = sub.get('url')
  106. if not sub_url:
  107. continue
  108. subtitles.setdefault(sub.get('lang', 'English'), []).append({
  109. 'url': self._proto_relative_url(sub_url),
  110. })
  111. season_number, episode_number, episode_title = self._search_regex(
  112. r'^S(\d+):E(\d+) - (.+)', title, 'episode info', fatal=False, group=(1, 2, 3), default=(None, None, None))
  113. return {
  114. 'id': video_id,
  115. 'title': title,
  116. 'formats': formats,
  117. 'subtitles': subtitles,
  118. 'thumbnails': thumbnails,
  119. 'description': video_data.get('description'),
  120. 'duration': int_or_none(video_data.get('duration')),
  121. 'uploader_id': video_data.get('publisher_id'),
  122. 'release_year': int_or_none(video_data.get('year')),
  123. 'season_number': int_or_none(season_number),
  124. 'episode_number': int_or_none(episode_number),
  125. 'episode_title': episode_title
  126. }
  127. class TubiTvShowIE(InfoExtractor):
  128. _VALID_URL = r'https?://(?:www\.)?tubitv\.com/series/[0-9]+/(?P<show_name>[^/?#]+)'
  129. _TESTS = [{
  130. 'url': 'https://tubitv.com/series/3936/the-joy-of-painting-with-bob-ross?start=true',
  131. 'playlist_mincount': 390,
  132. 'info_dict': {
  133. 'id': 'the-joy-of-painting-with-bob-ross',
  134. }
  135. }]
  136. def _entries(self, show_url, show_name):
  137. show_webpage = self._download_webpage(show_url, show_name)
  138. show_json = self._parse_json(self._search_regex(
  139. r'window\.__data\s*=\s*({[^<]+});\s*</script>',
  140. show_webpage, 'data'), show_name, transform_source=js_to_json)['video']
  141. for episode_id in show_json['fullContentById'].keys():
  142. if traverse_obj(show_json, ('byId', episode_id, 'type')) == 's':
  143. continue
  144. yield self.url_result(
  145. 'tubitv:%s' % episode_id,
  146. ie=TubiTvIE.ie_key(), video_id=episode_id)
  147. def _real_extract(self, url):
  148. show_name = self._match_valid_url(url).group('show_name')
  149. return self.playlist_result(self._entries(url, show_name), playlist_id=show_name)