2017-12-30 12:52:55 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import webbrowser
|
|
|
|
|
|
|
|
from builtins import input
|
|
|
|
from getpass import getpass
|
|
|
|
|
2017-12-30 13:32:52 +01:00
|
|
|
from toot import api, config, DEFAULT_INSTANCE, User, App
|
|
|
|
from toot.exceptions import ApiError, ConsoleError
|
2017-12-30 12:52:55 +01:00
|
|
|
from toot.output import print_out
|
|
|
|
|
|
|
|
|
2018-12-25 02:20:30 +01:00
|
|
|
def register_app(domain, scheme='https'):
|
2017-12-30 16:30:35 +01:00
|
|
|
print_out("Looking up instance info...")
|
|
|
|
instance = api.get_instance(domain)
|
|
|
|
|
|
|
|
print_out("Found instance <blue>{}</blue> running Mastodon version <yellow>{}</yellow>".format(
|
|
|
|
instance['title'], instance['version']))
|
2017-12-30 12:52:55 +01:00
|
|
|
|
|
|
|
try:
|
2017-12-30 16:30:35 +01:00
|
|
|
print_out("Registering application...")
|
2018-12-25 02:20:30 +01:00
|
|
|
response = api.create_app(domain, scheme)
|
2017-12-30 16:30:35 +01:00
|
|
|
except ApiError:
|
|
|
|
raise ConsoleError("Registration failed.")
|
2017-12-30 12:52:55 +01:00
|
|
|
|
2018-12-25 02:20:30 +01:00
|
|
|
base_url = scheme + '://' + domain
|
2017-12-30 12:52:55 +01:00
|
|
|
|
2017-12-30 16:30:35 +01:00
|
|
|
app = App(domain, base_url, response['client_id'], response['client_secret'])
|
2018-01-02 10:44:32 +01:00
|
|
|
config.save_app(app)
|
|
|
|
|
|
|
|
print_out("Application tokens saved.")
|
2017-12-30 12:52:55 +01:00
|
|
|
|
|
|
|
return app
|
|
|
|
|
|
|
|
|
2018-12-25 02:20:30 +01:00
|
|
|
def create_app_interactive(instance=None, scheme='https'):
|
2017-12-30 12:52:55 +01:00
|
|
|
if not instance:
|
|
|
|
print_out("Choose an instance [<green>{}</green>]: ".format(DEFAULT_INSTANCE), end="")
|
|
|
|
instance = input()
|
|
|
|
if not instance:
|
|
|
|
instance = DEFAULT_INSTANCE
|
|
|
|
|
2018-12-25 02:20:30 +01:00
|
|
|
return config.load_app(instance) or register_app(instance, scheme)
|
2017-12-30 12:52:55 +01:00
|
|
|
|
|
|
|
|
2018-01-02 10:44:32 +01:00
|
|
|
def create_user(app, access_token):
|
|
|
|
# Username is not yet known at this point, so fetch it from Mastodon
|
|
|
|
user = User(app.instance, None, access_token)
|
|
|
|
creds = api.verify_credentials(app, user)
|
|
|
|
|
|
|
|
user = User(app.instance, creds['username'], access_token)
|
|
|
|
config.save_user(user, activate=True)
|
2017-12-30 12:52:55 +01:00
|
|
|
|
2018-01-02 10:44:32 +01:00
|
|
|
print_out("Access token saved to config at: <green>{}</green>".format(
|
|
|
|
config.get_config_file_path()))
|
2017-12-30 12:52:55 +01:00
|
|
|
|
|
|
|
return user
|
|
|
|
|
|
|
|
|
|
|
|
def login_interactive(app, email=None):
|
|
|
|
print_out("Log in to <green>{}</green>".format(app.instance))
|
|
|
|
|
|
|
|
if email:
|
|
|
|
print_out("Email: <green>{}</green>".format(email))
|
|
|
|
|
|
|
|
while not email:
|
|
|
|
email = input('Email: ')
|
|
|
|
|
|
|
|
password = getpass('Password: ')
|
|
|
|
|
|
|
|
try:
|
|
|
|
print_out("Authenticating...")
|
|
|
|
response = api.login(app, email, password)
|
2017-12-30 13:32:52 +01:00
|
|
|
except ApiError:
|
2017-12-30 12:52:55 +01:00
|
|
|
raise ConsoleError("Login failed")
|
|
|
|
|
2018-01-02 10:44:32 +01:00
|
|
|
return create_user(app, response['access_token'])
|
2017-12-30 12:52:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
BROWSER_LOGIN_EXPLANATION = """
|
|
|
|
This authentication method requires you to log into your Mastodon instance
|
|
|
|
in your browser, where you will be asked to authorize <yellow>toot</yellow> to access
|
|
|
|
your account. When you do, you will be given an <yellow>authorization code</yellow>
|
|
|
|
which you need to paste here.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
def login_browser_interactive(app):
|
|
|
|
url = api.get_browser_login_url(app)
|
|
|
|
print_out(BROWSER_LOGIN_EXPLANATION)
|
|
|
|
|
|
|
|
print_out("This is the login URL:")
|
|
|
|
print_out(url)
|
|
|
|
print_out("")
|
|
|
|
|
|
|
|
yesno = input("Open link in default browser? [Y/n]")
|
|
|
|
if not yesno or yesno.lower() == 'y':
|
|
|
|
webbrowser.open(url)
|
|
|
|
|
|
|
|
authorization_code = ""
|
|
|
|
while not authorization_code:
|
|
|
|
authorization_code = input("Authorization code: ")
|
|
|
|
|
|
|
|
print_out("\nRequesting access token...")
|
|
|
|
response = api.request_access_token(app, authorization_code)
|
|
|
|
|
2018-01-02 10:44:32 +01:00
|
|
|
return create_user(app, response['access_token'])
|