File size: 9,871 Bytes
44bafb2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
"""Library specific exception definitions."""
from typing import Pattern, Union
import logging
logger = logging.getLogger(__name__)
class PytubeFixError(Exception):
"""Base pytubefix exception that all others inherit.
This is done to not pollute the built-in exceptions, which *could* result
in unintended errors being unexpectedly and incorrectly handled within
implementers code.
"""
### MISC Errors ###
class MaxRetriesExceeded(PytubeFixError):
"""Maximum number of retries exceeded."""
class HTMLParseError(PytubeFixError):
"""HTML could not be parsed"""
class ExtractError(PytubeFixError):
"""Data extraction based exception."""
class SABRError(PytubeFixError):
def __init__(self, msg: str):
self.msg = msg
super().__init__(self.msg)
@property
def error_string(self):
return self.msg
class RegexMatchError(ExtractError):
"""Regex pattern did not return any matches."""
def __init__(self, caller: str, pattern: Union[str, Pattern]):
"""
:param str caller:
Calling function
:param str pattern:
Pattern that failed to match
"""
super().__init__(
f"{caller}: could not find match for {pattern}")
self.caller = caller
self.pattern = pattern
class InterpretationError(PytubeFixError):
def __init__(self, js_url: str):
self.js_url = js_url
super().__init__(self.error_string)
@property
def error_string(self):
return f'Error interpreting player js: {self.js_url}'
### Video Unavailable Errors ###
# There are really 3 types of errors thrown
# 1. VideoUnavailable - This is the base error type for all video errors.
# Or a catchall if neither the user or developer cares about the specific error.
# 2. Known Error Type, Extra info useful for user
# 3. Unknown Error Type, Important to Developer
## 1. VideoUnavailable ##
class VideoUnavailable(PytubeFixError):
"""
Base video error.
This is the base error type for all video errors.
Call this if you can't group the error by known error type and it is not important to the developer.
"""
def __init__(self, video_id: str):
"""
:param str video_id:
A YouTube video identifier.
"""
self.video_id = video_id
super().__init__(self.error_string)
@property
def error_string(self):
return f'{self.video_id} is unavailable'
## 2. Known Error Type, Extra info useful for user ##
class VideoPrivate(VideoUnavailable):
def __init__(self, video_id: str):
"""
:param str video_id:
A YouTube video identifier.
"""
self.video_id = video_id
super().__init__(self.video_id)
@property
def error_string(self):
return f'{self.video_id} is a private video'
class MembersOnly(VideoUnavailable):
"""Video is members-only.
YouTube has special videos that are only viewable to users who have
subscribed to a content creator.
ref: https://support.google.com/youtube/answer/7544492?hl=en
"""
def __init__(self, video_id: str):
"""
:param str video_id:
A YouTube video identifier.
"""
self.video_id = video_id
super().__init__(self.video_id)
@property
def error_string(self):
return f'{self.video_id} is a members-only video'
class VideoRegionBlocked(VideoUnavailable):
def __init__(self, video_id: str):
"""
:param str video_id:
A YouTube video identifier.
"""
self.video_id = video_id
super().__init__(self.video_id)
@property
def error_string(self):
return f'{self.video_id} is not available in your region'
class BotDetection(VideoUnavailable):
def __init__(self, video_id: str):
"""
:param str video_id:
A YouTube video identifier.
"""
self.video_id = video_id
super().__init__(self.video_id)
@property
def error_string(self):
return (
f'{self.video_id} This request was detected as a bot. Use `use_po_token=True` or switch to WEB client to view. '
f'See more details at https://github.com/JuanBindez/pytubefix/pull/209')
class PoTokenRequired(VideoUnavailable):
def __init__(self, video_id: str, client_name: str):
"""
:param str video_id:
A YouTube video identifier.
:param str client_name:
A YouTube client identifier.
"""
self.video_id = video_id
self.client_name = client_name
super().__init__(self.video_id)
@property
def error_string(self):
return (
f'{self.video_id} The {self.client_name} client requires PoToken to obtain functional streams, '
f'See more details at https://github.com/JuanBindez/pytubefix/pull/209')
class LoginRequired(VideoUnavailable):
def __init__(self, video_id: str, reason: str):
"""
:param str video_id:
A YouTube video identifier.
"""
self.video_id = video_id
self.reason = reason
super().__init__(self.video_id)
@property
def error_string(self):
return (
f'{self.video_id} requires login to view, YouTube reason: {self.reason}')
# legacy livestream error types still supported
class RecordingUnavailable(VideoUnavailable):
def __init__(self, video_id: str):
"""
:param str video_id:
A YouTube video identifier.
"""
self.video_id = video_id
super().__init__(self.video_id)
@property
def error_string(self):
return f'{self.video_id} does not have a live stream recording available'
class LiveStreamError(VideoUnavailable):
"""Video is a live stream."""
def __init__(self, video_id: str):
"""
:param str video_id:
A YouTube video identifier.
"""
self.video_id = video_id
super().__init__(self.video_id)
@property
def error_string(self):
return f'{self.video_id} is streaming live and cannot be loaded'
class LiveStreamOffline(VideoUnavailable):
"""The live will start soon"""
def __init__(self, video_id: str, reason: str):
"""
:param str video_id:
A YouTube video identifier.
:param str reason:
reason for the error
"""
self.video_id = video_id
self.reason = reason
super().__init__(self.video_id)
@property
def error_string(self):
return f'{self.video_id} {self.reason}'
# legacy age restricted error types still supported
class AgeRestrictedError(VideoUnavailable):
"""Video is age restricted, and cannot be accessed without OAuth."""
def __init__(self, video_id: str):
"""
:param str video_id:
A YouTube video identifier.
"""
self.video_id = video_id
super().__init__(self.video_id)
@property
def error_string(self):
return f"{self.video_id} is age restricted, and can't be accessed without logging in."
class AgeCheckRequiredError(VideoUnavailable):
def __init__(self, video_id: str):
"""
:param str video_id:
A YouTube video identifier.
"""
self.video_id = video_id
super().__init__(self.video_id)
@property
def error_string(self):
return f"{self.video_id} has age restrictions and cannot be accessed without confirmation."
class AgeCheckRequiredAccountError(VideoUnavailable):
def __init__(self, video_id: str):
"""
:param str video_id:
A YouTube video identifier.
"""
self.video_id = video_id
super().__init__(self.video_id)
@property
def error_string(self):
return (
f"{self.video_id} may be inappropriate for "
f"some users. Sign in to your primary account to confirm your age.")
class InnerTubeResponseError(VideoUnavailable):
def __init__(self, video_id: str, client: str):
"""
:param str video_id:
A YouTube video identifier.
"""
self.video_id = video_id
self.client = client
super().__init__(self.video_id)
@property
def error_string(self):
return (
f"{self.video_id} : {self.client} client did not receive a response from YouTube")
## 3. Unknown Error Type, Important to Developer ##
class UnknownVideoError(VideoUnavailable):
"""Unknown video error."""
def __init__(self, video_id: str, status: str = None, reason: str = None, developer_message: str = None):
"""
:param str video_id:
A YouTube video identifier.
:param str status:
The status code of the response.
:param str reason:
The reason for the error.
:param str developer_message:
The message from the developer.
"""
self.video_id = video_id
self.status = status
self.reason = reason
self.developer_message = developer_message
logger.warning('Unknown Video Error')
logger.warning(f'Video ID: {self.video_id}')
logger.warning(f'Status: {self.status}')
logger.warning(f'Reason: {self.reason}')
logger.warning(f'Developer Message: {self.developer_message}')
logger.warning(
'Please open an issue at '
'https://github.com/JuanBindez/pytubefix/issues '
'and provide the above log output.'
)
super().__init__(self.video_id)
@property
def error_string(self):
return f'{self.video_id} has an unknown error, check logs for more info [Status: {self.status}] [Reason: {self.reason}]'
|