Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mikf
GitHub Repository: mikf/gallery-dl
Path: blob/master/gallery_dl/exception.py
8824 views
1
# -*- coding: utf-8 -*-
2
3
# Copyright 2015-2023 Mike Fährmann
4
#
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License version 2 as
7
# published by the Free Software Foundation.
8
9
"""Exception classes used by gallery-dl
10
11
Class Hierarchy:
12
13
Exception
14
└── GalleryDLException
15
├── ExtractionError
16
│ ├── HttpError
17
│ │ └── ChallengeError
18
│ ├── AuthorizationError
19
│ │ └── AuthRequired
20
│ ├── AuthenticationError
21
│ └── NotFoundError
22
├── InputError
23
│ ├── FormatError
24
│ │ ├── FilenameFormatError
25
│ │ └── DirectoryFormatError
26
│ ├── FilterError
27
│ ├── InputFileError
28
│ └── NoExtractorError
29
└── ControlException
30
├── StopExtraction
31
├── AbortExtraction
32
├── TerminateExtraction
33
└── RestartExtraction
34
"""
35
36
37
class GalleryDLException(Exception):
38
"""Base class for GalleryDL exceptions"""
39
default = None
40
msgfmt = None
41
code = 1
42
43
def __init__(self, message=None, fmt=True):
44
if not message:
45
message = self.default
46
elif isinstance(message, Exception):
47
message = f"{message.__class__.__name__}: {message}"
48
if fmt and self.msgfmt is not None:
49
message = self.msgfmt.replace("{}", message)
50
self.message = message
51
Exception.__init__(self, message)
52
53
54
###############################################################################
55
# Extractor Errors ############################################################
56
57
class ExtractionError(GalleryDLException):
58
"""Base class for exceptions during information extraction"""
59
code = 4
60
61
62
class HttpError(ExtractionError):
63
"""HTTP request during data extraction failed"""
64
default = "HTTP request failed"
65
66
def __init__(self, message="", response=None):
67
self.response = response
68
if response is None:
69
self.status = 0
70
else:
71
self.status = response.status_code
72
if not message:
73
message = (f"'{response.status_code} {response.reason}' "
74
f"for '{response.url}'")
75
ExtractionError.__init__(self, message)
76
77
78
class ChallengeError(HttpError):
79
code = 8
80
81
def __init__(self, challenge, response):
82
message = (
83
f"{challenge} ({response.status_code} {response.reason}) "
84
f"for '{response.url}'")
85
HttpError.__init__(self, message, response)
86
87
88
class AuthenticationError(ExtractionError):
89
"""Invalid or missing login credentials"""
90
default = "Invalid login credentials"
91
code = 16
92
93
94
class AuthorizationError(ExtractionError):
95
"""Insufficient privileges to access a resource"""
96
default = "Insufficient privileges to access this resource"
97
code = 16
98
99
100
class AuthRequired(AuthorizationError):
101
default = "Account credentials required"
102
103
def __init__(self, auth=None, resource="resource", message=None):
104
if auth:
105
if not isinstance(auth, str):
106
auth = " or ".join(auth)
107
108
if resource:
109
if " " not in resource:
110
resource = f"this {resource}"
111
resource = f" to access {resource}"
112
else:
113
resource = ""
114
115
message = f" ('{message}')" if message else ""
116
message = f"{auth} needed{resource}{message}"
117
AuthorizationError.__init__(self, message)
118
119
120
class NotFoundError(ExtractionError):
121
"""Requested resource (gallery/image) could not be found"""
122
msgfmt = "Requested {} could not be found"
123
default = "resource (gallery/image)"
124
125
126
###############################################################################
127
# User Input ##################################################################
128
129
class InputError(GalleryDLException):
130
"""Error caused by user input and config options"""
131
code = 32
132
133
134
class FormatError(InputError):
135
"""Error while building output paths"""
136
137
138
class FilenameFormatError(FormatError):
139
"""Error while building output filenames"""
140
msgfmt = "Applying filename format string failed ({})"
141
142
143
class DirectoryFormatError(FormatError):
144
"""Error while building output directory paths"""
145
msgfmt = "Applying directory format string failed ({})"
146
147
148
class FilterError(InputError):
149
"""Error while evaluating a filter expression"""
150
msgfmt = "Evaluating filter expression failed ({})"
151
152
153
class InputFileError(InputError):
154
"""Error when parsing an input file"""
155
156
157
class NoExtractorError(InputError):
158
"""No extractor can handle the given URL"""
159
160
161
###############################################################################
162
# Control Flow ################################################################
163
164
class ControlException(GalleryDLException):
165
code = 0
166
167
168
class StopExtraction(ControlException):
169
"""Stop data extraction"""
170
171
def __init__(self, target=None):
172
ControlException.__init__(self)
173
174
if target is None:
175
self.target = None
176
self.depth = 1
177
elif isinstance(target, int):
178
self.target = None
179
self.depth = target
180
elif target.isdecimal():
181
self.target = None
182
self.depth = int(target)
183
else:
184
self.target = target
185
self.depth = 128
186
187
188
class AbortExtraction(ExtractionError, ControlException):
189
"""Abort data extraction due to an error"""
190
191
192
class TerminateExtraction(ControlException):
193
"""Terminate data extraction"""
194
195
196
class RestartExtraction(ControlException):
197
"""Restart data extraction"""
198
199