kopia lustrzana https://github.com/ihabunek/toot
register
rodzic
92b9b4ef18
commit
1801f04b6a
|
@ -358,3 +358,7 @@ def clear_notifications(app, user):
|
|||
def get_instance(domain, scheme="https"):
|
||||
url = "{}://{}/api/v1/instance".format(scheme, domain)
|
||||
return http.anon_get(url).json()
|
||||
|
||||
|
||||
def get_instance_by_url(url):
|
||||
return http.anon_get(f"{url}/api/v1/instance").json()
|
||||
|
|
|
@ -1,13 +1,19 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
import sys
|
||||
import textwrap
|
||||
|
||||
from pprint import pprint
|
||||
from urllib.parse import urlparse
|
||||
|
||||
from urwid.escape import re
|
||||
|
||||
from toot import api, config
|
||||
from toot.auth import login_interactive, login_browser_interactive, create_app_interactive
|
||||
from toot.exceptions import ConsoleError, NotFoundError
|
||||
from toot.output import (print_out, print_instance, print_account, print_acct_list,
|
||||
print_search_results, print_timeline, print_notifications)
|
||||
from toot.utils import assert_domain_exists, editor_input, multiline_input, EOF_KEY
|
||||
from toot.output import (print_err, print_out, print_instance, print_account, print_acct_list,
|
||||
print_search_results, print_timeline, print_notifications, prompt)
|
||||
from toot.utils import assert_domain_exists, editor_input, get_text, is_url, multiline_input, EOF_KEY, parse_html
|
||||
|
||||
|
||||
def get_timeline_generator(app, user, args):
|
||||
|
@ -197,6 +203,31 @@ def auth(app, user, args):
|
|||
print_out("\nAuth tokens are stored in: <blue>{}</blue>".format(path))
|
||||
|
||||
|
||||
def register(app, user, args):
|
||||
instance_url = args.instance or prompt("Instance URL", validate=is_url)
|
||||
instance_url = instance_url.rstrip("/")
|
||||
|
||||
parsed_url = urlparse(instance_url)
|
||||
assert_domain_exists(parsed_url.netloc)
|
||||
base_url = f"{parsed_url.scheme}://{parsed_url.netloc}"
|
||||
print(base_url)
|
||||
|
||||
try:
|
||||
instance = api.get_instance_by_url(base_url)
|
||||
except NotFoundError:
|
||||
raise ConsoleError(f"Mastdon instance not found at {base_url}")
|
||||
|
||||
print_out()
|
||||
print_instance(instance)
|
||||
|
||||
|
||||
# pprint(instance)
|
||||
|
||||
# app = create_app_interactive(instance=args.instance, scheme=args.scheme)
|
||||
# print(app)
|
||||
# print(user)
|
||||
|
||||
|
||||
def login_cli(app, user, args):
|
||||
app = create_app_interactive(instance=args.instance, scheme=args.scheme)
|
||||
login_interactive(app, args.email)
|
||||
|
|
|
@ -10,6 +10,7 @@ from collections import namedtuple
|
|||
from toot import config, commands, CLIENT_NAME, CLIENT_WEBSITE, __version__
|
||||
from toot.exceptions import ApiError, ConsoleError
|
||||
from toot.output import print_out, print_err
|
||||
from toot.utils import is_url
|
||||
|
||||
VISIBILITY_CHOICES = ['public', 'unlisted', 'private', 'direct']
|
||||
|
||||
|
@ -55,6 +56,12 @@ def editor(value):
|
|||
return exe
|
||||
|
||||
|
||||
def url(value):
|
||||
if not is_url(value):
|
||||
raise ArgumentTypeError("Invalid URL")
|
||||
return value
|
||||
|
||||
|
||||
Command = namedtuple("Command", ["name", "description", "require_auth", "arguments"])
|
||||
|
||||
|
||||
|
@ -93,6 +100,11 @@ instance_arg = (["-i", "--instance"], {
|
|||
"help": 'mastodon instance to log into e.g. "mastodon.social"',
|
||||
})
|
||||
|
||||
instance_url_arg = (["-i", "--instance"], {
|
||||
"type": url,
|
||||
"help": "Mastodon instance URL",
|
||||
})
|
||||
|
||||
email_arg = (["-e", "--email"], {
|
||||
"type": str,
|
||||
"help": 'email address to log in with',
|
||||
|
@ -156,6 +168,12 @@ timeline_args = common_timeline_args + [
|
|||
]
|
||||
|
||||
AUTH_COMMANDS = [
|
||||
Command(
|
||||
name="register",
|
||||
description="Register a new account",
|
||||
arguments=[instance_url_arg],
|
||||
require_auth=False,
|
||||
),
|
||||
Command(
|
||||
name="login",
|
||||
description="Log into a mastodon instance using your browser (recommended)",
|
||||
|
|
|
@ -72,6 +72,27 @@ USE_ANSI_COLOR = use_ansi_color()
|
|||
QUIET = "--quiet" in sys.argv
|
||||
|
||||
|
||||
def prompt(caption, default=None, validate=None):
|
||||
default_label = f" [default: {default}]" if default else ""
|
||||
|
||||
while True:
|
||||
print_out(f"{caption}{default_label}: ", end="")
|
||||
value = input().strip()
|
||||
|
||||
if not value:
|
||||
if default:
|
||||
return default
|
||||
else:
|
||||
continue
|
||||
|
||||
if validate:
|
||||
if not validate(value):
|
||||
print_out("<red>Invalid value</red>")
|
||||
continue
|
||||
|
||||
return value
|
||||
|
||||
|
||||
def print_out(*args, **kwargs):
|
||||
if not QUIET:
|
||||
args = [colorize(a) if USE_ANSI_COLOR else strip_tags(a) for a in args]
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
from argparse import ArgumentTypeError
|
||||
import os
|
||||
import re
|
||||
import socket
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unicodedata
|
||||
from urllib.parse import urlparse
|
||||
import warnings
|
||||
|
||||
from bs4 import BeautifulSoup
|
||||
|
@ -116,3 +118,8 @@ def editor_input(editor, initial_text):
|
|||
text = f.read().decode()
|
||||
|
||||
return text.split(EDITOR_DIVIDER)[0].strip()
|
||||
|
||||
|
||||
def is_url(value):
|
||||
url = urlparse(value)
|
||||
return all((url.scheme, url.netloc))
|
||||
|
|
Ładowanie…
Reference in New Issue