From 087c49b1993114b1cd2f4b130999197e28cc6cc1 Mon Sep 17 00:00:00 2001 From: Lorenz Diener Date: Tue, 17 Apr 2018 17:21:16 +0200 Subject: Small fix to account_search --- mastodon/Mastodon.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'mastodon') diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 5f1edb3..589d487 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py @@ -687,10 +687,11 @@ class Mastodon: params) @api_version("1.0.0", "2.1.0") - def account_search(self, q, limit=None): + def account_search(self, q, limit=None. following=False): """ Fetch matching accounts. Will lookup an account remotely if the search term is - in the username@domain format and not yet in the database. + in the username@domain format and not yet in the database. Set `following` to + True to limit the search to users the logged-in user follows. Returns a list of `user dicts`_. """ -- cgit v1.2.3 From 78d3b3a5dff7fb465a21fbade09e972e9d8f0fbf Mon Sep 17 00:00:00 2001 From: Lorenz Diener Date: Tue, 17 Apr 2018 17:24:46 +0200 Subject: Fix for the fix --- mastodon/Mastodon.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'mastodon') diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 589d487..1056d7a 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py @@ -687,7 +687,7 @@ class Mastodon: params) @api_version("1.0.0", "2.1.0") - def account_search(self, q, limit=None. following=False): + def account_search(self, q, limit=None, following=False): """ Fetch matching accounts. Will lookup an account remotely if the search term is in the username@domain format and not yet in the database. Set `following` to -- cgit v1.2.3 From 5f44998dd201beb871f06c0813b3dc69d387f7da Mon Sep 17 00:00:00 2001 From: Lorenz Diener Date: Tue, 17 Apr 2018 17:49:08 +0200 Subject: First steps to 2.3.0 support --- mastodon/Mastodon.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'mastodon') diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 1056d7a..5a32be0 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py @@ -1143,9 +1143,9 @@ class Mastodon: url = '/api/v1/accounts/{0}/unmute'.format(str(id)) return self.__api_request('POST', url) - @api_version("1.1.1", "2.1.0") + @api_version("1.1.1", "2.3.0") def account_update_credentials(self, display_name=None, note=None, - avatar=None, header=None): + avatar=None, header=None, locked=None): """ Update the profile for the currently logged-in user. @@ -1154,6 +1154,8 @@ class Mastodon: 'avatar' and 'header' are images encoded in base64, prepended by a content-type (for example: 'data:image/png;base64,iVBORw0KGgoAAAA[...]') + 'locked' specifies whether the user needs to manually approve follow requests. + Returns the updated `user dict` of the logged-in user. """ params = self.__generate_params(locals()) -- cgit v1.2.3 From a1ecd2d6641979f011afeeda4d8ccdf2b2b6b3c3 Mon Sep 17 00:00:00 2001 From: Lorenz Diener Date: Tue, 17 Apr 2018 18:02:55 +0200 Subject: Raise last changed version on instance() --- mastodon/Mastodon.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'mastodon') diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 5a32be0..4357975 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py @@ -346,7 +346,7 @@ class Mastodon: ### # Reading data: Instances ### - @api_version("1.1.0", "1.4.2") + @api_version("1.1.0", "2.3.0") def instance(self): """ Retrieve basic information about the instance, including the URI and administrative contact email. -- cgit v1.2.3 From a825905b571697835d762ac63098491cbc4a0936 Mon Sep 17 00:00:00 2001 From: Lorenz Diener Date: Wed, 18 Apr 2018 21:56:36 +0200 Subject: Fix several bugs --- mastodon/Mastodon.py | 60 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 12 deletions(-) (limited to 'mastodon') diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 4357975..1cbb750 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py @@ -90,7 +90,7 @@ class Mastodon: __DEFAULT_BASE_URL = 'https://mastodon.social' __DEFAULT_TIMEOUT = 300 __DEFAULT_STREAM_RECONNECT_WAIT_SEC = 5 - __SUPPORTED_MASTODON_VERSION = "2.2.0" + __SUPPORTED_MASTODON_VERSION = "2.3.0" ### # Registering apps @@ -892,7 +892,7 @@ class Mastodon: ### @api_version("1.0.0", "2.0.0") def status_post(self, status, in_reply_to_id=None, media_ids=None, - sensitive=False, visibility='', spoiler_text=None): + sensitive=False, visibility=None, spoiler_text=None): """ Post a status. Can optionally be in reply to another status and contain media. @@ -930,11 +930,14 @@ class Mastodon: params_initial = locals() # Validate visibility parameter - valid_visibilities = ['private', 'public', 'unlisted', 'direct', ''] - params_initial['visibility'] = params_initial['visibility'].lower() - if params_initial['visibility'] not in valid_visibilities: - raise ValueError('Invalid visibility value! Acceptable ' - 'values are %s' % valid_visibilities) + valid_visibilities = ['private', 'public', 'unlisted', 'direct'] + if params_initial['visibility'] == None: + del params_initial['visibility'] + else: + params_initial['visibility'] = params_initial['visibility'].lower() + if params_initial['visibility'] not in valid_visibilities: + raise ValueError('Invalid visibility value! Acceptable ' + 'values are %s' % valid_visibilities) if params_initial['sensitive'] is False: del [params_initial['sensitive']] @@ -1145,21 +1148,54 @@ class Mastodon: @api_version("1.1.1", "2.3.0") def account_update_credentials(self, display_name=None, note=None, - avatar=None, header=None, locked=None): + avatar=None, avatar_mime_type=None, + header=None, header_mime_type=None, locked=None): """ Update the profile for the currently logged-in user. 'note' is the user's bio. - 'avatar' and 'header' are images encoded in base64, prepended by a content-type - (for example: 'data:image/png;base64,iVBORw0KGgoAAAA[...]') + 'avatar' and 'header' are images. As with media uploads, it is possible to either + pass image data and a mime type, or a filename of an image file, for either. 'locked' specifies whether the user needs to manually approve follow requests. Returns the updated `user dict` of the logged-in user. """ - params = self.__generate_params(locals()) - return self.__api_request('PATCH', '/api/v1/accounts/update_credentials', params) + params_initial = locals() + + # Load avatar, if specified + if avatar_mime_type is None and os.path.isfile(avatar): + avatar_mime_type = mimetypes.guess_type(avatar)[0] + avatar = open(avatar, 'rb') + + if (not avatar is None and avatar_mime_type is None): + raise MastodonIllegalArgumentError('Could not determine mime type or data passed directly without mime type.') + + # Load header, if specified + if header_mime_type is None and os.path.isfile(header): + header_mime_type = mimetypes.guess_type(header)[0] + header = open(header, 'rb') + + if (not header is None and header_mime_type is None): + raise MastodonIllegalArgumentError('Could not determine mime type or data passed directly without mime type.') + + # Clean up params + for param in ["avatar", "avatar_mime_type", "header", "header_mime_type"]: + if param in params_initial: + del params_initial[param] + + # Create file info + files = {} + if not avatar is None: + avatar_file_name = "mastodonpyupload_" + mimetypes.guess_extension(avatar_mime_type) + files["avatar"] = (avatar_file_name, avatar, avatar_mime_type) + if not header is None: + header_file_name = "mastodonpyupload_" + mimetypes.guess_extension(avatar_mime_type) + files["header"] = (header_file_name, header, header_mime_type) + + params = self.__generate_params(params_initial) + return self.__api_request('PATCH', '/api/v1/accounts/update_credentials', params, files=files) ### # Writing data: Lists -- cgit v1.2.3 From ad6b8eeb044760adf4b420199164b245012350f4 Mon Sep 17 00:00:00 2001 From: Lorenz Diener Date: Sun, 6 May 2018 01:36:48 +0200 Subject: Switch to using requests Session objects --- mastodon/Mastodon.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'mastodon') diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 1cbb750..fd44d65 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py @@ -194,6 +194,8 @@ class Mastodon: self.request_timeout = request_timeout + self.session = requests.Session() + # Versioning if mastodon_version == None: self.retrieve_mastodon_version() @@ -1585,7 +1587,7 @@ class Mastodon: else: kwargs['data'] = params - response_object = requests.request( + response_object = self.session.request( method, self.api_base_url + endpoint, **kwargs) except Exception as e: raise MastodonNetworkError("Could not complete request: %s" % e) @@ -1739,8 +1741,7 @@ class Mastodon: # Connect function (called and then potentially passed to async handler) def connect_func(): headers = {"Authorization": "Bearer " + self.access_token} - connection = requests.get(url + endpoint, headers = headers, data = params, stream = True) - + connection = self.session.get(url + endpoint, headers = headers, data = params, stream = True) if connection.status_code != 200: raise MastodonNetworkError("Could not connect to streaming server: %s" % connection.reason) return connection -- cgit v1.2.3 From 4088e16dec90adfcf0352bb3242ba8557f6b73c6 Mon Sep 17 00:00:00 2001 From: Lorenz Diener Date: Sun, 6 May 2018 01:45:02 +0200 Subject: Fix daemonization in python 2 --- mastodon/Mastodon.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'mastodon') diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index fd44d65..a2ff65f 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py @@ -1807,7 +1807,8 @@ class Mastodon: if run_async: handle = __stream_handle(connection, connect_func, reconnect_async, reconnect_async_wait_sec) - t = threading.Thread(args=(), daemon = True, target=handle._threadproc) + t = threading.Thread(args=(), target=handle._threadproc) + t.daemon = True t.start() return handle else: -- cgit v1.2.3 From ba2fccc1cf0957387f616da64c20e329eba95263 Mon Sep 17 00:00:00 2001 From: Lorenz Diener Date: Sun, 6 May 2018 15:42:38 +0200 Subject: Add lists tests and fix a bug --- mastodon/Mastodon.py | 1 + 1 file changed, 1 insertion(+) (limited to 'mastodon') diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index a2ff65f..6ab9291 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py @@ -708,6 +708,7 @@ class Mastodon: Returns a list of `list dicts`_. """ + id = self.__unpack_id(id) params = self.__generate_params(locals(), ['id']) url = '/api/v1/accounts/{0}/lists'.format(str(id)) return self.__api_request('GET', url, params) -- cgit v1.2.3