Require an explicit login - toot - Unnamed repository; edit this file 'description' to name the repository.
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) LICENSE
---
(DIR) commit 9e32f7f8dd04be6826821def7e91fa30c313dba7
(DIR) parent 4e742122032b74f2abe6a32a98aaf16a1521a028
(HTM) Author: Ivan Habunek <ivan@habunek.com>
Date: Thu, 13 Apr 2017 13:52:28 +0200
Require an explicit login
Diffstat:
README.rst | 41 ++++++++++++++++++++++++++-----
toot/console.py | 85 +++++++++++++++++++++++--------
2 files changed, 100 insertions(+), 26 deletions(-)
---
(DIR) diff --git a/README.rst b/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
+
+You will be asked to chose an instance_ and enter your credentials.
-On first use, you will be asked to choose a Mastodon instance and log in.
+.. _instance: https://github.com/tootsuite/documentation/blob/master/Using-Mastodon/List-of-Mastodon-instances.md
-The app and user tokens are saved in two files in your home directory:
+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!"
(DIR) diff --git a/toot/console.py b/toot/console.py
@@ -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")
+
+
+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':
+ 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'):
- import logging
logging.basicConfig(level=logging.DEBUG)
- app = load_app() or create_app_interactive()
- user = load_user() or login_interactive(app)
+ command = sys.argv[1] if len(sys.argv) > 1 else None
- 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()
+ if not command:
+ return print_usage()
+
+ try:
+ run_command(command)
+ except ConsoleError as e:
+ print(red(str(e)))