aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcodl <[email protected]>2019-04-15 03:23:04 +0200
committercodl <[email protected]>2019-04-15 03:31:09 +0200
commitad96297a0608bfd5196598cc3bbcce1f4aa03bc9 (patch)
treef5a509461ab2692b0a1bddc95cb498963a8b7ebb
parent8b8626978752baf14347498640b2319db832145e (diff)
downloadmastodon.py-ad96297a0608bfd5196598cc3bbcce1f4aa03bc9.tar.gz
more robust handling of pagination Link headers
during a cursory investigation for #163 I found that the code handling Link headers would not handle non-numeric post IDs like pleroma's flakeIDs correctly IDs starting with a number would be truncated to the first non-digit, and IDs not starting with a number would throw. Thankfully, all flakeIDs generated so far start with 9. Maybe 8 for the earliest ones, I'm not sure. Either way, so far it would only have misbehaved when using the pagination functions or accessing the _pagination_prev and _pagination_next attributes directly
-rw-r--r--mastodon/Mastodon.py16
1 files changed, 12 insertions, 4 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py
index fc585ba..0550d85 100644
--- a/mastodon/Mastodon.py
+++ b/mastodon/Mastodon.py
@@ -2272,13 +2272,17 @@ class Mastodon:
2272 if url['rel'] == 'next': 2272 if url['rel'] == 'next':
2273 # Be paranoid and extract max_id specifically 2273 # Be paranoid and extract max_id specifically
2274 next_url = url['url'] 2274 next_url = url['url']
2275 matchgroups = re.search(r"max_id=([0-9]*)", next_url) 2275 matchgroups = re.search(r"max_id=([^&]+)", next_url)
2276 2276
2277 if matchgroups: 2277 if matchgroups:
2278 next_params = copy.deepcopy(params) 2278 next_params = copy.deepcopy(params)
2279 next_params['_pagination_method'] = method 2279 next_params['_pagination_method'] = method
2280 next_params['_pagination_endpoint'] = endpoint 2280 next_params['_pagination_endpoint'] = endpoint
2281 next_params['max_id'] = int(matchgroups.group(1)) 2281 max_id = matchgroups.group(1)
2282 if max_id.is_digit():
2283 next_params['max_id'] = int(max_id)
2284 else:
2285 next_params['max_id'] = max_id
2282 if "since_id" in next_params: 2286 if "since_id" in next_params:
2283 del next_params['since_id'] 2287 del next_params['since_id']
2284 response[-1]._pagination_next = next_params 2288 response[-1]._pagination_next = next_params
@@ -2286,13 +2290,17 @@ class Mastodon:
2286 if url['rel'] == 'prev': 2290 if url['rel'] == 'prev':
2287 # Be paranoid and extract since_id specifically 2291 # Be paranoid and extract since_id specifically
2288 prev_url = url['url'] 2292 prev_url = url['url']
2289 matchgroups = re.search(r"since_id=([0-9]*)", prev_url) 2293 matchgroups = re.search(r"since_id=([^&]+)", prev_url)
2290 2294
2291 if matchgroups: 2295 if matchgroups:
2292 prev_params = copy.deepcopy(params) 2296 prev_params = copy.deepcopy(params)
2293 prev_params['_pagination_method'] = method 2297 prev_params['_pagination_method'] = method
2294 prev_params['_pagination_endpoint'] = endpoint 2298 prev_params['_pagination_endpoint'] = endpoint
2295 prev_params['since_id'] = int(matchgroups.group(1)) 2299 since_id = matchgroups.group(1)
2300 if since_id.is_digit():
2301 prev_params['since_id'] = int(since_id)
2302 else:
2303 prev_params['since_id'] = since_id
2296 if "max_id" in prev_params: 2304 if "max_id" in prev_params:
2297 del prev_params['max_id'] 2305 del prev_params['max_id']
2298 response[0]._pagination_prev = prev_params 2306 response[0]._pagination_prev = prev_params
Powered by cgit v1.2.3 (git 2.41.0)