add thumbnail customization support

pull/257/head
halcy 2022-11-13 13:14:34 +02:00
rodzic 4218cf2930
commit fdb6e2a140
8 zmienionych plików z 71186 dodań i 70099 usunięć

Wyświetl plik

@ -10,12 +10,12 @@ Refer to mastodon changelog and API docs for details when implementing, add or m
----- -----
* [x] Add ability to exclude local content from federated timeline * [x] Add ability to exclude local content from federated timeline
* [x] Add ability to exclude remote content from hashtag timelines in web UI * [x] Add ability to exclude remote content from hashtag timelines in web UI
* [ ] Add invites_enabled attribute to GET /api/v1/instance in REST API * [x] Add invites_enabled attribute to GET /api/v1/instance in REST API
3.2.0 3.2.0
----- -----
* [ ] Add personal notes for accounts * [ ] Add personal notes for accounts
* [ ] Add customizable thumbnails for audio and video attachments * [x] Add customizable thumbnails for audio and video attachments
* [ ] Add color extraction for thumbnails * [ ] Add color extraction for thumbnails
3.3.0 3.3.0

Wyświetl plik

@ -19,6 +19,7 @@ import copy
import threading import threading
import sys import sys
import six import six
import uuid
from decorator import decorate from decorator import decorate
import hashlib import hashlib
@ -2481,16 +2482,14 @@ class Mastodon:
### ###
# Writing data: Media # Writing data: Media
### ###
@api_version("1.0.0", "3.1.4", __DICT_VERSION_MEDIA) @api_version("1.0.0", "3.2.0", __DICT_VERSION_MEDIA)
def media_post(self, media_file, mime_type=None, description=None, focus=None, file_name=None, synchronous=False): def media_post(self, media_file, mime_type=None, description=None, focus=None, file_name=None, thumbnail=None, thumbnail_mime_type=None, synchronous=False):
""" """
Post an image, video or audio file. `media_file` can either be image data or Post an image, video or audio file. `media_file` can either be data or
a file name. If image data is passed directly, the mime a file name. If data is passed directly, the mime type has to be specified
type has to be specified manually, otherwise, it is manually, otherwise, it is determined from the file name. `focus` should be a tuple
determined from the file name. `focus` should be a tuple of floats between -1 and 1, giving the x and y coordinates of the images
of floats between -1 and 1, giving the x and y coordinates focus point for cropping (with the origin being the images center).
of the images focus point for cropping (with the origin being the images
center).
Throws a `MastodonIllegalArgumentError` if the mime type of the Throws a `MastodonIllegalArgumentError` if the mime type of the
passed data or file can not be determined properly. passed data or file can not be determined properly.
@ -2498,6 +2497,10 @@ class Mastodon:
`file_name` can be specified to upload a file with the given name, `file_name` can be specified to upload a file with the given name,
which is ignored by Mastodon, but some other Fediverse server software which is ignored by Mastodon, but some other Fediverse server software
will display it. If no name is specified, a random name will be generated. will display it. If no name is specified, a random name will be generated.
The filename of a file specified in media_file will be ignored.
Starting with Mastodon 3.2.0, `thumbnail` can be specified in the same way as `media_file`
to upload a custom thumbnail image for audio and video files.
Returns a `media dict`_. This contains the id that can be used in Returns a `media dict`_. This contains the id that can be used in
status_post to attach the media file to a toot. status_post to attach the media file to a toot.
@ -2515,28 +2518,36 @@ class Mastodon:
media_file = open(media_file, 'rb') media_file = open(media_file, 'rb')
if mime_type is None: if mime_type is None:
raise MastodonIllegalArgumentError('Could not determine mime type' raise MastodonIllegalArgumentError('Could not determine mime type or data passed directly without mime type.')
' or data passed directly '
'without mime type.')
if file_name is None: if file_name is None:
random_suffix = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(10)) random_suffix = uuid.uuid4().hex
file_name = "mastodonpyupload_" + str(time.time()) + "_" + str(random_suffix) + mimetypes.guess_extension(mime_type) file_name = "mastodonpyupload_" + str(time.time()) + "_" + str(random_suffix) + mimetypes.guess_extension(mime_type)
if focus != None: if focus != None:
focus = str(focus[0]) + "," + str(focus[1]) focus = str(focus[0]) + "," + str(focus[1])
media_file_description = (file_name, media_file, mime_type) files = {'file': (file_name, media_file, mime_type)}
if not thumbnail is None:
if not self.verify_minimum_version("3.2.0"):
raise MastodonVersionError('Thumbnail requires version > 3.2.0')
if isinstance(thumbnail, str) and os.path.isfile(thumbnail):
thumbnail_mime_type = guess_type(thumbnail)
thumbnail = open(thumbnail, 'rb')
elif isinstance(thumbnail, str) and os.path.isfile(thumbnail):
thumbnail = open(thumbnail, 'rb')
if thumbnail_mime_type is None:
raise MastodonIllegalArgumentError('Could not determine mime type or data passed directly without mime type.')
files["thumbnail"] = ("thumb" + mimetypes.guess_extension(mime_type), thumbnail, thumbnail_mime_type)
# Disambiguate URL by version # Disambiguate URL by version
if self.verify_minimum_version("3.1.4"): if self.verify_minimum_version("3.1.4"):
ret_dict = self.__api_request('POST', '/api/v2/media', ret_dict = self.__api_request('POST', '/api/v2/media', files = files, params={'description': description, 'focus': focus})
files={'file': media_file_description},
params={'description': description, 'focus': focus})
else: else:
ret_dict = self.__api_request('POST', '/api/v1/media', ret_dict = self.__api_request('POST', '/api/v1/media', files = files, params={'description': description, 'focus': focus})
files={'file': media_file_description},
params={'description': description, 'focus': focus})
# Wait for processing? # Wait for processing?
if synchronous: if synchronous:

Wyświetl plik

@ -34,7 +34,7 @@ It also needs various things to be set up for it. The following command should d
RAILS_ENV=development bin/tootctl accounts create admin2 --email zerocool@example.com --confirmed --role Owner && \ RAILS_ENV=development bin/tootctl accounts create admin2 --email zerocool@example.com --confirmed --role Owner && \
RAILS_ENV=development bin/tootctl accounts create mastodonpy_test --email mastodonpy_test@localhost:3000 --confirmed && \ RAILS_ENV=development bin/tootctl accounts create mastodonpy_test --email mastodonpy_test@localhost:3000 --confirmed && \
RAILS_ENV=development bin/tootctl accounts create mastodonpy_test_2 --email mastodonpy_test_2@localhost:3000 --confirmed && \ RAILS_ENV=development bin/tootctl accounts create mastodonpy_test_2 --email mastodonpy_test_2@localhost:3000 --confirmed && \
sql -d mastodon_development < ~/masto/Mastodon.py/tests/setup.sql && sleep 4 && \ psql -d mastodon_development < ~/masto/Mastodon.py/tests/setup.sql && sleep 4 && \
RAILS_ENV=development DB_PASS="" foreman start RAILS_ENV=development DB_PASS="" foreman start
You _may_ additionally have to set up a database password and pass it as DB_PASS for the streaming tests to function. You _may_ additionally have to set up a database password and pass it as DB_PASS for the streaming tests to function.

Wyświetl plik

@ -33,9 +33,11 @@ def test_media_post_v1(api):
@pytest.mark.parametrize('sensitive', (False, True)) @pytest.mark.parametrize('sensitive', (False, True))
def test_media_post(api, sensitive): def test_media_post(api, sensitive):
media = api.media_post( media = api.media_post(
'tests/video.mp4', 'tests/video.mp4',
description="me when a cat", description="me when a cat",
focus=(-0.5, 0.3)) focus=(-0.5, 0.3),
thumbnail='tests/amewatson.jpg'
)
assert media assert media
assert media.url is None assert media.url is None
@ -46,10 +48,10 @@ def test_media_post(api, sensitive):
assert not media2.url is None assert not media2.url is None
status = api.status_post( status = api.status_post(
'LOL check this out', 'LOL check this out',
media_ids=media2, media_ids=media2,
sensitive=sensitive sensitive=sensitive
) )
assert status assert status