diff options
-rw-r--r-- | README.rst | 23 | ||||
-rw-r--r-- | docs/index.rst | 2 | ||||
-rw-r--r-- | mastodon/Mastodon.py | 36 |
3 files changed, 47 insertions, 14 deletions
@@ -2,8 +2,29 @@ Mastodon.py | |||
2 | =========== | 2 | =========== |
3 | .. code-block:: python | 3 | .. code-block:: python |
4 | 4 | ||
5 | from mastodon import Mastodon | ||
6 | |||
7 | # Register app - only once! | ||
8 | ''' | ||
9 | Mastodon.create_app( | ||
10 | 'pytooterapp', | ||
11 | to_file = 'pytooter_clientcred.txt' | ||
12 | ) | ||
13 | ''' | ||
14 | |||
15 | # Log in - either every time, or use persisted | ||
16 | ''' | ||
17 | mastodon = Mastodon(client_id = 'pytooter_clientcred.txt') | ||
18 | mastodon.log_in( | ||
19 | '[email protected]', | ||
20 | 'incrediblygoodpassword', | ||
21 | to_file = 'pytooter_usercred.txt' | ||
22 | ) | ||
23 | ''' | ||
24 | |||
25 | # Create actual instance | ||
5 | mastodon = Mastodon( | 26 | mastodon = Mastodon( |
6 | client_id = 'pytooter_clientcred.txt', | 27 | client_id = 'pytooter_clientcred.txt', |
7 | access_token = 'pytooter_usercred.txt' | 28 | access_token = 'pytooter_usercred.txt' |
8 | ) | 29 | ) |
9 | mastodon.toot('Tooting from python!') | 30 | mastodon.toot('Tooting from python!') |
diff --git a/docs/index.rst b/docs/index.rst index 8be642a..6279d48 100644 --- a/docs/index.rst +++ b/docs/index.rst | |||
@@ -19,7 +19,7 @@ Mastodon.py | |||
19 | ''' | 19 | ''' |
20 | mastodon = Mastodon(client_id = 'pytooter_clientcred.txt') | 20 | mastodon = Mastodon(client_id = 'pytooter_clientcred.txt') |
21 | mastodon.log_in( | 21 | mastodon.log_in( |
22 | 'pytooter', | 22 | 'my_login_e[email protected]', |
23 | 'incrediblygoodpassword', | 23 | 'incrediblygoodpassword', |
24 | to_file = 'pytooter_usercred.txt' | 24 | to_file = 'pytooter_usercred.txt' |
25 | ) | 25 | ) |
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 06fe8a1..69d71ec 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py | |||
@@ -56,8 +56,10 @@ class Mastodon: | |||
56 | request_data['redirect_uris'] = 'urn:ietf:wg:oauth:2.0:oob'; | 56 | request_data['redirect_uris'] = 'urn:ietf:wg:oauth:2.0:oob'; |
57 | 57 | ||
58 | response = requests.post(api_base_url + '/api/v1/apps', data = request_data, timeout = request_timeout).json() | 58 | response = requests.post(api_base_url + '/api/v1/apps', data = request_data, timeout = request_timeout).json() |
59 | except: | 59 | except Exception as e: |
60 | raise MastodonNetworkError("Could not complete request.") | 60 | import traceback |
61 | traceback.print_exc() | ||
62 | raise MastodonNetworkError("Could not complete request: %s" % e) | ||
61 | 63 | ||
62 | if to_file != None: | 64 | if to_file != None: |
63 | with open(to_file, 'w') as secret_file: | 65 | with open(to_file, 'w') as secret_file: |
@@ -142,8 +144,10 @@ class Mastodon: | |||
142 | try: | 144 | try: |
143 | response = self.__api_request('POST', '/oauth/token', params, do_ratelimiting = False) | 145 | response = self.__api_request('POST', '/oauth/token', params, do_ratelimiting = False) |
144 | self.access_token = response['access_token'] | 146 | self.access_token = response['access_token'] |
145 | except: | 147 | except Exception as e: |
146 | raise MastodonIllegalArgumentError('Invalid user name, password or scopes.') | 148 | import traceback |
149 | traceback.print_exc() | ||
150 | raise MastodonIllegalArgumentError('Invalid user name, password or scopes: %s' % e) | ||
147 | 151 | ||
148 | requested_scopes = " ".join(sorted(scopes)) | 152 | requested_scopes = " ".join(sorted(scopes)) |
149 | received_scopes = " ".join(sorted(response["scope"].split(" "))) | 153 | received_scopes = " ".join(sorted(response["scope"].split(" "))) |
@@ -415,8 +419,10 @@ class Mastodon: | |||
415 | media_ids_proper.append(media_id["id"]) | 419 | media_ids_proper.append(media_id["id"]) |
416 | else: | 420 | else: |
417 | media_ids_proper.append(media_id) | 421 | media_ids_proper.append(media_id) |
418 | except: | 422 | except Exception as e: |
419 | raise MastodonIllegalArgumentError("Invalid media dict.") | 423 | import traceback |
424 | traceback.print_exc() | ||
425 | raise MastodonIllegalArgumentError("Invalid media dict: %s" % e) | ||
420 | 426 | ||
421 | params_initial["media_ids"] = media_ids_proper | 427 | params_initial["media_ids"] = media_ids_proper |
422 | 428 | ||
@@ -640,8 +646,10 @@ class Mastodon: | |||
640 | 646 | ||
641 | if method == 'DELETE': | 647 | if method == 'DELETE': |
642 | response_object = requests.delete(self.api_base_url + endpoint, data = params, headers = headers, files = files, timeout = self.request_timeout) | 648 | response_object = requests.delete(self.api_base_url + endpoint, data = params, headers = headers, files = files, timeout = self.request_timeout) |
643 | except: | 649 | except Exception as e: |
644 | raise MastodonNetworkError("Could not complete request.") | 650 | import traceback |
651 | traceback.print_exc() | ||
652 | raise MastodonNetworkError("Could not complete request: %s" % e) | ||
645 | 653 | ||
646 | if response_object == None: | 654 | if response_object == None: |
647 | raise MastodonIllegalArgumentError("Illegal request.") | 655 | raise MastodonIllegalArgumentError("Illegal request.") |
@@ -649,7 +657,7 @@ class Mastodon: | |||
649 | # Handle response | 657 | # Handle response |
650 | if self.debug_requests == True: | 658 | if self.debug_requests == True: |
651 | print('Mastodon: Response received with code ' + str(response_object.status_code) + '.') | 659 | print('Mastodon: Response received with code ' + str(response_object.status_code) + '.') |
652 | print('Respose headers: ' + str(response_object.headers)) | 660 | print('response headers: ' + str(response_object.headers)) |
653 | print('Response text content: ' + str(response_object.text)) | 661 | print('Response text content: ' + str(response_object.text)) |
654 | 662 | ||
655 | if response_object.status_code == 404: | 663 | if response_object.status_code == 404: |
@@ -661,7 +669,9 @@ class Mastodon: | |||
661 | try: | 669 | try: |
662 | response = response_object.json() | 670 | response = response_object.json() |
663 | except: | 671 | except: |
664 | raise MastodonAPIError("Could not parse response as JSON, respose code was " + str(response_object.status_code)) | 672 | import traceback |
673 | traceback.print_exc() | ||
674 | raise MastodonAPIError("Could not parse response as JSON, response code was %s, bad json content was '%s'" % (response_object.status_code, response_object.content)) | ||
665 | 675 | ||
666 | # Handle rate limiting | 676 | # Handle rate limiting |
667 | if 'X-RateLimit-Remaining' in response_object.headers and do_ratelimiting: | 677 | if 'X-RateLimit-Remaining' in response_object.headers and do_ratelimiting: |
@@ -678,8 +688,10 @@ class Mastodon: | |||
678 | server_time_diff = time.time() - server_time | 688 | server_time_diff = time.time() - server_time |
679 | self.ratelimit_reset += server_time_diff | 689 | self.ratelimit_reset += server_time_diff |
680 | self.ratelimit_lastcall = time.time() | 690 | self.ratelimit_lastcall = time.time() |
681 | except: | 691 | except Exception as e: |
682 | raise MastodonRatelimitError("Rate limit time calculations failed.") | 692 | import traceback |
693 | traceback.print_exc() | ||
694 | raise MastodonRatelimitError("Rate limit time calculations failed: %s" % e) | ||
683 | 695 | ||
684 | if "error" in response and response["error"] == "Throttled": | 696 | if "error" in response and response["error"] == "Throttled": |
685 | if self.ratelimit_method == "throw": | 697 | if self.ratelimit_method == "throw": |