aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhalcy <halcy@ARARAGI-KUN>2022-11-13 14:06:50 +0200
committerhalcy <halcy@ARARAGI-KUN>2022-11-13 14:06:50 +0200
commitadcddefd59de91b6a2ca981c9bd923551ac38694 (patch)
tree8a495fc2ed542e86d3bf859bc3b73fdcf8d5d6aa /mastodon
parentc670ed8ce26749442c180dbf8a0a60c153f5d5c4 (diff)
downloadmastodon.py-adcddefd59de91b6a2ca981c9bd923551ac38694.tar.gz
clean up media loading, add thumbnail to media_update
Diffstat (limited to 'mastodon')
-rw-r--r--mastodon/Mastodon.py102
1 files changed, 41 insertions, 61 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py
index 6340abc..ff542ef 100644
--- a/mastodon/Mastodon.py
+++ b/mastodon/Mastodon.py
@@ -2212,24 +2212,6 @@ class Mastodon:
2212 """ 2212 """
2213 params_initial = collections.OrderedDict(locals()) 2213 params_initial = collections.OrderedDict(locals())
2214 2214
2215 # Load avatar, if specified
2216 if not avatar is None:
2217 if avatar_mime_type is None and (isinstance(avatar, str) and os.path.isfile(avatar)):
2218 avatar_mime_type = guess_type(avatar)
2219 avatar = open(avatar, 'rb')
2220
2221 if avatar_mime_type is None:
2222 raise MastodonIllegalArgumentError('Could not determine mime type or data passed directly without mime type.')
2223
2224 # Load header, if specified
2225 if not header is None:
2226 if header_mime_type is None and (isinstance(header, str) and os.path.isfile(header)):
2227 header_mime_type = guess_type(header)
2228 header = open(header, 'rb')
2229
2230 if header_mime_type is None:
2231 raise MastodonIllegalArgumentError('Could not determine mime type or data passed directly without mime type.')
2232
2233 # Convert fields 2215 # Convert fields
2234 if fields != None: 2216 if fields != None:
2235 if len(fields) > 4: 2217 if len(fields) > 4:
@@ -2248,11 +2230,9 @@ class Mastodon:
2248 # Create file info 2230 # Create file info
2249 files = {} 2231 files = {}
2250 if not avatar is None: 2232 if not avatar is None:
2251 avatar_file_name = "mastodonpyupload_" + mimetypes.guess_extension(avatar_mime_type) 2233 files["avatar"] = self.__load_media_file(avatar, avatar_mime_type)
2252 files["avatar"] = (avatar_file_name, avatar, avatar_mime_type)
2253 if not header is None: 2234 if not header is None:
2254 header_file_name = "mastodonpyupload_" + mimetypes.guess_extension(header_mime_type) 2235 files["header"] = self.__load_media_file(header, header_mime_type)
2255 files["header"] = (header_file_name, header, header_mime_type)
2256 2236
2257 params = self.__generate_params(params_initial) 2237 params = self.__generate_params(params_initial)
2258 return self.__api_request('PATCH', '/api/v1/accounts/update_credentials', params, files=files) 2238 return self.__api_request('PATCH', '/api/v1/accounts/update_credentials', params, files=files)
@@ -2511,37 +2491,15 @@ class Mastodon:
2511 "synchronous" to emulate the old behaviour. Not recommended, inefficient 2491 "synchronous" to emulate the old behaviour. Not recommended, inefficient
2512 and deprecated, you know the deal. 2492 and deprecated, you know the deal.
2513 """ 2493 """
2514 if mime_type is None and (isinstance(media_file, str) and os.path.isfile(media_file)): 2494 files = {'file': self.__load_media_file(media_file, mime_type, file_name)}
2515 mime_type = guess_type(media_file)
2516 media_file = open(media_file, 'rb')
2517 elif isinstance(media_file, str) and os.path.isfile(media_file):
2518 media_file = open(media_file, 'rb')
2519
2520 if mime_type is None:
2521 raise MastodonIllegalArgumentError('Could not determine mime type or data passed directly without mime type.')
2522
2523 if file_name is None:
2524 random_suffix = uuid.uuid4().hex
2525 file_name = "mastodonpyupload_" + str(time.time()) + "_" + str(random_suffix) + mimetypes.guess_extension(mime_type)
2526 2495
2527 if focus != None: 2496 if focus != None:
2528 focus = str(focus[0]) + "," + str(focus[1]) 2497 focus = str(focus[0]) + "," + str(focus[1])
2529
2530 files = {'file': (file_name, media_file, mime_type)}
2531 2498
2532 if not thumbnail is None: 2499 if not thumbnail is None:
2533 if not self.verify_minimum_version("3.2.0"): 2500 if not self.verify_minimum_version("3.2.0"):
2534 raise MastodonVersionError('Thumbnail requires version > 3.2.0') 2501 raise MastodonVersionError('Thumbnail requires version > 3.2.0')
2535 if isinstance(thumbnail, str) and os.path.isfile(thumbnail): 2502 files["thumbnail"] = self.__load_media_file(thumbnail, thumbnail_mime_type)
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 2503
2546 # Disambiguate URL by version 2504 # Disambiguate URL by version
2547 if self.verify_minimum_version("3.1.4"): 2505 if self.verify_minimum_version("3.1.4"):
@@ -2564,11 +2522,11 @@ class Mastodon:
2564 2522
2565 return ret_dict 2523 return ret_dict
2566 2524
2567 @api_version("2.3.0", "2.3.0", __DICT_VERSION_MEDIA) 2525 @api_version("2.3.0", "3.2.0", __DICT_VERSION_MEDIA)
2568 def media_update(self, id, description=None, focus=None): 2526 def media_update(self, id, description=None, focus=None, thumbnail=None, thumbnail_mime_type=None):
2569 """ 2527 """
2570 Update the metadata of the media file with the given `id`. `description` and 2528 Update the metadata of the media file with the given `id`. `description` and
2571 `focus` are as in `media_post()`_ . 2529 `focus` and `thumbnail` are as in `media_post()`_ .
2572 2530
2573 Returns the updated `media dict`_. 2531 Returns the updated `media dict`_.
2574 """ 2532 """
@@ -2576,9 +2534,16 @@ class Mastodon:
2576 2534
2577 if focus != None: 2535 if focus != None:
2578 focus = str(focus[0]) + "," + str(focus[1]) 2536 focus = str(focus[0]) + "," + str(focus[1])
2579 2537
2580 params = self.__generate_params(locals(), ['id']) 2538 params = self.__generate_params(locals(), ['id', 'thumbnail', 'thumbnail_mime_type'])
2581 return self.__api_request('PUT', '/api/v1/media/{0}'.format(str(id)), params) 2539
2540 if not thumbnail is None:
2541 if not self.verify_minimum_version("3.2.0"):
2542 raise MastodonVersionError('Thumbnail requires version > 3.2.0')
2543 files = {"thumbnail": self.__load_media_file(thumbnail, thumbnail_mime_type)}
2544 return self.__api_request('PUT', '/api/v1/media/{0}'.format(str(id)), params, files = files)
2545 else:
2546 return self.__api_request('PUT', '/api/v1/media/{0}'.format(str(id)), params)
2582 2547
2583 @api_version("3.1.4", "3.1.4", __DICT_VERSION_MEDIA) 2548 @api_version("3.1.4", "3.1.4", __DICT_VERSION_MEDIA)
2584 def media(self, id): 2549 def media(self, id):
@@ -3823,7 +3788,29 @@ class Mastodon:
3823 """Internal helper for oauth code""" 3788 """Internal helper for oauth code"""
3824 self._refresh_token = value 3789 self._refresh_token = value
3825 return 3790 return
3826 3791
3792 def __guess_type(self, media_file):
3793 """Internal helper to guess media file type"""
3794 mime_type = None
3795 try:
3796 mime_type = magic.from_file(media_file, mime=True)
3797 except AttributeError:
3798 mime_type = mimetypes.guess_type(media_file)[0]
3799 return mime_type
3800
3801 def __load_media_file(self, media_file, mime_type = None, file_name = None):
3802 if isinstance(media_file, str) and os.path.isfile(media_file):
3803 mime_type = self.__guess_type(media_file)
3804 media_file = open(media_file, 'rb')
3805 elif isinstance(media_file, str) and os.path.isfile(media_file):
3806 media_file = open(media_file, 'rb')
3807 if mime_type is None:
3808 raise MastodonIllegalArgumentError('Could not determine mime type or data passed directly without mime type.')
3809 if file_name is None:
3810 random_suffix = uuid.uuid4().hex
3811 file_name = "mastodonpyupload_" + str(time.time()) + "_" + str(random_suffix) + mimetypes.guess_extension(mime_type)
3812 return (file_name, media_file, mime_type)
3813
3827 @staticmethod 3814 @staticmethod
3828 def __protocolize(base_url): 3815 def __protocolize(base_url):
3829 """Internal add-protocol-to-url helper""" 3816 """Internal add-protocol-to-url helper"""
@@ -3913,10 +3900,3 @@ class MastodonMalformedEventError(MastodonError):
3913 """Raised when the server-sent event stream is malformed""" 3900 """Raised when the server-sent event stream is malformed"""
3914 pass 3901 pass
3915 3902
3916def guess_type(media_file):
3917 mime_type = None
3918 try:
3919 mime_type = magic.from_file(media_file, mime=True)
3920 except AttributeError:
3921 mime_type = mimetypes.guess_type(media_file)[0]
3922 return mime_type
Powered by cgit v1.2.3 (git 2.41.0)