aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLorenz Diener <[email protected]>2017-04-26 11:43:16 +0200
committerLorenz Diener <[email protected]>2017-04-26 11:43:16 +0200
commit6a2c394c506eb8a4943f4612702dbf2e0283078e (patch)
treea9b90aca87617baa8da44813c7f09823be21e592
parentd5362139a7f0c3f73feb78f1504d255b38555c8f (diff)
downloadmastodon.py-6a2c394c506eb8a4943f4612702dbf2e0283078e.tar.gz
Restored python 2 compatibility
-rw-r--r--mastodon/Mastodon.py30
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
4import os 3import os
5import os.path 4import os.path
6import mimetypes 5import mimetypes
@@ -10,10 +9,9 @@ import string
10import pytz 9import pytz
11import datetime 10import datetime
12from contextlib import closing 11from contextlib import closing
13from urllib.parse import urlencode
14
15import pytz 12import pytz
16import requests 13import requests
14from requests.models import urlencode
17import dateutil 15import dateutil
18import dateutil.parser 16import 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)
Powered by cgit v1.2.3 (git 2.41.0)