Restored python 2 compatibility

This commit is contained in:
Lorenz Diener 2017-04-26 11:43:16 +02:00
pare d5362139a7
commit 6a2c394c50

Veure arxiu

@ -1,6 +1,5 @@
# coding: utf-8 # coding: utf-8
import os import os
import os.path import os.path
import mimetypes import mimetypes
@ -10,10 +9,9 @@ import string
import pytz import pytz
import datetime import datetime
from contextlib import closing from contextlib import closing
from urllib.parse import urlencode
import pytz import pytz
import requests import requests
from requests.models import urlencode
import dateutil import dateutil
import dateutil.parser import dateutil.parser
@ -134,28 +132,24 @@ class Mastodon:
self.access_token = token_file.readline().rstrip() self.access_token = token_file.readline().rstrip()
@property def __get_token_expired(self):
def token_expired(self) -> bool:
if self._token_expired < datetime.datetime.now(): if self._token_expired < datetime.datetime.now():
return True return True
else: else:
return False return False
@token_expired.setter def __set_token_expired(self, value):
def token_expired(self, value: int):
self._token_expired = datetime.datetime.now() + datetime.timedelta(seconds=value) self._token_expired = datetime.datetime.now() + datetime.timedelta(seconds=value)
return return
@property def __get_refresh_token(self):
def refresh_token(self) -> str:
return self._refresh_token return self._refresh_token
@refresh_token.setter def __set_refresh_token(self, value):
def refresh_token(self, value):
self._refresh_token = value self._refresh_token = value
return return
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: def auth_request_url(self, client_id = None, redirect_uris = "urn:ietf:wg:oauth:2.0:oob", scopes = ['read', 'write', 'follow']):
"""Returns the url that a client needs to request the grant from the server. """Returns the url that a client needs to request the grant from the server.
https://mastodon.social/oauth/authorize?client_id=XXX&response_type=code&redirect_uris=YYY&scope=read+write+follow https://mastodon.social/oauth/authorize?client_id=XXX&response_type=code&redirect_uris=YYY&scope=read+write+follow
""" """
@ -174,9 +168,9 @@ class Mastodon:
formatted_params = urlencode(params) formatted_params = urlencode(params)
return "".join([self.api_base_url, "/oauth/authorize?", formatted_params]) return "".join([self.api_base_url, "/oauth/authorize?", formatted_params])
def log_in(self, username: str = None, password: str = None,\ def log_in(self, username = None, password = None,\
code: str = None, redirect_uri: str = "urn:ietf:wg:oauth:2.0:oob", refresh_token: str = None,\ code = None, redirect_uri = "urn:ietf:wg:oauth:2.0:oob", refresh_token = None,\
scopes: list = ['read', 'write', 'follow'], to_file: str = None) -> str: scopes = ['read', 'write', 'follow'], to_file = None):
""" """
Docs: https://github.com/doorkeeper-gem/doorkeeper/wiki/Interacting-as-an-OAuth-client-with-Doorkeeper Docs: https://github.com/doorkeeper-gem/doorkeeper/wiki/Interacting-as-an-OAuth-client-with-Doorkeeper
@ -214,8 +208,8 @@ class Mastodon:
try: try:
response = self.__api_request('POST', '/oauth/token', params, do_ratelimiting = False) response = self.__api_request('POST', '/oauth/token', params, do_ratelimiting = False)
self.access_token = response['access_token'] self.access_token = response['access_token']
self.refresh_token = response.get('refresh_token') self.__set_refresh_token(response.get('refresh_token'))
self.token_expired = int(response.get('expires_in', 0)) self.__set_token_expired(int(response.get('expires_in', 0)))
except Exception as e: except Exception as e:
if username is not None or password is not None: if username is not None or password is not None:
raise MastodonIllegalArgumentError('Invalid user name, password, or redirect_uris: %s' % e) raise MastodonIllegalArgumentError('Invalid user name, password, or redirect_uris: %s' % e)