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