aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'mastodon')
-rw-r--r--mastodon/Mastodon.py52
1 files changed, 38 insertions, 14 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py
index 465beb7..c84ac6a 100644
--- a/mastodon/Mastodon.py
+++ b/mastodon/Mastodon.py
@@ -267,16 +267,17 @@ class Mastodon:
267 267
268 if to_file is not None: 268 if to_file is not None:
269 with open(to_file, 'w') as secret_file: 269 with open(to_file, 'w') as secret_file:
270 secret_file.write(response['client_id'] + '\n') 270 secret_file.write(response['client_id'] + "\n")
271 secret_file.write(response['client_secret'] + '\n') 271 secret_file.write(response['client_secret'] + "\n")
272 272 secret_file.write(api_base_url + "\n")
273
273 return (response['client_id'], response['client_secret']) 274 return (response['client_id'], response['client_secret'])
274 275
275 ### 276 ###
276 # Authentication, including constructor 277 # Authentication, including constructor
277 ### 278 ###
278 def __init__(self, client_id=None, client_secret=None, access_token=None, 279 def __init__(self, client_id=None, client_secret=None, access_token=None,
279 api_base_url=__DEFAULT_BASE_URL, debug_requests=False, 280 api_base_url=None, debug_requests=False,
280 ratelimit_method="wait", ratelimit_pacefactor=1.1, 281 ratelimit_method="wait", ratelimit_pacefactor=1.1,
281 request_timeout=__DEFAULT_TIMEOUT, mastodon_version=None, 282 request_timeout=__DEFAULT_TIMEOUT, mastodon_version=None,
282 version_check_mode = "created", session=None): 283 version_check_mode = "created", session=None):
@@ -285,9 +286,12 @@ class Mastodon:
285 give a `client_id` and it is not a file, you must also give a secret. If you specify an 286 give a `client_id` and it is not a file, you must also give a secret. If you specify an
286 `access_token` then you don't need to specify a `client_id`. It is allowed to specify 287 `access_token` then you don't need to specify a `client_id`. It is allowed to specify
287 neither - in this case, you will be restricted to only using endpoints that do not 288 neither - in this case, you will be restricted to only using endpoints that do not
288 require authentication. 289 require authentication. If a file is given as `client_id`, client ID, secret and
290 base url are read from that file.
289 291
290 You can also specify an `access_token`, directly or as a file (as written by `log_in()`_). 292 You can also specify an `access_token`, directly or as a file (as written by `log_in()`_). If
293 a file is given, Mastodon.py also tries to load the base URL from this file, if present. A
294 client id and secret are not required in this case.
291 295
292 Mastodon.py can try to respect rate limits in several ways, controlled by `ratelimit_method`. 296 Mastodon.py can try to respect rate limits in several ways, controlled by `ratelimit_method`.
293 "throw" makes functions throw a `MastodonRatelimitError` when the rate 297 "throw" makes functions throw a `MastodonRatelimitError` when the rate
@@ -298,8 +302,9 @@ class Mastodon:
298 even in "wait" and "pace" mode, requests can still fail due to network or other problems! Also 302 even in "wait" and "pace" mode, requests can still fail due to network or other problems! Also
299 note that "pace" and "wait" are NOT thread safe. 303 note that "pace" and "wait" are NOT thread safe.
300 304
301 Specify `api_base_url` if you wish to talk to an instance other than the flagship one. 305 Specify `api_base_url` if you wish to talk to an instance other than the flagship one. When
302 If a file is given as `client_id`, client ID and secret are read from that file. 306 reading from client id or access token files as written by Mastodon.py 1.5.0 or larger,
307 this can be omitted.
303 308
304 By default, a timeout of 300 seconds is used for all requests. If you wish to change this, 309 By default, a timeout of 300 seconds is used for all requests. If you wish to change this,
305 pass the desired timeout (in seconds) as `request_timeout`. 310 pass the desired timeout (in seconds) as `request_timeout`.
@@ -317,7 +322,10 @@ class Mastodon:
317 changed after the version of Mastodon that is connected has been released. If it is set to "none", 322 changed after the version of Mastodon that is connected has been released. If it is set to "none",
318 version checking is disabled. 323 version checking is disabled.
319 """ 324 """
320 self.api_base_url = Mastodon.__protocolize(api_base_url) 325 self.api_base_url = None
326 if not api_base_url is None:
327 self.api_base_url = Mastodon.__protocolize(api_base_url)
328
321 self.client_id = client_id 329 self.client_id = client_id
322 self.client_secret = client_secret 330 self.client_secret = client_secret
323 self.access_token = access_token 331 self.access_token = access_token
@@ -364,6 +372,13 @@ class Mastodon:
364 with open(self.client_id, 'r') as secret_file: 372 with open(self.client_id, 'r') as secret_file:
365 self.client_id = secret_file.readline().rstrip() 373 self.client_id = secret_file.readline().rstrip()
366 self.client_secret = secret_file.readline().rstrip() 374 self.client_secret = secret_file.readline().rstrip()
375
376 try_base_url = secret_file.readline().rstrip()
377 if (not try_base_url is None) and len(try_base_url) != 0:
378 try_base_url = Mastodon.__protocolize(try_base_url)
379 if not (self.api_base_url is None or try_base_url == self.api_base_url):
380 raise MastodonIllegalArgumentError('Mismatch in base URLs between files and/or specified')
381 self.api_base_url = try_base_url
367 else: 382 else:
368 if self.client_secret is None: 383 if self.client_secret is None:
369 raise MastodonIllegalArgumentError('Specified client id directly, but did not supply secret') 384 raise MastodonIllegalArgumentError('Specified client id directly, but did not supply secret')
@@ -371,7 +386,14 @@ class Mastodon:
371 if self.access_token is not None and os.path.isfile(self.access_token): 386 if self.access_token is not None and os.path.isfile(self.access_token):
372 with open(self.access_token, 'r') as token_file: 387 with open(self.access_token, 'r') as token_file:
373 self.access_token = token_file.readline().rstrip() 388 self.access_token = token_file.readline().rstrip()
374 389
390 try_base_url = token_file.readline().rstrip()
391 if (not try_base_url is None) and len(try_base_url) != 0:
392 try_base_url = Mastodon.__protocolize(try_base_url)
393 if not (self.api_base_url is None or try_base_url == self.api_base_url):
394 raise MastodonIllegalArgumentError('Mismatch in base URLs between files and/or specified')
395 self.api_base_url = try_base_url
396
375 def retrieve_mastodon_version(self): 397 def retrieve_mastodon_version(self):
376 """ 398 """
377 Determine installed mastodon version and set major, minor and patch (not including RC info) accordingly. 399 Determine installed mastodon version and set major, minor and patch (not including RC info) accordingly.
@@ -508,8 +530,9 @@ class Mastodon:
508 530
509 if to_file is not None: 531 if to_file is not None:
510 with open(to_file, 'w') as token_file: 532 with open(to_file, 'w') as token_file:
511 token_file.write(response['access_token'] + '\n') 533 token_file.write(response['access_token'] + "\n")
512 534 token_file.write(self.api_base_url + "\n")
535
513 self.__logged_in_id = None 536 self.__logged_in_id = None
514 537
515 return response['access_token'] 538 return response['access_token']
@@ -572,8 +595,9 @@ class Mastodon:
572 595
573 if to_file is not None: 596 if to_file is not None:
574 with open(to_file, 'w') as token_file: 597 with open(to_file, 'w') as token_file:
575 token_file.write(response['access_token'] + '\n') 598 token_file.write(response['access_token'] + "\n")
576 599 token_file.write(self.api_base_url + "\n")
600
577 self.__logged_in_id = None 601 self.__logged_in_id = None
578 602
579 return response['access_token'] 603 return response['access_token']
Powered by cgit v1.2.3 (git 2.41.0)