From 911fcc733cb60694e59f4c2bdf61e66219cd1508 Mon Sep 17 00:00:00 2001 From: Alex McGivern Date: Wed, 26 Apr 2017 12:59:49 +0100 Subject: added calls for fetching instance data, status cards, filing reports, and updating the user profile --- mastodon/Mastodon.py | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) (limited to 'mastodon') diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 4717674..919825a 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py @@ -213,6 +213,17 @@ class Mastodon: return response['access_token'] + ### + # Reading data: Instance + ### + def instance(self): + """ + Retrieve basic information about the instance, including the URI and administrative contact email. + + Returns a dict. + """ + return self.__api_request('GET', '/api/v1/instance/') + ### # Reading data: Timelines ## @@ -277,6 +288,14 @@ class Mastodon: """ return self.__api_request('GET', '/api/v1/statuses/' + str(id)) + def status_card(self, id): + """ + Fetch a card associated with a status. + + Returns a card dict. + """ + return self.__api_request('GET', '/api/v1/statuses/' + str(id) + '/card') + def status_context(self, id): """ Fetch information about ancestors and descendants of a toot. @@ -392,7 +411,6 @@ class Mastodon: """ params = self.__generate_params(locals()) return self.__api_request('GET', '/api/v1/accounts/search', params) - ### # Reading data: Searching @@ -426,6 +444,17 @@ class Mastodon: """ return self.__api_request('GET', '/api/v1/blocks') + ### + # Reading data: Reports + ### + def reports(self): + """ + Fetch a list of reports made by the authenticated user. + + Returns a list of report dicts. + """ + return self.__api_request('GET', '/api/v1/reports') + ### # Reading data: Favourites ### @@ -611,6 +640,31 @@ class Mastodon: """ return self.__api_request('POST', '/api/v1/accounts/' + str(id) + "/unmute") + def account_update_credentials(self, display_name = None, note = None, avatar = None, header = None): + """ + Update the profile for the currently authenticated user. + + 'note' is the user's bio. + + 'avatar' and 'header' are PNG images encoded in base64. + """ + params = self.__generate_params(locals()) + return self.__api_request('POST', '/api/v1/accounts/update_credentials', params) + + ### + # Writing data: Reports + ### + def report(self, id, toots, comment): + """ + Report a user to the admin. + + Accepts a list of toot IDs associated with the report, and a comment. + + Returns a report dict. + """ + params = self.__generate_params(locals()) + return self.__api_request('POST', '/api/v1/reports/', params) + ### # Writing data: Follow requests ### -- cgit v1.2.3 From 973182cda07bc64545523b2ae0940c6f170c6db6 Mon Sep 17 00:00:00 2001 From: Alex McGivern Date: Wed, 26 Apr 2017 23:13:49 +0100 Subject: account_update_credentials uses PATCH, not POST --- 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 2e557e4..a32241b 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py @@ -646,7 +646,7 @@ class Mastodon: 'avatar' and 'header' are PNG images encoded in base64. """ params = self.__generate_params(locals()) - return self.__api_request('POST', '/api/v1/accounts/update_credentials', params) + return self.__api_request('PATCH', '/api/v1/accounts/update_credentials', params) ### # Writing data: Reports -- cgit v1.2.3 From 91e5388daef3f66b546726dcd57e84682df40a8f Mon Sep 17 00:00:00 2001 From: Alex McGivern Date: Thu, 27 Apr 2017 00:21:32 +0100 Subject: added content-type hint fixed POST parameters for reports added handling for PATCH requests added check for missing Date header to prevent errors when testing against Puma --- mastodon/Mastodon.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'mastodon') diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index a32241b..3b00f37 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py @@ -643,7 +643,8 @@ class Mastodon: 'note' is the user's bio. - 'avatar' and 'header' are PNG images encoded in base64. + 'avatar' and 'header' are images encoded in base64, prepended by a content-type + (for example: 'data:image/png;base64,iVBORw0KGgoAAAA[...]') """ params = self.__generate_params(locals()) return self.__api_request('PATCH', '/api/v1/accounts/update_credentials', params) @@ -651,7 +652,7 @@ class Mastodon: ### # Writing data: Reports ### - def report(self, id, toots, comment): + def report(self, account_id, status_ids, comment): """ Report a user to the admin. @@ -813,6 +814,9 @@ class Mastodon: if method == 'POST': response_object = requests.post(self.api_base_url + endpoint, data = params, headers = headers, files = files, timeout = self.request_timeout) + if method == 'PATCH': + response_object = requests.patch(self.api_base_url + endpoint, data = params, headers = headers, files = files, timeout = self.request_timeout) + if method == 'DELETE': response_object = requests.delete(self.api_base_url + endpoint, data = params, headers = headers, files = files, timeout = self.request_timeout) except Exception as e: @@ -848,11 +852,12 @@ class Mastodon: self.ratelimit_reset = self.__datetime_to_epoch(ratelimit_reset_datetime) # Adjust server time to local clock - server_time_datetime = dateutil.parser.parse(response_object.headers['Date']) - server_time = self.__datetime_to_epoch(server_time_datetime) - server_time_diff = time.time() - server_time - self.ratelimit_reset += server_time_diff - self.ratelimit_lastcall = time.time() + if 'Date' in response_object.headers: + server_time_datetime = dateutil.parser.parse(response_object.headers['Date']) + server_time = self.__datetime_to_epoch(server_time_datetime) + server_time_diff = time.time() - server_time + self.ratelimit_reset += server_time_diff + self.ratelimit_lastcall = time.time() except Exception as e: raise MastodonRatelimitError("Rate limit time calculations failed: %s" % e) -- cgit v1.2.3