diff options
Diffstat (limited to 'mastodon')
-rw-r--r-- | mastodon/Mastodon.py | 67 |
1 files changed, 55 insertions, 12 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 98ac72a..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)) | ||
126 | 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 | |||
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 |
@@ -1633,13 +1650,23 @@ class Mastodon: | |||
1633 | # Reading data: Bookmarks | 1650 | # Reading data: Bookmarks |
1634 | ### | 1651 | ### |
1635 | @api_version("3.1.0", "3.1.0", __DICT_VERSION_STATUS) | 1652 | @api_version("3.1.0", "3.1.0", __DICT_VERSION_STATUS) |
1636 | def bookmarks(self): | 1653 | def bookmarks(self, max_id=None, min_id=None, since_id=None, limit=None): |
1637 | """ | 1654 | """ |
1638 | Get a list of statuses bookmarked by the logged-in user. | 1655 | Get a list of statuses bookmarked by the logged-in user. |
1639 | 1656 | ||
1640 | Returns a list of `toot dicts`_. | 1657 | Returns a list of `toot dicts`_. |
1641 | """ | 1658 | """ |
1642 | return self.__api_request('GET', '/api/v1/bookmarks') | 1659 | if max_id != None: |
1660 | max_id = self.__unpack_id(max_id) | ||
1661 | |||
1662 | if min_id != None: | ||
1663 | min_id = self.__unpack_id(min_id) | ||
1664 | |||
1665 | if since_id != None: | ||
1666 | since_id = self.__unpack_id(since_id) | ||
1667 | |||
1668 | params = self.__generate_params(locals()) | ||
1669 | return self.__api_request('GET', '/api/v1/bookmarks', params) | ||
1643 | 1670 | ||
1644 | ### | 1671 | ### |
1645 | # Writing data: Statuses | 1672 | # Writing data: Statuses |
@@ -3040,8 +3067,8 @@ class Mastodon: | |||
3040 | 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. |
3041 | """ | 3068 | """ |
3042 | if isinstance(previous_page, list) and len(previous_page) != 0: | 3069 | if isinstance(previous_page, list) and len(previous_page) != 0: |
3043 | if hasattr(previous_page[-1], '_pagination_next'): | 3070 | if hasattr(previous_page, '_pagination_next'): |
3044 | params = copy.deepcopy(previous_page[-1]._pagination_next) | 3071 | params = copy.deepcopy(previous_page._pagination_next) |
3045 | else: | 3072 | else: |
3046 | return None | 3073 | return None |
3047 | else: | 3074 | else: |
@@ -3064,8 +3091,8 @@ class Mastodon: | |||
3064 | 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. |
3065 | """ | 3092 | """ |
3066 | if isinstance(next_page, list) and len(next_page) != 0: | 3093 | if isinstance(next_page, list) and len(next_page) != 0: |
3067 | if hasattr(next_page[0], '_pagination_prev'): | 3094 | if hasattr(next_page, '_pagination_prev'): |
3068 | params = copy.deepcopy(next_page[0]._pagination_prev) | 3095 | params = copy.deepcopy(next_page._pagination_prev) |
3069 | else: | 3096 | else: |
3070 | return None | 3097 | return None |
3071 | else: | 3098 | else: |
@@ -3230,7 +3257,7 @@ class Mastodon: | |||
3230 | if (key in json_object and isinstance(json_object[key], six.text_type)): | 3257 | if (key in json_object and isinstance(json_object[key], six.text_type)): |
3231 | if json_object[key].lower() == 'true': | 3258 | if json_object[key].lower() == 'true': |
3232 | json_object[key] = True | 3259 | json_object[key] = True |
3233 | if json_object[key].lower() == 'False': | 3260 | if json_object[key].lower() == 'false': |
3234 | json_object[key] = False | 3261 | json_object[key] = False |
3235 | return json_object | 3262 | return json_object |
3236 | 3263 | ||
@@ -3443,6 +3470,7 @@ class Mastodon: | |||
3443 | if isinstance(response, list) and \ | 3470 | if isinstance(response, list) and \ |
3444 | 'Link' in response_object.headers and \ | 3471 | 'Link' in response_object.headers and \ |
3445 | response_object.headers['Link'] != "": | 3472 | response_object.headers['Link'] != "": |
3473 | response = AttribAccessList(response) | ||
3446 | tmp_urls = requests.utils.parse_header_links( | 3474 | tmp_urls = requests.utils.parse_header_links( |
3447 | response_object.headers['Link'].rstrip('>').replace('>,<', ',<')) | 3475 | response_object.headers['Link'].rstrip('>').replace('>,<', ',<')) |
3448 | for url in tmp_urls: | 3476 | for url in tmp_urls: |
@@ -3467,7 +3495,12 @@ class Mastodon: | |||
3467 | del next_params['since_id'] | 3495 | del next_params['since_id'] |
3468 | if "min_id" in next_params: | 3496 | if "min_id" in next_params: |
3469 | del next_params['min_id'] | 3497 | del next_params['min_id'] |
3470 | 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 | ||
3471 | 3504 | ||
3472 | if url['rel'] == 'prev': | 3505 | if url['rel'] == 'prev': |
3473 | # Be paranoid and extract since_id or min_id specifically | 3506 | # Be paranoid and extract since_id or min_id specifically |
@@ -3486,8 +3519,13 @@ class Mastodon: | |||
3486 | prev_params['since_id'] = since_id | 3519 | prev_params['since_id'] = since_id |
3487 | if "max_id" in prev_params: | 3520 | if "max_id" in prev_params: |
3488 | del prev_params['max_id'] | 3521 | del prev_params['max_id'] |
3489 | response[0]._pagination_prev = prev_params | 3522 | response._pagination_prev = prev_params |
3490 | 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 | |||
3491 | # New and fantastico (post-2.6.0): min_id pagination | 3529 | # New and fantastico (post-2.6.0): min_id pagination |
3492 | matchgroups = re.search(r"[?&]min_id=([^&]+)", prev_url) | 3530 | matchgroups = re.search(r"[?&]min_id=([^&]+)", prev_url) |
3493 | if matchgroups: | 3531 | if matchgroups: |
@@ -3501,7 +3539,12 @@ class Mastodon: | |||
3501 | prev_params['min_id'] = min_id | 3539 | prev_params['min_id'] = min_id |
3502 | if "max_id" in prev_params: | 3540 | if "max_id" in prev_params: |
3503 | del prev_params['max_id'] | 3541 | del prev_params['max_id'] |
3504 | 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 | ||
3505 | 3548 | ||
3506 | return response | 3549 | return response |
3507 | 3550 | ||