aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'mastodon')
-rw-r--r--mastodon/Mastodon.py29
1 files changed, 23 insertions, 6 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py
index f78d303..dce2081 100644
--- a/mastodon/Mastodon.py
+++ b/mastodon/Mastodon.py
@@ -2222,9 +2222,12 @@ class Mastodon:
2222 if not response_object.ok: 2222 if not response_object.ok:
2223 try: 2223 try:
2224 response = response_object.json(object_hook=self.__json_hooks) 2224 response = response_object.json(object_hook=self.__json_hooks)
2225 if not isinstance(response, dict) or 'error' not in response: 2225 if isinstance(response, dict) and 'error' in response:
2226 error_msg = response['error']
2227 elif isinstance(response, str):
2228 error_msg = response
2229 else:
2226 error_msg = None 2230 error_msg = None
2227 error_msg = response['error']
2228 except ValueError: 2231 except ValueError:
2229 error_msg = None 2232 error_msg = None
2230 2233
@@ -2250,6 +2253,8 @@ class Mastodon:
2250 # on any 404 2253 # on any 404
2251 elif response_object.status_code == 401: 2254 elif response_object.status_code == 401:
2252 ex_type = MastodonUnauthorizedError 2255 ex_type = MastodonUnauthorizedError
2256 elif response_object.status_code == 502:
2257 ex_type = MastodonServerError
2253 else: 2258 else:
2254 ex_type = MastodonAPIError 2259 ex_type = MastodonAPIError
2255 2260
@@ -2280,13 +2285,17 @@ class Mastodon:
2280 if url['rel'] == 'next': 2285 if url['rel'] == 'next':
2281 # Be paranoid and extract max_id specifically 2286 # Be paranoid and extract max_id specifically
2282 next_url = url['url'] 2287 next_url = url['url']
2283 matchgroups = re.search(r"max_id=([0-9]*)", next_url) 2288 matchgroups = re.search(r"[?&]max_id=([^&]+)", next_url)
2284 2289
2285 if matchgroups: 2290 if matchgroups:
2286 next_params = copy.deepcopy(params) 2291 next_params = copy.deepcopy(params)
2287 next_params['_pagination_method'] = method 2292 next_params['_pagination_method'] = method
2288 next_params['_pagination_endpoint'] = endpoint 2293 next_params['_pagination_endpoint'] = endpoint
2289 next_params['max_id'] = int(matchgroups.group(1)) 2294 max_id = matchgroups.group(1)
2295 if max_id.isdigit():
2296 next_params['max_id'] = int(max_id)
2297 else:
2298 next_params['max_id'] = max_id
2290 if "since_id" in next_params: 2299 if "since_id" in next_params:
2291 del next_params['since_id'] 2300 del next_params['since_id']
2292 response[-1]._pagination_next = next_params 2301 response[-1]._pagination_next = next_params
@@ -2294,13 +2303,17 @@ class Mastodon:
2294 if url['rel'] == 'prev': 2303 if url['rel'] == 'prev':
2295 # Be paranoid and extract since_id specifically 2304 # Be paranoid and extract since_id specifically
2296 prev_url = url['url'] 2305 prev_url = url['url']
2297 matchgroups = re.search(r"since_id=([0-9]*)", prev_url) 2306 matchgroups = re.search(r"[?&]since_id=([^&]+)", prev_url)
2298 2307
2299 if matchgroups: 2308 if matchgroups:
2300 prev_params = copy.deepcopy(params) 2309 prev_params = copy.deepcopy(params)
2301 prev_params['_pagination_method'] = method 2310 prev_params['_pagination_method'] = method
2302 prev_params['_pagination_endpoint'] = endpoint 2311 prev_params['_pagination_endpoint'] = endpoint
2303 prev_params['since_id'] = int(matchgroups.group(1)) 2312 since_id = matchgroups.group(1)
2313 if since_id.isdigit():
2314 prev_params['since_id'] = int(since_id)
2315 else:
2316 prev_params['since_id'] = since_id
2304 if "max_id" in prev_params: 2317 if "max_id" in prev_params:
2305 del prev_params['max_id'] 2318 del prev_params['max_id']
2306 response[0]._pagination_prev = prev_params 2319 response[0]._pagination_prev = prev_params
@@ -2536,6 +2549,10 @@ class MastodonAPIError(MastodonError):
2536 """Raised when the mastodon API generates a response that cannot be handled""" 2549 """Raised when the mastodon API generates a response that cannot be handled"""
2537 pass 2550 pass
2538 2551
2552class MastodonServerError(MastodonError):
2553 """Raised if the Server is malconfigured, e.g. returns a 502 error code"""
2554 pass
2555
2539class MastodonNotFoundError(MastodonAPIError): 2556class MastodonNotFoundError(MastodonAPIError):
2540 """Raised when the mastodon API returns a 404 Not Found error""" 2557 """Raised when the mastodon API returns a 404 Not Found error"""
2541 pass 2558 pass
Powered by cgit v1.2.3 (git 2.41.0)