diff options
Diffstat (limited to 'mastodon/media.py')
-rw-r--r-- | mastodon/media.py | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/mastodon/media.py b/mastodon/media.py new file mode 100644 index 0000000..3c815fb --- /dev/null +++ b/mastodon/media.py | |||
@@ -0,0 +1,114 @@ | |||
1 | # admin.py - admin / moderation endpoints | ||
2 | |||
3 | import time | ||
4 | |||
5 | from .versions import _DICT_VERSION_MEDIA | ||
6 | from .errors import MastodonVersionError, MastodonAPIError | ||
7 | from .utility import api_version | ||
8 | |||
9 | from .internals import Mastodon as Internals | ||
10 | |||
11 | class Mastodon(Internals): | ||
12 | ### | ||
13 | # Reading data: Media | ||
14 | ### | ||
15 | @api_version("3.1.4", "3.1.4", _DICT_VERSION_MEDIA) | ||
16 | def media(self, id): | ||
17 | """ | ||
18 | Get the updated JSON for one non-attached / in progress media upload belonging | ||
19 | to the logged-in user. | ||
20 | """ | ||
21 | id = self.__unpack_id(id) | ||
22 | return self.__api_request('GET', '/api/v1/media/{0}'.format(str(id))) | ||
23 | |||
24 | ### | ||
25 | # Writing data: Media | ||
26 | ### | ||
27 | @api_version("1.0.0", "3.2.0", _DICT_VERSION_MEDIA) | ||
28 | def media_post(self, media_file, mime_type=None, description=None, focus=None, file_name=None, thumbnail=None, thumbnail_mime_type=None, synchronous=False): | ||
29 | """ | ||
30 | Post an image, video or audio file. `media_file` can either be data or | ||
31 | a file name. If data is passed directly, the mime type has to be specified | ||
32 | manually, otherwise, it is determined from the file name. `focus` should be a tuple | ||
33 | of floats between -1 and 1, giving the x and y coordinates of the images | ||
34 | focus point for cropping (with the origin being the images center). | ||
35 | |||
36 | Throws a `MastodonIllegalArgumentError` if the mime type of the | ||
37 | passed data or file can not be determined properly. | ||
38 | |||
39 | `file_name` can be specified to upload a file with the given name, | ||
40 | which is ignored by Mastodon, but some other Fediverse server software | ||
41 | will display it. If no name is specified, a random name will be generated. | ||
42 | The filename of a file specified in media_file will be ignored. | ||
43 | |||
44 | Starting with Mastodon 3.2.0, `thumbnail` can be specified in the same way as `media_file` | ||
45 | to upload a custom thumbnail image for audio and video files. | ||
46 | |||
47 | Returns a :ref:`media dict <media dict>`. This contains the id that can be used in | ||
48 | status_post to attach the media file to a toot. | ||
49 | |||
50 | When using the v2 API (post Mastodon version 3.1.4), the `url` in the | ||
51 | returned dict will be `null`, since attachments are processed | ||
52 | asynchronously. You can fetch an updated dict using `media`. Pass | ||
53 | "synchronous" to emulate the old behaviour. Not recommended, inefficient | ||
54 | and deprecated, will eat your API quota, you know the deal. | ||
55 | """ | ||
56 | files = {'file': self.__load_media_file( | ||
57 | media_file, mime_type, file_name)} | ||
58 | |||
59 | if focus is not None: | ||
60 | focus = str(focus[0]) + "," + str(focus[1]) | ||
61 | |||
62 | if thumbnail is not None: | ||
63 | if not self.verify_minimum_version("3.2.0", cached=True): | ||
64 | raise MastodonVersionError('Thumbnail requires version > 3.2.0') | ||
65 | files["thumbnail"] = self.__load_media_file( | ||
66 | thumbnail, thumbnail_mime_type) | ||
67 | |||
68 | # Disambiguate URL by version | ||
69 | if self.verify_minimum_version("3.1.4", cached=True): | ||
70 | ret_dict = self.__api_request( | ||
71 | 'POST', '/api/v2/media', files=files, params={'description': description, 'focus': focus}) | ||
72 | else: | ||
73 | ret_dict = self.__api_request( | ||
74 | 'POST', '/api/v1/media', files=files, params={'description': description, 'focus': focus}) | ||
75 | |||
76 | # Wait for processing? | ||
77 | if synchronous: | ||
78 | if self.verify_minimum_version("3.1.4"): | ||
79 | while not "url" in ret_dict or ret_dict.url is None: | ||
80 | try: | ||
81 | ret_dict = self.media(ret_dict) | ||
82 | time.sleep(5.0) | ||
83 | except: | ||
84 | raise MastodonAPIError("Attachment could not be processed") | ||
85 | else: | ||
86 | # Old version always waits | ||
87 | return ret_dict | ||
88 | |||
89 | return ret_dict | ||
90 | |||
91 | @api_version("2.3.0", "3.2.0", _DICT_VERSION_MEDIA) | ||
92 | def media_update(self, id, description=None, focus=None, thumbnail=None, thumbnail_mime_type=None): | ||
93 | """ | ||
94 | Update the metadata of the media file with the given `id`. `description` and | ||
95 | `focus` and `thumbnail` are as in :ref:`media_post() <media_post()>` . | ||
96 | |||
97 | Returns the updated :ref:`media dict <media dict>`. | ||
98 | """ | ||
99 | id = self.__unpack_id(id) | ||
100 | |||
101 | if focus is not None: | ||
102 | focus = str(focus[0]) + "," + str(focus[1]) | ||
103 | |||
104 | params = self.__generate_params( | ||
105 | locals(), ['id', 'thumbnail', 'thumbnail_mime_type']) | ||
106 | |||
107 | if thumbnail is not None: | ||
108 | if not self.verify_minimum_version("3.2.0", cached=True): | ||
109 | raise MastodonVersionError('Thumbnail requires version > 3.2.0') | ||
110 | files = {"thumbnail": self.__load_media_file( | ||
111 | thumbnail, thumbnail_mime_type)} | ||
112 | return self.__api_request('PUT', '/api/v1/media/{0}'.format(str(id)), params, files=files) | ||
113 | else: | ||
114 | return self.__api_request('PUT', '/api/v1/media/{0}'.format(str(id)), params) | ||