aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'mastodon')
-rw-r--r--mastodon/Mastodon.py53
1 files changed, 48 insertions, 5 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py
index 7cc7f1b..b0e5f82 100644
--- a/mastodon/Mastodon.py
+++ b/mastodon/Mastodon.py
@@ -111,6 +111,45 @@ class Mastodon:
111 __DEFAULT_TIMEOUT = 300 111 __DEFAULT_TIMEOUT = 300
112 __DEFAULT_STREAM_TIMEOUT = 300 112 __DEFAULT_STREAM_TIMEOUT = 300
113 __DEFAULT_STREAM_RECONNECT_WAIT_SEC = 5 113 __DEFAULT_STREAM_RECONNECT_WAIT_SEC = 5
114 __DEFAULT_SCOPES = ['read', 'write', 'follow', 'push']
115 __SCOPE_SETS = {
116 'read': [
117 'read:accounts',
118 'read:blocks',
119 'read:favourites',
120 'read:filters',
121 'read:follows',
122 'read:lists',
123 'read:mutes',
124 'read:notifications',
125 'read:reports',
126 'read:search',
127 'read:statuses'
128 ],
129 'write': [
130 'write:accounts',
131 'write:blocks',
132 'write:favourites',
133 'write:filters',
134 'write:follows',
135 'write:lists',
136 'write:media',
137 'write:mutes',
138 'write:notifications',
139 'write:reports',
140 'write:statuses',
141 ],
142 'follow': [
143 'read:blocks',
144 'read:follows',
145 'read:mutes',
146 'write:blocks',
147 'write:follows',
148 'write:mutes',
149 ]
150 }
151 __VALID_SCOPES = ['read', 'write', 'follow', 'push'] + __SCOPE_SETS['read'] + __SCOPE_SETS['write']
152
114 __SUPPORTED_MASTODON_VERSION = "2.4.0" 153 __SUPPORTED_MASTODON_VERSION = "2.4.0"
115 154
116 # Dict versions 155 # Dict versions
@@ -139,10 +178,11 @@ class Mastodon:
139 # Registering apps 178 # Registering apps
140 ### 179 ###
141 @staticmethod 180 @staticmethod
142 def create_app(client_name, scopes=['read', 'write', 'follow', 'push'], redirect_uris=None, website=None, to_file=None, 181 def create_app(client_name, scopes=__DEFAULT_SCOPES, redirect_uris=None, website=None, to_file=None,
143 api_base_url=__DEFAULT_BASE_URL, request_timeout=__DEFAULT_TIMEOUT): 182 api_base_url=__DEFAULT_BASE_URL, request_timeout=__DEFAULT_TIMEOUT):
144 """ 183 """
145 Create a new app with given `client_name` and `scopes` (read, write, follow, push) 184 Create a new app with given `client_name` and `scopes` (The basic scropse are "read", "write", "follow" and "push"
185 - more granular scopes are available, please refere to Mastodon documentation for which).
146 186
147 Specify `redirect_uris` if you want users to be redirected to a certain page after authenticating. 187 Specify `redirect_uris` if you want users to be redirected to a certain page after authenticating.
148 Specify `to_file` to persist your apps info to a file so you can use them in the constructor. 188 Specify `to_file` to persist your apps info to a file so you can use them in the constructor.
@@ -312,7 +352,7 @@ class Mastodon:
312 return Mastodon.__SUPPORTED_MASTODON_VERSION 352 return Mastodon.__SUPPORTED_MASTODON_VERSION
313 353
314 def auth_request_url(self, client_id=None, redirect_uris="urn:ietf:wg:oauth:2.0:oob", 354 def auth_request_url(self, client_id=None, redirect_uris="urn:ietf:wg:oauth:2.0:oob",
315 scopes=['read', 'write', 'follow', 'push']): 355 scopes=__DEFAULT_SCOPES):
316 """Returns the url that a client needs to request the grant from the server. 356 """Returns the url that a client needs to request the grant from the server.
317 """ 357 """
318 if client_id is None: 358 if client_id is None:
@@ -332,7 +372,7 @@ class Mastodon:
332 372
333 def log_in(self, username=None, password=None, 373 def log_in(self, username=None, password=None,
334 code=None, redirect_uri="urn:ietf:wg:oauth:2.0:oob", refresh_token=None, 374 code=None, redirect_uri="urn:ietf:wg:oauth:2.0:oob", refresh_token=None,
335 scopes=['read', 'write', 'follow', 'push'], to_file=None): 375 scopes=__DEFAULT_SCOPES, to_file=None):
336 """ 376 """
337 Get the access token for a user. 377 Get the access token for a user.
338 378
@@ -381,7 +421,10 @@ class Mastodon:
381 raise MastodonIllegalArgumentError('Invalid request: %s' % e) 421 raise MastodonIllegalArgumentError('Invalid request: %s' % e)
382 422
383 received_scopes = response["scope"].split(" ") 423 received_scopes = response["scope"].split(" ")
384 424 for scope_set in self.__SCOPE_SETS.keys():
425 if scope_set in received_scopes:
426 received_scopes += self.__SCOPE_SETS[scope_set]
427
385 if not set(scopes) <= set(received_scopes): 428 if not set(scopes) <= set(received_scopes):
386 raise MastodonAPIError( 429 raise MastodonAPIError(
387 'Granted scopes "' + " ".join(received_scopes) + '" do not contain all of the requested scopes "' + " ".join(scopes) + '".') 430 'Granted scopes "' + " ".join(received_scopes) + '" do not contain all of the requested scopes "' + " ".join(scopes) + '".')
Powered by cgit v1.2.3 (git 2.41.0)