diff options
Diffstat (limited to 'mastodon')
-rw-r--r-- | mastodon/Mastodon.py | 30 |
1 files changed, 12 insertions, 18 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 03a475e..215482e 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py | |||
@@ -1,6 +1,5 @@ | |||
1 | # coding: utf-8 | 1 | # coding: utf-8 |
2 | 2 | ||
3 | |||
4 | import os | 3 | import os |
5 | import os.path | 4 | import os.path |
6 | import mimetypes | 5 | import mimetypes |
@@ -10,10 +9,9 @@ import string | |||
10 | import pytz | 9 | import pytz |
11 | import datetime | 10 | import datetime |
12 | from contextlib import closing | 11 | from contextlib import closing |
13 | from urllib.parse import urlencode | ||
14 | |||
15 | import pytz | 12 | import pytz |
16 | import requests | 13 | import requests |
14 | from requests.models import urlencode | ||
17 | import dateutil | 15 | import dateutil |
18 | import dateutil.parser | 16 | import dateutil.parser |
19 | 17 | ||
@@ -134,28 +132,24 @@ class Mastodon: | |||
134 | self.access_token = token_file.readline().rstrip() | 132 | self.access_token = token_file.readline().rstrip() |
135 | 133 | ||
136 | 134 | ||
137 | @property | 135 | def __get_token_expired(self): |
138 | def token_expired(self) -> bool: | ||
139 | if self._token_expired < datetime.datetime.now(): | 136 | if self._token_expired < datetime.datetime.now(): |
140 | return True | 137 | return True |
141 | else: | 138 | else: |
142 | return False | 139 | return False |
143 | 140 | ||
144 | @token_expired.setter | 141 | def __set_token_expired(self, value): |
145 | def token_expired(self, value: int): | ||
146 | self._token_expired = datetime.datetime.now() + datetime.timedelta(seconds=value) | 142 | self._token_expired = datetime.datetime.now() + datetime.timedelta(seconds=value) |
147 | return | 143 | return |
148 | 144 | ||
149 | @property | 145 | def __get_refresh_token(self): |
150 | def refresh_token(self) -> str: | ||
151 | return self._refresh_token | 146 | return self._refresh_token |
152 | 147 | ||
153 | @refresh_token.setter | 148 | def __set_refresh_token(self, value): |
154 | def refresh_token(self, value): | ||
155 | self._refresh_token = value | 149 | self._refresh_token = value |
156 | return | 150 | return |
157 | 151 | ||
158 | def auth_request_url(self, client_id: str = None, redirect_uris: str = "urn:ietf:wg:oauth:2.0:oob", scopes: list = ['read', 'write', 'follow']) -> str: | 152 | def auth_request_url(self, client_id = None, redirect_uris = "urn:ietf:wg:oauth:2.0:oob", scopes = ['read', 'write', 'follow']): |
159 | """Returns the url that a client needs to request the grant from the server. | 153 | """Returns the url that a client needs to request the grant from the server. |
160 | https://mastodon.social/oauth/authorize?client_id=XXX&response_type=code&redirect_uris=YYY&scope=read+write+follow | 154 | https://mastodon.social/oauth/authorize?client_id=XXX&response_type=code&redirect_uris=YYY&scope=read+write+follow |
161 | """ | 155 | """ |
@@ -174,9 +168,9 @@ class Mastodon: | |||
174 | formatted_params = urlencode(params) | 168 | formatted_params = urlencode(params) |
175 | return "".join([self.api_base_url, "/oauth/authorize?", formatted_params]) | 169 | return "".join([self.api_base_url, "/oauth/authorize?", formatted_params]) |
176 | 170 | ||
177 | def log_in(self, username: str = None, password: str = None,\ | 171 | def log_in(self, username = None, password = None,\ |
178 | code: str = None, redirect_uri: str = "urn:ietf:wg:oauth:2.0:oob", refresh_token: str = None,\ | 172 | code = None, redirect_uri = "urn:ietf:wg:oauth:2.0:oob", refresh_token = None,\ |
179 | scopes: list = ['read', 'write', 'follow'], to_file: str = None) -> str: | 173 | scopes = ['read', 'write', 'follow'], to_file = None): |
180 | """ | 174 | """ |
181 | Docs: https://github.com/doorkeeper-gem/doorkeeper/wiki/Interacting-as-an-OAuth-client-with-Doorkeeper | 175 | Docs: https://github.com/doorkeeper-gem/doorkeeper/wiki/Interacting-as-an-OAuth-client-with-Doorkeeper |
182 | 176 | ||
@@ -214,8 +208,8 @@ class Mastodon: | |||
214 | try: | 208 | try: |
215 | response = self.__api_request('POST', '/oauth/token', params, do_ratelimiting = False) | 209 | response = self.__api_request('POST', '/oauth/token', params, do_ratelimiting = False) |
216 | self.access_token = response['access_token'] | 210 | self.access_token = response['access_token'] |
217 | self.refresh_token = response.get('refresh_token') | 211 | self.__set_refresh_token(response.get('refresh_token')) |
218 | self.token_expired = int(response.get('expires_in', 0)) | 212 | self.__set_token_expired(int(response.get('expires_in', 0))) |
219 | except Exception as e: | 213 | except Exception as e: |
220 | if username is not None or password is not None: | 214 | if username is not None or password is not None: |
221 | raise MastodonIllegalArgumentError('Invalid user name, password, or redirect_uris: %s' % e) | 215 | raise MastodonIllegalArgumentError('Invalid user name, password, or redirect_uris: %s' % e) |