2017-04-19 12:47:30 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2019-08-22 15:10:37 +00:00
|
|
|
import sys
|
|
|
|
|
2017-12-30 12:32:52 +00:00
|
|
|
from toot import api, config
|
2017-12-30 11:52:55 +00:00
|
|
|
from toot.auth import login_interactive, login_browser_interactive, create_app_interactive
|
2017-12-30 15:30:35 +00:00
|
|
|
from toot.exceptions import ConsoleError, NotFoundError
|
2019-02-17 13:03:11 +00:00
|
|
|
from toot.output import (print_out, print_instance, print_account,
|
2019-04-16 12:09:54 +00:00
|
|
|
print_search_results, print_timeline, print_notifications)
|
2019-08-22 15:10:37 +00:00
|
|
|
from toot.utils import assert_domain_exists, editor_input, multiline_input, EOF_KEY
|
2017-04-19 12:47:30 +00:00
|
|
|
|
|
|
|
|
2019-02-15 13:06:47 +00:00
|
|
|
def get_timeline_generator(app, user, args):
|
2018-06-12 08:40:36 +00:00
|
|
|
# Make sure tag, list and public are not used simultaneously
|
2018-06-12 09:53:10 +00:00
|
|
|
if len([arg for arg in [args.tag, args.list, args.public] if arg]) > 1:
|
|
|
|
raise ConsoleError("Only one of --public, --tag, or --list can be used at one time.")
|
2018-06-12 08:40:36 +00:00
|
|
|
|
|
|
|
if args.local and not (args.public or args.tag):
|
|
|
|
raise ConsoleError("The --local option is only valid alongside --public or --tag.")
|
|
|
|
|
2019-02-15 13:06:47 +00:00
|
|
|
if args.instance and not (args.public or args.tag):
|
|
|
|
raise ConsoleError("The --instance option is only valid alongside --public or --tag.")
|
|
|
|
|
2018-06-12 08:40:36 +00:00
|
|
|
if args.public:
|
2020-05-11 10:54:35 +00:00
|
|
|
return api.public_timeline_generator(app, user, local=args.local, limit=args.count)
|
2018-06-12 08:40:36 +00:00
|
|
|
elif args.tag:
|
2020-05-11 10:54:35 +00:00
|
|
|
return api.tag_timeline_generator(app, user, args.tag, local=args.local, limit=args.count)
|
2018-06-12 09:53:10 +00:00
|
|
|
elif args.list:
|
2019-02-15 13:06:47 +00:00
|
|
|
return api.timeline_list_generator(app, user, args.list, limit=args.count)
|
2018-03-05 18:13:45 +00:00
|
|
|
else:
|
2019-02-15 13:06:47 +00:00
|
|
|
return api.home_timeline_generator(app, user, limit=args.count)
|
|
|
|
|
|
|
|
|
|
|
|
def timeline(app, user, args):
|
|
|
|
generator = get_timeline_generator(app, user, args)
|
2018-06-12 08:40:36 +00:00
|
|
|
|
2019-02-13 13:15:47 +00:00
|
|
|
while(True):
|
2019-02-15 13:06:47 +00:00
|
|
|
try:
|
|
|
|
items = next(generator)
|
|
|
|
except StopIteration:
|
|
|
|
print_out("That's all folks.")
|
|
|
|
return
|
2019-02-13 13:15:47 +00:00
|
|
|
|
|
|
|
if args.reverse:
|
|
|
|
items = reversed(items)
|
|
|
|
|
|
|
|
print_timeline(items)
|
|
|
|
|
|
|
|
if args.once:
|
|
|
|
break
|
|
|
|
|
|
|
|
char = input("\nContinue? [Y/n] ")
|
|
|
|
if char.lower() == "n":
|
|
|
|
break
|
2018-07-25 18:40:59 +00:00
|
|
|
|
2017-04-19 12:47:30 +00:00
|
|
|
|
2019-01-19 17:38:17 +00:00
|
|
|
def thread(app, user, args):
|
|
|
|
toot = api.single_status(app, user, args.status_id)
|
|
|
|
context = api.context(app, user, args.status_id)
|
|
|
|
thread = []
|
|
|
|
for item in context['ancestors']:
|
|
|
|
thread.append(item)
|
|
|
|
|
|
|
|
thread.append(toot)
|
|
|
|
|
|
|
|
for item in context['descendants']:
|
|
|
|
thread.append(item)
|
|
|
|
|
|
|
|
print_timeline(thread)
|
2017-04-19 12:47:30 +00:00
|
|
|
|
2019-02-13 13:15:47 +00:00
|
|
|
|
2017-04-21 18:23:48 +00:00
|
|
|
def curses(app, user, args):
|
2019-02-15 13:06:47 +00:00
|
|
|
generator = get_timeline_generator(app, user, args)
|
2018-01-06 10:56:52 +00:00
|
|
|
from toot.ui.app import TimelineApp
|
2019-01-03 15:52:31 +00:00
|
|
|
TimelineApp(app, user, generator).run()
|
2017-04-21 18:23:48 +00:00
|
|
|
|
|
|
|
|
2017-04-19 12:47:30 +00:00
|
|
|
def post(app, user, args):
|
2019-08-22 15:10:37 +00:00
|
|
|
# TODO: this might be achievable, explore options
|
|
|
|
if args.editor and not sys.stdin.isatty():
|
|
|
|
raise ConsoleError("Cannot run editor if not in tty.")
|
|
|
|
|
2019-08-01 10:56:21 +00:00
|
|
|
if args.media and len(args.media) > 4:
|
|
|
|
raise ConsoleError("Cannot attach more than 4 files.")
|
|
|
|
|
2019-08-22 15:10:37 +00:00
|
|
|
# Read any text that might be piped to stdin
|
|
|
|
if not args.text and not sys.stdin.isatty():
|
|
|
|
args.text = sys.stdin.read().rstrip()
|
|
|
|
|
2017-04-19 12:47:30 +00:00
|
|
|
if args.media:
|
2019-08-01 10:56:21 +00:00
|
|
|
media = [_do_upload(app, user, file) for file in args.media]
|
|
|
|
media_ids = [m["id"] for m in media]
|
2017-04-19 12:47:30 +00:00
|
|
|
else:
|
2017-12-30 15:42:52 +00:00
|
|
|
media = None
|
2017-04-19 12:47:30 +00:00
|
|
|
media_ids = None
|
|
|
|
|
2017-12-30 15:42:52 +00:00
|
|
|
if media and not args.text:
|
2019-08-01 10:56:21 +00:00
|
|
|
args.text = "\n".join(m['text_url'] for m in media)
|
2017-12-30 15:42:52 +00:00
|
|
|
|
2019-08-22 15:10:37 +00:00
|
|
|
if args.editor:
|
|
|
|
args.text = editor_input(args.editor, args.text)
|
|
|
|
elif not args.text:
|
2019-01-02 09:49:49 +00:00
|
|
|
print_out("Write or paste your toot. Press <yellow>{}</yellow> to post it.".format(EOF_KEY))
|
|
|
|
args.text = multiline_input()
|
|
|
|
|
2017-12-30 15:42:52 +00:00
|
|
|
if not args.text:
|
|
|
|
raise ConsoleError("You must specify either text or media to post.")
|
|
|
|
|
2018-06-07 08:04:50 +00:00
|
|
|
response = api.post_status(
|
|
|
|
app, user, args.text,
|
|
|
|
visibility=args.visibility,
|
|
|
|
media_ids=media_ids,
|
|
|
|
sensitive=args.sensitive,
|
|
|
|
spoiler_text=args.spoiler_text,
|
2018-06-13 10:43:31 +00:00
|
|
|
in_reply_to_id=args.reply_to,
|
2019-07-30 14:13:29 +00:00
|
|
|
language=args.language,
|
2018-06-07 08:04:50 +00:00
|
|
|
)
|
2017-04-19 12:47:30 +00:00
|
|
|
|
2017-05-08 07:09:20 +00:00
|
|
|
print_out("Toot posted: <green>{}</green>".format(response.get('url')))
|
2017-04-19 12:47:30 +00:00
|
|
|
|
|
|
|
|
2018-06-14 08:40:16 +00:00
|
|
|
def delete(app, user, args):
|
|
|
|
api.delete_status(app, user, args.status_id)
|
|
|
|
print_out("<green>✓ Status deleted</green>")
|
|
|
|
|
|
|
|
|
2019-01-02 11:24:38 +00:00
|
|
|
def favourite(app, user, args):
|
|
|
|
api.favourite(app, user, args.status_id)
|
|
|
|
print_out("<green>✓ Status favourited</green>")
|
|
|
|
|
|
|
|
|
|
|
|
def unfavourite(app, user, args):
|
|
|
|
api.unfavourite(app, user, args.status_id)
|
|
|
|
print_out("<green>✓ Status unfavourited</green>")
|
|
|
|
|
|
|
|
|
|
|
|
def reblog(app, user, args):
|
|
|
|
api.reblog(app, user, args.status_id)
|
|
|
|
print_out("<green>✓ Status reblogged</green>")
|
|
|
|
|
|
|
|
|
|
|
|
def unreblog(app, user, args):
|
|
|
|
api.unreblog(app, user, args.status_id)
|
|
|
|
print_out("<green>✓ Status unreblogged</green>")
|
|
|
|
|
|
|
|
|
|
|
|
def pin(app, user, args):
|
|
|
|
api.pin(app, user, args.status_id)
|
|
|
|
print_out("<green>✓ Status pinned</green>")
|
|
|
|
|
|
|
|
|
|
|
|
def unpin(app, user, args):
|
|
|
|
api.unpin(app, user, args.status_id)
|
|
|
|
print_out("<green>✓ Status unpinned</green>")
|
|
|
|
|
|
|
|
|
2019-01-24 08:36:25 +00:00
|
|
|
def reblogged_by(app, user, args):
|
|
|
|
for account in api.reblogged_by(app, user, args.status_id):
|
|
|
|
print_out("{}\n @{}".format(account['display_name'], account['acct']))
|
|
|
|
|
|
|
|
|
2017-04-19 12:47:30 +00:00
|
|
|
def auth(app, user, args):
|
2018-01-02 09:44:32 +00:00
|
|
|
config_data = config.load_config()
|
|
|
|
|
|
|
|
if not config_data["users"]:
|
|
|
|
print_out("You are not logged in to any accounts")
|
|
|
|
return
|
|
|
|
|
|
|
|
active_user = config_data["active_user"]
|
|
|
|
|
|
|
|
print_out("Authenticated accounts:")
|
|
|
|
for uid, u in config_data["users"].items():
|
|
|
|
active_label = "ACTIVE" if active_user == uid else ""
|
|
|
|
print_out("* <green>{}</green> <yellow>{}</yellow>".format(uid, active_label))
|
|
|
|
|
|
|
|
path = config.get_config_file_path()
|
|
|
|
print_out("\nAuth tokens are stored in: <blue>{}</blue>".format(path))
|
2017-04-19 12:47:30 +00:00
|
|
|
|
|
|
|
|
2018-06-15 07:39:28 +00:00
|
|
|
def login_cli(app, user, args):
|
2018-12-25 01:20:30 +00:00
|
|
|
app = create_app_interactive(instance=args.instance, scheme=args.scheme)
|
2017-08-26 09:33:36 +00:00
|
|
|
login_interactive(app, args.email)
|
2017-04-19 12:47:30 +00:00
|
|
|
|
2017-05-08 07:09:20 +00:00
|
|
|
print_out()
|
|
|
|
print_out("<green>✓ Successfully logged in.</green>")
|
2017-04-19 12:47:30 +00:00
|
|
|
|
|
|
|
|
2018-06-15 07:39:28 +00:00
|
|
|
def login(app, user, args):
|
2018-12-25 01:20:30 +00:00
|
|
|
app = create_app_interactive(instance=args.instance, scheme=args.scheme)
|
2017-12-30 11:52:55 +00:00
|
|
|
login_browser_interactive(app)
|
2017-08-26 12:39:53 +00:00
|
|
|
|
|
|
|
print_out()
|
|
|
|
print_out("<green>✓ Successfully logged in.</green>")
|
2017-04-19 12:47:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
def logout(app, user, args):
|
2018-01-02 09:44:32 +00:00
|
|
|
user = config.load_user(args.account, throw=True)
|
|
|
|
config.delete_user(user)
|
|
|
|
print_out("<green>✓ User {} logged out</green>".format(config.user_id(user)))
|
|
|
|
|
2017-04-19 12:47:30 +00:00
|
|
|
|
2018-01-02 09:44:32 +00:00
|
|
|
def activate(app, user, args):
|
|
|
|
user = config.load_user(args.account, throw=True)
|
|
|
|
config.activate_user(user)
|
|
|
|
print_out("<green>✓ User {} active</green>".format(config.user_id(user)))
|
2017-04-19 12:47:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
def upload(app, user, args):
|
|
|
|
response = _do_upload(app, user, args.file)
|
|
|
|
|
2017-12-30 12:14:37 +00:00
|
|
|
msg = "Successfully uploaded media ID <yellow>{}</yellow>, type '<yellow>{}</yellow>'"
|
|
|
|
|
2017-05-08 07:09:20 +00:00
|
|
|
print_out()
|
2017-12-30 12:14:37 +00:00
|
|
|
print_out(msg.format(response['id'], response['type']))
|
2017-05-08 07:09:20 +00:00
|
|
|
print_out("Original URL: <green>{}</green>".format(response['url']))
|
|
|
|
print_out("Preview URL: <green>{}</green>".format(response['preview_url']))
|
|
|
|
print_out("Text URL: <green>{}</green>".format(response['text_url']))
|
2017-04-19 12:47:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
def search(app, user, args):
|
|
|
|
response = api.search(app, user, args.query, args.resolve)
|
2017-12-29 13:42:51 +00:00
|
|
|
print_search_results(response)
|
2017-04-19 12:47:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _do_upload(app, user, file):
|
2017-05-08 07:09:20 +00:00
|
|
|
print_out("Uploading media: <green>{}</green>".format(file.name))
|
2017-04-19 12:47:30 +00:00
|
|
|
return api.upload_media(app, user, file)
|
|
|
|
|
|
|
|
|
|
|
|
def _find_account(app, user, account_name):
|
2017-05-07 08:42:04 +00:00
|
|
|
"""For a given account name, returns the Account object.
|
2017-04-19 12:47:30 +00:00
|
|
|
|
2017-05-07 08:42:04 +00:00
|
|
|
Raises an exception if not found.
|
|
|
|
"""
|
|
|
|
if not account_name:
|
|
|
|
raise ConsoleError("Empty account name given")
|
|
|
|
|
|
|
|
accounts = api.search_accounts(app, user, account_name)
|
|
|
|
|
|
|
|
if account_name[0] == "@":
|
|
|
|
account_name = account_name[1:]
|
|
|
|
|
|
|
|
for account in accounts:
|
|
|
|
if account['acct'] == account_name:
|
2017-04-19 12:47:30 +00:00
|
|
|
return account
|
|
|
|
|
2017-04-19 13:29:40 +00:00
|
|
|
raise ConsoleError("Account not found")
|
|
|
|
|
|
|
|
|
2017-04-19 12:47:30 +00:00
|
|
|
def follow(app, user, args):
|
|
|
|
account = _find_account(app, user, args.account)
|
|
|
|
api.follow(app, user, account['id'])
|
2017-05-08 07:09:20 +00:00
|
|
|
print_out("<green>✓ You are now following {}</green>".format(args.account))
|
2017-04-19 12:47:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
def unfollow(app, user, args):
|
|
|
|
account = _find_account(app, user, args.account)
|
2017-04-26 09:49:21 +00:00
|
|
|
api.unfollow(app, user, account['id'])
|
2017-05-08 07:09:20 +00:00
|
|
|
print_out("<green>✓ You are no longer following {}</green>".format(args.account))
|
2017-04-19 12:47:30 +00:00
|
|
|
|
|
|
|
|
2017-04-26 09:49:21 +00:00
|
|
|
def mute(app, user, args):
|
|
|
|
account = _find_account(app, user, args.account)
|
|
|
|
api.mute(app, user, account['id'])
|
2017-05-08 07:09:20 +00:00
|
|
|
print_out("<green>✓ You have muted {}</green>".format(args.account))
|
2017-04-19 12:47:30 +00:00
|
|
|
|
2017-04-26 09:49:21 +00:00
|
|
|
|
|
|
|
def unmute(app, user, args):
|
|
|
|
account = _find_account(app, user, args.account)
|
|
|
|
api.unmute(app, user, account['id'])
|
2017-05-08 07:09:20 +00:00
|
|
|
print_out("<green>✓ {} is no longer muted</green>".format(args.account))
|
2017-04-26 09:49:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
def block(app, user, args):
|
|
|
|
account = _find_account(app, user, args.account)
|
|
|
|
api.block(app, user, account['id'])
|
2017-05-08 07:09:20 +00:00
|
|
|
print_out("<green>✓ You are now blocking {}</green>".format(args.account))
|
2017-04-26 09:49:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
def unblock(app, user, args):
|
|
|
|
account = _find_account(app, user, args.account)
|
|
|
|
api.unblock(app, user, account['id'])
|
2017-05-08 07:09:20 +00:00
|
|
|
print_out("<green>✓ {} is no longer blocked</green>".format(args.account))
|
2017-04-19 12:47:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
def whoami(app, user, args):
|
2017-04-19 13:29:40 +00:00
|
|
|
account = api.verify_credentials(app, user)
|
2017-12-29 13:42:51 +00:00
|
|
|
print_account(account)
|
2017-04-19 12:47:30 +00:00
|
|
|
|
2017-04-19 13:29:40 +00:00
|
|
|
|
|
|
|
def whois(app, user, args):
|
|
|
|
account = _find_account(app, user, args.account)
|
2017-12-29 13:42:51 +00:00
|
|
|
print_account(account)
|
2017-12-29 13:26:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def instance(app, user, args):
|
|
|
|
name = args.instance or (app and app.instance)
|
|
|
|
if not name:
|
|
|
|
raise ConsoleError("Please specify instance name.")
|
|
|
|
|
2017-12-30 15:30:35 +00:00
|
|
|
assert_domain_exists(name)
|
|
|
|
|
|
|
|
try:
|
2018-12-30 08:53:12 +00:00
|
|
|
instance = api.get_instance(name, args.scheme)
|
2017-12-30 15:30:35 +00:00
|
|
|
print_instance(instance)
|
|
|
|
except NotFoundError:
|
|
|
|
raise ConsoleError(
|
|
|
|
"Instance not found at {}.\n"
|
|
|
|
"The given domain probably does not host a Mastodon instance.".format(name)
|
|
|
|
)
|
2019-02-17 13:03:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
def notifications(app, user, args):
|
2019-02-17 13:18:51 +00:00
|
|
|
if args.clear:
|
|
|
|
api.clear_notifications(app, user)
|
|
|
|
print_out("<green>Cleared notifications</green>")
|
|
|
|
return
|
|
|
|
|
2019-02-17 14:02:05 +00:00
|
|
|
notifications = api.get_notifications(app, user)
|
|
|
|
if not notifications:
|
|
|
|
print_out("<yellow>No notification</yellow>")
|
|
|
|
return
|
|
|
|
|
2019-04-16 12:09:54 +00:00
|
|
|
print_notifications(notifications)
|
2019-08-24 09:20:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
def tui(app, user, args):
|
|
|
|
from .tui.app import TUI
|
|
|
|
TUI.create(app, user).run()
|