diff options
author | Aljoscha Rittner <[email protected]> | 2022-06-24 17:30:29 +0200 |
---|---|---|
committer | GitHub <[email protected]> | 2022-06-24 17:30:29 +0200 |
commit | c9008a1cdcd31ba47c10d7b6f35cc7aceffe81f5 (patch) | |
tree | d1e450afcca6760d53d340359be6cbc26e35137d /mastodon | |
parent | 002c6511a7c6a340efdaee15849a31afa38871fc (diff) | |
parent | e98b033646b0d9d7851d2a0e736ff6f1b60b0ef8 (diff) | |
download | mastodon.py-c9008a1cdcd31ba47c10d7b6f35cc7aceffe81f5.tar.gz |
Merge pull request #233 from arittner/master
Changes the storage for pagination information
Diffstat (limited to 'mastodon')
-rw-r--r-- | mastodon/Mastodon.py | 51 |
1 files changed, 42 insertions, 9 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 8b0afb3..0922a6f 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py | |||
@@ -120,10 +120,27 @@ class AttribAccessDict(dict): | |||
120 | raise AttributeError("Attribute-style access is read only") | 120 | raise AttributeError("Attribute-style access is read only") |
121 | super(AttribAccessDict, self).__setattr__(attr, val) | 121 | super(AttribAccessDict, self).__setattr__(attr, val) |
122 | 122 | ||
123 | |||
123 | ### | 124 | ### |
124 | # The actual Mastodon class | 125 | # List helper class. |
126 | # Defined at top level so it can be pickled. | ||
125 | ### | 127 | ### |
128 | class AttribAccessList(list): | ||
129 | def __getattr__(self, attr): | ||
130 | if attr in self: | ||
131 | return self[attr] | ||
132 | else: | ||
133 | raise AttributeError("Attribute not found: " + str(attr)) | ||
134 | |||
135 | def __setattr__(self, attr, val): | ||
136 | if attr in self: | ||
137 | raise AttributeError("Attribute-style access is read only") | ||
138 | super(AttribAccessList, self).__setattr__(attr, val) | ||
139 | |||
126 | 140 | ||
141 | ### | ||
142 | # The actual Mastodon class | ||
143 | ### | ||
127 | class Mastodon: | 144 | class Mastodon: |
128 | """ | 145 | """ |
129 | Thorough and easy to use Mastodon | 146 | Thorough and easy to use Mastodon |
@@ -3050,8 +3067,8 @@ class Mastodon: | |||
3050 | Returns the next page or None if no further data is available. | 3067 | Returns the next page or None if no further data is available. |
3051 | """ | 3068 | """ |
3052 | if isinstance(previous_page, list) and len(previous_page) != 0: | 3069 | if isinstance(previous_page, list) and len(previous_page) != 0: |
3053 | if hasattr(previous_page[-1], '_pagination_next'): | 3070 | if hasattr(previous_page, '_pagination_next'): |
3054 | params = copy.deepcopy(previous_page[-1]._pagination_next) | 3071 | params = copy.deepcopy(previous_page._pagination_next) |
3055 | else: | 3072 | else: |
3056 | return None | 3073 | return None |
3057 | else: | 3074 | else: |
@@ -3074,8 +3091,8 @@ class Mastodon: | |||
3074 | Returns the previous page or None if no further data is available. | 3091 | Returns the previous page or None if no further data is available. |
3075 | """ | 3092 | """ |
3076 | if isinstance(next_page, list) and len(next_page) != 0: | 3093 | if isinstance(next_page, list) and len(next_page) != 0: |
3077 | if hasattr(next_page[0], '_pagination_prev'): | 3094 | if hasattr(next_page, '_pagination_prev'): |
3078 | params = copy.deepcopy(next_page[0]._pagination_prev) | 3095 | params = copy.deepcopy(next_page._pagination_prev) |
3079 | else: | 3096 | else: |
3080 | return None | 3097 | return None |
3081 | else: | 3098 | else: |
@@ -3453,6 +3470,7 @@ class Mastodon: | |||
3453 | if isinstance(response, list) and \ | 3470 | if isinstance(response, list) and \ |
3454 | 'Link' in response_object.headers and \ | 3471 | 'Link' in response_object.headers and \ |
3455 | response_object.headers['Link'] != "": | 3472 | response_object.headers['Link'] != "": |
3473 | response = AttribAccessList(response) | ||
3456 | tmp_urls = requests.utils.parse_header_links( | 3474 | tmp_urls = requests.utils.parse_header_links( |
3457 | response_object.headers['Link'].rstrip('>').replace('>,<', ',<')) | 3475 | response_object.headers['Link'].rstrip('>').replace('>,<', ',<')) |
3458 | for url in tmp_urls: | 3476 | for url in tmp_urls: |
@@ -3477,7 +3495,12 @@ class Mastodon: | |||
3477 | del next_params['since_id'] | 3495 | del next_params['since_id'] |
3478 | if "min_id" in next_params: | 3496 | if "min_id" in next_params: |
3479 | del next_params['min_id'] | 3497 | del next_params['min_id'] |
3480 | response[-1]._pagination_next = next_params | 3498 | response._pagination_next = next_params |
3499 | |||
3500 | # Maybe other API users rely on the pagination info in the last item | ||
3501 | # Will be removed in future | ||
3502 | if isinstance(response[-1], AttribAccessDict): | ||
3503 | response[-1]._pagination_next = next_params | ||
3481 | 3504 | ||
3482 | if url['rel'] == 'prev': | 3505 | if url['rel'] == 'prev': |
3483 | # Be paranoid and extract since_id or min_id specifically | 3506 | # Be paranoid and extract since_id or min_id specifically |
@@ -3496,8 +3519,13 @@ class Mastodon: | |||
3496 | prev_params['since_id'] = since_id | 3519 | prev_params['since_id'] = since_id |
3497 | if "max_id" in prev_params: | 3520 | if "max_id" in prev_params: |
3498 | del prev_params['max_id'] | 3521 | del prev_params['max_id'] |
3499 | response[0]._pagination_prev = prev_params | 3522 | response._pagination_prev = prev_params |
3500 | 3523 | ||
3524 | # Maybe other API users rely on the pagination info in the first item | ||
3525 | # Will be removed in future | ||
3526 | if isinstance(response[0], AttribAccessDict): | ||
3527 | response[0]._pagination_prev = prev_params | ||
3528 | |||
3501 | # New and fantastico (post-2.6.0): min_id pagination | 3529 | # New and fantastico (post-2.6.0): min_id pagination |
3502 | matchgroups = re.search(r"[?&]min_id=([^&]+)", prev_url) | 3530 | matchgroups = re.search(r"[?&]min_id=([^&]+)", prev_url) |
3503 | if matchgroups: | 3531 | if matchgroups: |
@@ -3511,7 +3539,12 @@ class Mastodon: | |||
3511 | prev_params['min_id'] = min_id | 3539 | prev_params['min_id'] = min_id |
3512 | if "max_id" in prev_params: | 3540 | if "max_id" in prev_params: |
3513 | del prev_params['max_id'] | 3541 | del prev_params['max_id'] |
3514 | response[0]._pagination_prev = prev_params | 3542 | response._pagination_prev = prev_params |
3543 | |||
3544 | # Maybe other API users rely on the pagination info in the first item | ||
3545 | # Will be removed in future | ||
3546 | if isinstance(response[0], AttribAccessDict): | ||
3547 | response[0]._pagination_prev = prev_params | ||
3515 | 3548 | ||
3516 | return response | 3549 | return response |
3517 | 3550 | ||