]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/dropout.py
[cleanup] Add more ruff rules (#10149)
[yt-dlp.git] / yt_dlp / extractor / dropout.py
CommitLineData
db22142f
O
1import functools
2
e9efb99f
S
3from .common import InfoExtractor
4from .vimeo import VHXEmbedIE
5from ..utils import (
e9efb99f 6 ExtractorError,
db22142f 7 OnDemandPagedList,
28786529 8 clean_html,
db22142f 9 extract_attributes,
e9efb99f
S
10 get_element_by_class,
11 get_element_by_id,
db22142f 12 get_elements_html_by_class,
e9efb99f 13 int_or_none,
db22142f 14 traverse_obj,
e9efb99f
S
15 unified_strdate,
16 urlencode_postdata,
17)
18
19
20class DropoutIE(InfoExtractor):
21 _LOGIN_URL = 'https://www.dropout.tv/login'
22 _NETRC_MACHINE = 'dropout'
23
24 _VALID_URL = r'https?://(?:www\.)?dropout\.tv/(?:[^/]+/)*videos/(?P<id>[^/]+)/?$'
25 _TESTS = [
26 {
27 'url': 'https://www.dropout.tv/game-changer/season:2/videos/yes-or-no',
28 'note': 'Episode in a series',
29 'md5': '5e000fdfd8d8fa46ff40456f1c2af04a',
30 'info_dict': {
31 'id': '738153',
32 'display_id': 'yes-or-no',
33 'ext': 'mp4',
34 'title': 'Yes or No',
35 'description': 'Ally, Brennan, and Zac are asked a simple question, but is there a correct answer?',
36 'release_date': '20200508',
37 'thumbnail': 'https://vhx.imgix.net/chuncensoredstaging/assets/351e3f24-c4a3-459a-8b79-dc80f1e5b7fd.jpg',
38 'series': 'Game Changer',
39 'season_number': 2,
40 'season': 'Season 2',
41 'episode_number': 6,
42 'episode': 'Yes or No',
43 'duration': 1180,
44 'uploader_id': 'user80538407',
45 'uploader_url': 'https://vimeo.com/user80538407',
add96eb9 46 'uploader': 'OTT Videos',
e9efb99f 47 },
add96eb9 48 'expected_warnings': ['Ignoring subtitle tracks found in the HLS manifest'],
e9efb99f
S
49 },
50 {
51 'url': 'https://www.dropout.tv/dimension-20-fantasy-high/season:1/videos/episode-1',
52 'note': 'Episode in a series (missing release_date)',
53 'md5': '712caf7c191f1c47c8f1879520c2fa5c',
54 'info_dict': {
55 'id': '320562',
56 'display_id': 'episode-1',
57 'ext': 'mp4',
58 'title': 'The Beginning Begins',
59 'description': 'The cast introduces their PCs, including a neurotic elf, a goblin PI, and a corn-worshipping cleric.',
60 'thumbnail': 'https://vhx.imgix.net/chuncensoredstaging/assets/4421ed0d-f630-4c88-9004-5251b2b8adfa.jpg',
61 'series': 'Dimension 20: Fantasy High',
62 'season_number': 1,
63 'season': 'Season 1',
64 'episode_number': 1,
65 'episode': 'The Beginning Begins',
66 'duration': 6838,
67 'uploader_id': 'user80538407',
68 'uploader_url': 'https://vimeo.com/user80538407',
add96eb9 69 'uploader': 'OTT Videos',
e9efb99f 70 },
add96eb9 71 'expected_warnings': ['Ignoring subtitle tracks found in the HLS manifest'],
e9efb99f
S
72 },
73 {
74 'url': 'https://www.dropout.tv/videos/misfits-magic-holiday-special',
75 'note': 'Episode not in a series',
76 'md5': 'c30fa18999c5880d156339f13c953a26',
77 'info_dict': {
78 'id': '1915774',
79 'display_id': 'misfits-magic-holiday-special',
80 'ext': 'mp4',
81 'title': 'Misfits & Magic Holiday Special',
82 'description': 'The magical misfits spend Christmas break at Gowpenny, with an unwelcome visitor.',
83 'release_date': '20211215',
84 'thumbnail': 'https://vhx.imgix.net/chuncensoredstaging/assets/d91ea8a6-b250-42ed-907e-b30fb1c65176-8e24b8e5.jpg',
85 'duration': 11698,
86 'uploader_id': 'user80538407',
87 'uploader_url': 'https://vimeo.com/user80538407',
add96eb9 88 'uploader': 'OTT Videos',
e9efb99f 89 },
add96eb9 90 'expected_warnings': ['Ignoring subtitle tracks found in the HLS manifest'],
91 },
e9efb99f
S
92 ]
93
94 def _get_authenticity_token(self, display_id):
95 signin_page = self._download_webpage(
96 self._LOGIN_URL, display_id, note='Getting authenticity token')
97 return self._html_search_regex(
98 r'name=["\']authenticity_token["\'] value=["\'](.+?)["\']',
99 signin_page, 'authenticity_token')
100
101 def _login(self, display_id):
102 username, password = self._get_login_info()
28786529 103 if not username:
104 return True
e9efb99f
S
105
106 response = self._download_webpage(
28786529 107 self._LOGIN_URL, display_id, note='Logging in', fatal=False,
108 data=urlencode_postdata({
e9efb99f
S
109 'email': username,
110 'password': password,
111 'authenticity_token': self._get_authenticity_token(display_id),
add96eb9 112 'utf8': True,
e9efb99f
S
113 }))
114
115 user_has_subscription = self._search_regex(
116 r'user_has_subscription:\s*["\'](.+?)["\']', response, 'subscription status', default='none')
117 if user_has_subscription.lower() == 'true':
28786529 118 return
e9efb99f 119 elif user_has_subscription.lower() == 'false':
28786529 120 return 'Account is not subscribed'
e9efb99f 121 else:
28786529 122 return 'Incorrect username/password'
e9efb99f
S
123
124 def _real_extract(self, url):
125 display_id = self._match_id(url)
34baaced
JB
126
127 webpage = None
128 if self._get_cookies('https://www.dropout.tv').get('_session'):
129 webpage = self._download_webpage(url, display_id)
130 if not webpage or '<div id="watch-unauthorized"' in webpage:
28786529 131 login_err = self._login(display_id)
132 webpage = self._download_webpage(url, display_id)
34baaced 133 if login_err and '<div id="watch-unauthorized"' in webpage:
28786529 134 if login_err is True:
34baaced 135 self.raise_login_required(method='any')
28786529 136 raise ExtractorError(login_err, expected=True)
e9efb99f
S
137
138 embed_url = self._search_regex(r'embed_url:\s*["\'](.+?)["\']', webpage, 'embed url')
139 thumbnail = self._og_search_thumbnail(webpage)
140 watch_info = get_element_by_id('watch-info', webpage) or ''
141
142 title = clean_html(get_element_by_class('video-title', watch_info))
143 season_episode = get_element_by_class(
144 'site-font-secondary-color', get_element_by_class('text', watch_info))
145 episode_number = int_or_none(self._search_regex(
146 r'Episode (\d+)', season_episode or '', 'episode', default=None))
147
148 return {
149 '_type': 'url_transparent',
150 'ie_key': VHXEmbedIE.ie_key(),
aedaa455 151 'url': VHXEmbedIE._smuggle_referrer(embed_url, 'https://www.dropout.tv'),
a44ca5a4 152 'id': self._search_regex(r'embed\.vhx\.tv/videos/(.+?)\?', embed_url, 'id'),
e9efb99f
S
153 'display_id': display_id,
154 'title': title,
155 'description': self._html_search_meta('description', webpage, fatal=False),
156 'thumbnail': thumbnail.split('?')[0] if thumbnail else None, # Ignore crop/downscale
157 'series': clean_html(get_element_by_class('series-title', watch_info)),
158 'episode_number': episode_number,
159 'episode': title if episode_number else None,
160 'season_number': int_or_none(self._search_regex(
161 r'Season (\d+),', season_episode or '', 'season', default=None)),
162 'release_date': unified_strdate(self._search_regex(
163 r'data-meta-field-name=["\']release_dates["\'] data-meta-field-value=["\'](.+?)["\']',
164 watch_info, 'release date', default=None)),
165 }
166
167
168class DropoutSeasonIE(InfoExtractor):
db22142f
O
169 _PAGE_SIZE = 24
170 _VALID_URL = r'https?://(?:www\.)?dropout\.tv/(?P<id>[^\/$&?#]+)(?:/?$|/season:(?P<season>[0-9]+)/?$)'
e9efb99f
S
171 _TESTS = [
172 {
173 'url': 'https://www.dropout.tv/dimension-20-fantasy-high/season:1',
174 'note': 'Multi-season series with the season in the url',
db22142f 175 'playlist_count': 24,
e9efb99f
S
176 'info_dict': {
177 'id': 'dimension-20-fantasy-high-season-1',
add96eb9 178 'title': 'Dimension 20 Fantasy High - Season 1',
179 },
e9efb99f
S
180 },
181 {
182 'url': 'https://www.dropout.tv/dimension-20-fantasy-high',
183 'note': 'Multi-season series with the season not in the url',
db22142f 184 'playlist_count': 24,
e9efb99f
S
185 'info_dict': {
186 'id': 'dimension-20-fantasy-high-season-1',
add96eb9 187 'title': 'Dimension 20 Fantasy High - Season 1',
188 },
e9efb99f
S
189 },
190 {
191 'url': 'https://www.dropout.tv/dimension-20-shriek-week',
192 'note': 'Single-season series',
193 'playlist_count': 4,
194 'info_dict': {
195 'id': 'dimension-20-shriek-week-season-1',
add96eb9 196 'title': 'Dimension 20 Shriek Week - Season 1',
197 },
db22142f
O
198 },
199 {
200 'url': 'https://www.dropout.tv/breaking-news-no-laugh-newsroom/season:3',
201 'note': 'Multi-season series with season in the url that requires pagination',
202 'playlist_count': 25,
203 'info_dict': {
204 'id': 'breaking-news-no-laugh-newsroom-season-3',
add96eb9 205 'title': 'Breaking News No Laugh Newsroom - Season 3',
206 },
207 },
e9efb99f
S
208 ]
209
db22142f
O
210 def _fetch_page(self, url, season_id, page):
211 page += 1
212 webpage = self._download_webpage(
213 f'{url}?page={page}', season_id, note=f'Downloading page {page}', expected_status={400})
214 yield from [self.url_result(item_url, DropoutIE) for item_url in traverse_obj(
215 get_elements_html_by_class('browse-item-link', webpage), (..., {extract_attributes}, 'href'))]
216
e9efb99f
S
217 def _real_extract(self, url):
218 season_id = self._match_id(url)
db22142f 219 season_num = self._match_valid_url(url).group('season') or 1
e9efb99f 220 season_title = season_id.replace('-', ' ').title()
e9efb99f 221
db22142f
O
222 return self.playlist_result(
223 OnDemandPagedList(functools.partial(self._fetch_page, url, season_id), self._PAGE_SIZE),
224 f'{season_id}-season-{season_num}', f'{season_title} - Season {season_num}')