mirror of
https://github.com/ihabunek/toot.git
synced 2024-11-03 04:17:21 -05:00
Require an explicit login
This commit is contained in:
parent
4e74212203
commit
9e32f7f8dd
41
README.rst
41
README.rst
@ -25,18 +25,47 @@ Install using pip:
|
||||
Usage
|
||||
-----
|
||||
|
||||
Currently implements only posting a new status:
|
||||
|
||||
Firstly, you will need to login to a Mastodon instance:
|
||||
|
||||
.. code-block::
|
||||
|
||||
toot post "Hello world!"
|
||||
toot login
|
||||
|
||||
On first use, you will be asked to choose a Mastodon instance and log in.
|
||||
You will be asked to chose an instance_ and enter your credentials.
|
||||
|
||||
The app and user tokens are saved in two files in your home directory:
|
||||
.. _instance: https://github.com/tootsuite/documentation/blob/master/Using-Mastodon/List-of-Mastodon-instances.md
|
||||
|
||||
The application and user access tokens will be saved in two files in your home directory:
|
||||
|
||||
* ``~/.config/toot/app.cfg``
|
||||
* ``~/.config/toot/user.cfg``
|
||||
|
||||
To logout, delete these files.
|
||||
You can check whether you are currently logged in:
|
||||
|
||||
.. code-block::
|
||||
|
||||
toot auth
|
||||
|
||||
And you can logout which will remove the stored access tokens:
|
||||
|
||||
.. code-block::
|
||||
|
||||
toot logout
|
||||
|
||||
Show timeline
|
||||
~~~~~~~~~~~~~
|
||||
|
||||
To show recent items in your public timeline:
|
||||
|
||||
.. code-block::
|
||||
|
||||
toot timeline
|
||||
|
||||
Post status
|
||||
~~~~~~~~~~~
|
||||
|
||||
To post a new status to your timeline:
|
||||
|
||||
.. code-block::
|
||||
|
||||
toot post "Hello world!"
|
||||
|
@ -1,5 +1,6 @@
|
||||
import os
|
||||
import sys
|
||||
import logging
|
||||
|
||||
from bs4 import BeautifulSoup
|
||||
from builtins import input
|
||||
@ -13,6 +14,10 @@ from .config import save_user, load_user, load_app, save_app, CONFIG_APP_FILE, C
|
||||
from . import create_app, login, post_status, timeline_home, DEFAULT_INSTANCE
|
||||
|
||||
|
||||
class ConsoleError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def green(text):
|
||||
return "\033[92m{}\033[0m".format(text)
|
||||
|
||||
@ -22,17 +27,20 @@ def red(text):
|
||||
|
||||
|
||||
def create_app_interactive():
|
||||
instance = input("Choose an instance [{}]: ".format(DEFAULT_INSTANCE))
|
||||
instance = input("Choose an instance [%s]: " % green(DEFAULT_INSTANCE))
|
||||
if not instance:
|
||||
instance = DEFAULT_INSTANCE
|
||||
|
||||
base_url = 'https://{}'.format(instance)
|
||||
|
||||
print("Creating app with {}".format(base_url))
|
||||
app = create_app(base_url)
|
||||
print("Registering application with %s" % green(base_url))
|
||||
try:
|
||||
app = create_app(base_url)
|
||||
except:
|
||||
raise ConsoleError("Failed authenticating application. Did you enter a valid instance?")
|
||||
|
||||
print("App tokens saved to: {}".format(green(CONFIG_APP_FILE)))
|
||||
save_app(app)
|
||||
print("Application tokens saved to: {}".format(green(CONFIG_APP_FILE)))
|
||||
|
||||
return app
|
||||
|
||||
@ -43,7 +51,10 @@ def login_interactive(app):
|
||||
password = getpass('Password: ')
|
||||
|
||||
print("Authenticating...")
|
||||
user = login(app, email, password)
|
||||
try:
|
||||
user = login(app, email, password)
|
||||
except:
|
||||
raise ConsoleError("Login failed")
|
||||
|
||||
save_user(user)
|
||||
print("User token saved to " + green(CONFIG_USER_FILE))
|
||||
@ -55,8 +66,11 @@ def print_usage():
|
||||
print("toot - interact with Mastodon from the command line")
|
||||
print("")
|
||||
print("Usage:")
|
||||
print(" toot post \"All your base are belong to us\"")
|
||||
print(" toot timeline")
|
||||
print(" toot login - log into a Mastodon instance (saves access tokens to `~/.config/toot/`)")
|
||||
print(" toot logout - log out (delete saved access tokens)")
|
||||
print(" toot auth - shows currently logged in user and instance")
|
||||
print(" toot post <msg> - toot a new post to your timeline")
|
||||
print(" toot timeline - shows your public timeline")
|
||||
print("")
|
||||
print("https://github.com/ihabunek/toot")
|
||||
|
||||
@ -132,21 +146,52 @@ def cmd_auth(app, user):
|
||||
print("You are not logged in")
|
||||
|
||||
|
||||
def main():
|
||||
command = sys.argv[1] if len(sys.argv) > 1 else None
|
||||
def cmd_logout(app, user):
|
||||
os.unlink(CONFIG_APP_FILE)
|
||||
os.unlink(CONFIG_USER_FILE)
|
||||
print("You are now logged out")
|
||||
|
||||
if os.getenv('TOOT_DEBUG'):
|
||||
import logging
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
|
||||
app = load_app() or create_app_interactive()
|
||||
user = load_user() or login_interactive(app)
|
||||
def run_command(command):
|
||||
app = load_app()
|
||||
user = load_user()
|
||||
|
||||
# Commands which can run when not logged in
|
||||
if command == 'login':
|
||||
return login_interactive(create_app_interactive())
|
||||
|
||||
if command == 'auth':
|
||||
return cmd_auth(app, user)
|
||||
|
||||
# Commands which require user to be logged in
|
||||
if not app or not user:
|
||||
print(red("You are not logged in."))
|
||||
print(red("Please run `toot login` first."))
|
||||
return
|
||||
|
||||
if command == 'logout':
|
||||
return cmd_logout(app, user)
|
||||
|
||||
if command == 'post':
|
||||
cmd_post_status(app, user)
|
||||
elif command == 'auth':
|
||||
cmd_auth(app, user)
|
||||
elif command == 'timeline':
|
||||
cmd_timeline(app, user)
|
||||
else:
|
||||
print_usage()
|
||||
return cmd_post_status(app, user)
|
||||
|
||||
if command == 'timeline':
|
||||
return cmd_timeline(app, user)
|
||||
|
||||
print(red("Unknown command '{}'\n".format(command)))
|
||||
print_usage()
|
||||
|
||||
|
||||
def main():
|
||||
if os.getenv('TOOT_DEBUG'):
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
|
||||
command = sys.argv[1] if len(sys.argv) > 1 else None
|
||||
|
||||
if not command:
|
||||
return print_usage()
|
||||
|
||||
try:
|
||||
run_command(command)
|
||||
except ConsoleError as e:
|
||||
print(red(str(e)))
|
||||
|
Loading…
Reference in New Issue
Block a user