diff options
-rw-r--r-- | docs/index.rst | 7 | ||||
-rw-r--r-- | mastodon/Mastodon.py | 38 | ||||
-rw-r--r-- | setup.py | 3 | ||||
-rw-r--r-- | tests/test_push.py | 2 |
4 files changed, 47 insertions, 3 deletions
diff --git a/docs/index.rst b/docs/index.rst index b5b1ea5..4e95bbb 100644 --- a/docs/index.rst +++ b/docs/index.rst | |||
@@ -554,6 +554,7 @@ Media dicts | |||
554 | # rounded), and a "length" field giving a videos length in a human-readable format. | 554 | # rounded), and a "length" field giving a videos length in a human-readable format. |
555 | # Note that a video may have an image as preview. | 555 | # Note that a video may have an image as preview. |
556 | # May also contain a 'focus' dict. | 556 | # May also contain a 'focus' dict. |
557 | 'blurhash': # The blurhash for the image, used for preview / placeholder generation | ||
557 | } | 558 | } |
558 | 559 | ||
559 | # Metadata dicts (image) - all fields are optional: | 560 | # Metadata dicts (image) - all fields are optional: |
@@ -1067,6 +1068,12 @@ These functions allow for convenient retrieval of paginated data. | |||
1067 | .. automethod:: Mastodon.fetch_previous | 1068 | .. automethod:: Mastodon.fetch_previous |
1068 | .. automethod:: Mastodon.fetch_remaining | 1069 | .. automethod:: Mastodon.fetch_remaining |
1069 | 1070 | ||
1071 | Blurhash decoding | ||
1072 | ----------------- | ||
1073 | This function allows for easy basic decoding of blurhash strings to images. | ||
1074 | |||
1075 | .. automethod:: Mastodon.decode_blurhash | ||
1076 | |||
1070 | Streaming | 1077 | Streaming |
1071 | --------- | 1078 | --------- |
1072 | These functions allow access to the streaming API. | 1079 | These functions allow access to the streaming API. |
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 181b0e7..174e798 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py | |||
@@ -25,6 +25,7 @@ from cryptography.hazmat.primitives.asymmetric import ec | |||
25 | import http_ece | 25 | import http_ece |
26 | import base64 | 26 | import base64 |
27 | import json | 27 | import json |
28 | import blurhash | ||
28 | 29 | ||
29 | try: | 30 | try: |
30 | from urllib.parse import urlparse | 31 | from urllib.parse import urlparse |
@@ -2346,7 +2347,42 @@ class Mastodon: | |||
2346 | ) | 2347 | ) |
2347 | 2348 | ||
2348 | return json.loads(decrypted.decode('utf-8'), object_hook = Mastodon.__json_hooks) | 2349 | return json.loads(decrypted.decode('utf-8'), object_hook = Mastodon.__json_hooks) |
2349 | 2350 | ||
2351 | ### | ||
2352 | # Blurhash utilities | ||
2353 | ### | ||
2354 | def decode_blurhash(self, media_dict, out_size = (16, 16), size_per_component = True, return_linear = True): | ||
2355 | """ | ||
2356 | Basic media-dict blurhash decoding. | ||
2357 | |||
2358 | out_size is the desired result size in pixels, either absolute or per blurhash | ||
2359 | component (this is the default). | ||
2360 | |||
2361 | By default, this function will return the image as linear RGB, ready for further | ||
2362 | scaling operations. If you want to display the image directly, set return_linear | ||
2363 | to False. | ||
2364 | |||
2365 | Returns the decoded blurhash image as a three-dimensional list: [height][width][3], | ||
2366 | with the last dimension being RGB colours. | ||
2367 | |||
2368 | For further info and tips for advanced usage, refer to the documentation for the | ||
2369 | blurhash module: https://github.com/halcy/blurhash-python | ||
2370 | """ | ||
2371 | # Figure out what size to decode to | ||
2372 | decode_components_x, decode_components_y = blurhash.components(media_dict["blurhash"]) | ||
2373 | if size_per_component == False: | ||
2374 | decode_size_x = out_size[0] | ||
2375 | decode_size_y = out_size[1] | ||
2376 | else: | ||
2377 | decode_size_x = decode_components_x * out_size[0] | ||
2378 | decode_size_y = decode_components_y * out_size[1] | ||
2379 | |||
2380 | # Decode | ||
2381 | decoded_image = blurhash.decode(media_dict["blurhash"], decode_size_x, decode_size_y, linear = return_linear) | ||
2382 | |||
2383 | # And that's pretty much it. | ||
2384 | return decoded_image | ||
2385 | |||
2350 | ### | 2386 | ### |
2351 | # Pagination | 2387 | # Pagination |
2352 | ### | 2388 | ### |
@@ -17,7 +17,8 @@ setup(name='Mastodon.py', | |||
17 | 'python-magic', | 17 | 'python-magic', |
18 | 'decorator>=4.0.0', | 18 | 'decorator>=4.0.0', |
19 | 'http_ece>=1.0.5', | 19 | 'http_ece>=1.0.5', |
20 | 'cryptography>=1.6.0' | 20 | 'cryptography>=1.6.0', |
21 | 'blurhash>=1.1.0', | ||
21 | ], | 22 | ], |
22 | tests_require=test_deps, | 23 | tests_require=test_deps, |
23 | extras_require=extras, | 24 | extras_require=extras, |
diff --git a/tests/test_push.py b/tests/test_push.py index 2ad215e..ad02cd8 100644 --- a/tests/test_push.py +++ b/tests/test_push.py | |||
@@ -66,4 +66,4 @@ def test_push_delete(api): | |||
66 | 66 | ||
67 | api.push_subscription_delete() | 67 | api.push_subscription_delete() |
68 | with pytest.raises(MastodonNotFoundError): | 68 | with pytest.raises(MastodonNotFoundError): |
69 | api.push_subscription() \ No newline at end of file | 69 | api.push_subscription() |