diff options
author | halcy <halcy@ARARAGI-KUN> | 2022-11-13 13:14:34 +0200 |
---|---|---|
committer | halcy <halcy@ARARAGI-KUN> | 2022-11-13 13:14:34 +0200 |
commit | fdb6e2a140c4f103602e04d782dfbd849e2de788 (patch) | |
tree | ea29347bbf60c8decbf68406e18382a6fa9b4ddb /mastodon | |
parent | 4218cf29301f6819c986fb9b7d6af810b12fa2d1 (diff) | |
download | mastodon.py-fdb6e2a140c4f103602e04d782dfbd849e2de788.tar.gz |
add thumbnail customization support
Diffstat (limited to 'mastodon')
-rw-r--r-- | mastodon/Mastodon.py | 53 |
1 files changed, 32 insertions, 21 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index d502597..dd30e17 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py | |||
@@ -19,6 +19,7 @@ import copy | |||
19 | import threading | 19 | import threading |
20 | import sys | 20 | import sys |
21 | import six | 21 | import six |
22 | import uuid | ||
22 | from decorator import decorate | 23 | from decorator import decorate |
23 | import hashlib | 24 | import hashlib |
24 | 25 | ||
@@ -2481,16 +2482,14 @@ class Mastodon: | |||
2481 | ### | 2482 | ### |
2482 | # Writing data: Media | 2483 | # Writing data: Media |
2483 | ### | 2484 | ### |
2484 | @api_version("1.0.0", "3.1.4", __DICT_VERSION_MEDIA) | 2485 | @api_version("1.0.0", "3.2.0", __DICT_VERSION_MEDIA) |
2485 | def media_post(self, media_file, mime_type=None, description=None, focus=None, file_name=None, synchronous=False): | 2486 | def media_post(self, media_file, mime_type=None, description=None, focus=None, file_name=None, thumbnail=None, thumbnail_mime_type=None, synchronous=False): |
2486 | """ | 2487 | """ |
2487 | Post an image, video or audio file. `media_file` can either be image data or | 2488 | Post an image, video or audio file. `media_file` can either be data or |
2488 | a file name. If image data is passed directly, the mime | 2489 | a file name. If data is passed directly, the mime type has to be specified |
2489 | type has to be specified manually, otherwise, it is | 2490 | manually, otherwise, it is determined from the file name. `focus` should be a tuple |
2490 | determined from the file name. `focus` should be a tuple | 2491 | of floats between -1 and 1, giving the x and y coordinates of the images |
2491 | of floats between -1 and 1, giving the x and y coordinates | 2492 | focus point for cropping (with the origin being the images center). |
2492 | of the images focus point for cropping (with the origin being the images | ||
2493 | center). | ||
2494 | 2493 | ||
2495 | Throws a `MastodonIllegalArgumentError` if the mime type of the | 2494 | Throws a `MastodonIllegalArgumentError` if the mime type of the |
2496 | passed data or file can not be determined properly. | 2495 | passed data or file can not be determined properly. |
@@ -2498,6 +2497,10 @@ class Mastodon: | |||
2498 | `file_name` can be specified to upload a file with the given name, | 2497 | `file_name` can be specified to upload a file with the given name, |
2499 | which is ignored by Mastodon, but some other Fediverse server software | 2498 | which is ignored by Mastodon, but some other Fediverse server software |
2500 | will display it. If no name is specified, a random name will be generated. | 2499 | will display it. If no name is specified, a random name will be generated. |
2500 | The filename of a file specified in media_file will be ignored. | ||
2501 | |||
2502 | Starting with Mastodon 3.2.0, `thumbnail` can be specified in the same way as `media_file` | ||
2503 | to upload a custom thumbnail image for audio and video files. | ||
2501 | 2504 | ||
2502 | Returns a `media dict`_. This contains the id that can be used in | 2505 | Returns a `media dict`_. This contains the id that can be used in |
2503 | status_post to attach the media file to a toot. | 2506 | status_post to attach the media file to a toot. |
@@ -2515,28 +2518,36 @@ class Mastodon: | |||
2515 | media_file = open(media_file, 'rb') | 2518 | media_file = open(media_file, 'rb') |
2516 | 2519 | ||
2517 | if mime_type is None: | 2520 | if mime_type is None: |
2518 | raise MastodonIllegalArgumentError('Could not determine mime type' | 2521 | raise MastodonIllegalArgumentError('Could not determine mime type or data passed directly without mime type.') |
2519 | ' or data passed directly ' | ||
2520 | 'without mime type.') | ||
2521 | 2522 | ||
2522 | if file_name is None: | 2523 | if file_name is None: |
2523 | random_suffix = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(10)) | 2524 | random_suffix = uuid.uuid4().hex |
2524 | file_name = "mastodonpyupload_" + str(time.time()) + "_" + str(random_suffix) + mimetypes.guess_extension(mime_type) | 2525 | file_name = "mastodonpyupload_" + str(time.time()) + "_" + str(random_suffix) + mimetypes.guess_extension(mime_type) |
2525 | 2526 | ||
2526 | if focus != None: | 2527 | if focus != None: |
2527 | focus = str(focus[0]) + "," + str(focus[1]) | 2528 | focus = str(focus[0]) + "," + str(focus[1]) |
2528 | 2529 | ||
2529 | media_file_description = (file_name, media_file, mime_type) | 2530 | files = {'file': (file_name, media_file, mime_type)} |
2530 | 2531 | ||
2532 | if not thumbnail is None: | ||
2533 | if not self.verify_minimum_version("3.2.0"): | ||
2534 | raise MastodonVersionError('Thumbnail requires version > 3.2.0') | ||
2535 | if isinstance(thumbnail, str) and os.path.isfile(thumbnail): | ||
2536 | thumbnail_mime_type = guess_type(thumbnail) | ||
2537 | thumbnail = open(thumbnail, 'rb') | ||
2538 | elif isinstance(thumbnail, str) and os.path.isfile(thumbnail): | ||
2539 | thumbnail = open(thumbnail, 'rb') | ||
2540 | |||
2541 | if thumbnail_mime_type is None: | ||
2542 | raise MastodonIllegalArgumentError('Could not determine mime type or data passed directly without mime type.') | ||
2543 | |||
2544 | files["thumbnail"] = ("thumb" + mimetypes.guess_extension(mime_type), thumbnail, thumbnail_mime_type) | ||
2545 | |||
2531 | # Disambiguate URL by version | 2546 | # Disambiguate URL by version |
2532 | if self.verify_minimum_version("3.1.4"): | 2547 | if self.verify_minimum_version("3.1.4"): |
2533 | ret_dict = self.__api_request('POST', '/api/v2/media', | 2548 | ret_dict = self.__api_request('POST', '/api/v2/media', files = files, params={'description': description, 'focus': focus}) |
2534 | files={'file': media_file_description}, | ||
2535 | params={'description': description, 'focus': focus}) | ||
2536 | else: | 2549 | else: |
2537 | ret_dict = self.__api_request('POST', '/api/v1/media', | 2550 | ret_dict = self.__api_request('POST', '/api/v1/media', files = files, params={'description': description, 'focus': focus}) |
2538 | files={'file': media_file_description}, | ||
2539 | params={'description': description, 'focus': focus}) | ||
2540 | 2551 | ||
2541 | # Wait for processing? | 2552 | # Wait for processing? |
2542 | if synchronous: | 2553 | if synchronous: |