diff options
Diffstat (limited to 'mastodon/utility.py')
-rw-r--r-- | mastodon/utility.py | 116 |
1 files changed, 114 insertions, 2 deletions
diff --git a/mastodon/utility.py b/mastodon/utility.py index 53980b6..2510dca 100644 --- a/mastodon/utility.py +++ b/mastodon/utility.py | |||
@@ -1,10 +1,13 @@ | |||
1 | # utility.py - utility functions, externally usable | 1 | # utility.py - utility functions, externally usable |
2 | 2 | ||
3 | import re | 3 | import re |
4 | from decorator import decorate | ||
5 | from .error import MastodonVersionError, MastodonAPIError | ||
6 | import dateutil | 4 | import dateutil |
7 | import datetime | 5 | import datetime |
6 | import copy | ||
7 | |||
8 | from decorator import decorate | ||
9 | from .errors import MastodonVersionError, MastodonAPIError | ||
10 | from .compat import IMPL_HAS_BLURHASH, blurhash | ||
8 | 11 | ||
9 | # Module level: | 12 | # Module level: |
10 | 13 | ||
@@ -141,3 +144,112 @@ class Mastodon(): | |||
141 | return datetime.datetime.fromtimestamp(epoch_time) | 144 | return datetime.datetime.fromtimestamp(epoch_time) |
142 | else: | 145 | else: |
143 | raise MastodonAPIError("No server time in response.") | 146 | raise MastodonAPIError("No server time in response.") |
147 | |||
148 | ### | ||
149 | # Blurhash utilities | ||
150 | ### | ||
151 | def decode_blurhash(self, media_dict, out_size=(16, 16), size_per_component=True, return_linear=True): | ||
152 | """ | ||
153 | Basic media-dict blurhash decoding. | ||
154 | |||
155 | out_size is the desired result size in pixels, either absolute or per blurhash | ||
156 | component (this is the default). | ||
157 | |||
158 | By default, this function will return the image as linear RGB, ready for further | ||
159 | scaling operations. If you want to display the image directly, set return_linear | ||
160 | to False. | ||
161 | |||
162 | Returns the decoded blurhash image as a three-dimensional list: [height][width][3], | ||
163 | with the last dimension being RGB colours. | ||
164 | |||
165 | For further info and tips for advanced usage, refer to the documentation for the | ||
166 | blurhash module: https://github.com/halcy/blurhash-python | ||
167 | """ | ||
168 | if not IMPL_HAS_BLURHASH: | ||
169 | raise NotImplementedError( | ||
170 | 'To use the blurhash functions, please install the blurhash Python module.') | ||
171 | |||
172 | # Figure out what size to decode to | ||
173 | decode_components_x, decode_components_y = blurhash.components(media_dict["blurhash"]) | ||
174 | if size_per_component: | ||
175 | decode_size_x = decode_components_x * out_size[0] | ||
176 | decode_size_y = decode_components_y * out_size[1] | ||
177 | else: | ||
178 | decode_size_x = out_size[0] | ||
179 | decode_size_y = out_size[1] | ||
180 | |||
181 | # Decode | ||
182 | decoded_image = blurhash.decode(media_dict["blurhash"], decode_size_x, decode_size_y, linear=return_linear) | ||
183 | |||
184 | # And that's pretty much it. | ||
185 | return decoded_image | ||
186 | |||
187 | ### | ||
188 | # Pagination | ||
189 | ### | ||
190 | def fetch_next(self, previous_page): | ||
191 | """ | ||
192 | Fetches the next page of results of a paginated request. Pass in the | ||
193 | previous page in its entirety, or the pagination information dict | ||
194 | returned as a part of that pages last status ('_pagination_next'). | ||
195 | |||
196 | Returns the next page or None if no further data is available. | ||
197 | """ | ||
198 | if isinstance(previous_page, list) and len(previous_page) != 0: | ||
199 | if hasattr(previous_page, '_pagination_next'): | ||
200 | params = copy.deepcopy(previous_page._pagination_next) | ||
201 | else: | ||
202 | return None | ||
203 | else: | ||
204 | params = copy.deepcopy(previous_page) | ||
205 | |||
206 | method = params['_pagination_method'] | ||
207 | del params['_pagination_method'] | ||
208 | |||
209 | endpoint = params['_pagination_endpoint'] | ||
210 | del params['_pagination_endpoint'] | ||
211 | |||
212 | return self.__api_request(method, endpoint, params) | ||
213 | |||
214 | def fetch_previous(self, next_page): | ||
215 | """ | ||
216 | Fetches the previous page of results of a paginated request. Pass in the | ||
217 | previous page in its entirety, or the pagination information dict | ||
218 | returned as a part of that pages first status ('_pagination_prev'). | ||
219 | |||
220 | Returns the previous page or None if no further data is available. | ||
221 | """ | ||
222 | if isinstance(next_page, list) and len(next_page) != 0: | ||
223 | if hasattr(next_page, '_pagination_prev'): | ||
224 | params = copy.deepcopy(next_page._pagination_prev) | ||
225 | else: | ||
226 | return None | ||
227 | else: | ||
228 | params = copy.deepcopy(next_page) | ||
229 | |||
230 | method = params['_pagination_method'] | ||
231 | del params['_pagination_method'] | ||
232 | |||
233 | endpoint = params['_pagination_endpoint'] | ||
234 | del params['_pagination_endpoint'] | ||
235 | |||
236 | return self.__api_request(method, endpoint, params) | ||
237 | |||
238 | def fetch_remaining(self, first_page): | ||
239 | """ | ||
240 | Fetches all the remaining pages of a paginated request starting from a | ||
241 | first page and returns the entire set of results (including the first page | ||
242 | that was passed in) as a big list. | ||
243 | |||
244 | Be careful, as this might generate a lot of requests, depending on what you are | ||
245 | fetching, and might cause you to run into rate limits very quickly. | ||
246 | """ | ||
247 | first_page = copy.deepcopy(first_page) | ||
248 | |||
249 | all_pages = [] | ||
250 | current_page = first_page | ||
251 | while current_page is not None and len(current_page) > 0: | ||
252 | all_pages.extend(current_page) | ||
253 | current_page = self.fetch_next(current_page) | ||
254 | |||
255 | return all_pages | ||