2017-04-19 12:47:30 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
from datetime import datetime
|
2017-12-12 10:36:08 +00:00
|
|
|
from itertools import zip_longest
|
2017-04-19 12:47:30 +00:00
|
|
|
from itertools import chain
|
2017-12-29 13:42:51 +00:00
|
|
|
from textwrap import TextWrapper
|
2017-04-19 12:47:30 +00:00
|
|
|
|
2017-12-30 11:52:55 +00:00
|
|
|
from toot import api, config, ConsoleError
|
|
|
|
from toot.auth import login_interactive, login_browser_interactive, create_app_interactive
|
2017-12-29 13:42:51 +00:00
|
|
|
from toot.output import print_out, print_instance, print_account, print_search_results
|
2017-04-19 12:47:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _print_timeline(item):
|
|
|
|
def wrap_text(text, width):
|
|
|
|
wrapper = TextWrapper(width=width, break_long_words=False, break_on_hyphens=False)
|
|
|
|
return chain(*[wrapper.wrap(l) for l in text.split("\n")])
|
|
|
|
|
|
|
|
def timeline_rows(item):
|
|
|
|
name = item['name']
|
|
|
|
time = item['time'].strftime('%Y-%m-%d %H:%M%Z')
|
|
|
|
|
|
|
|
left_column = [name, time]
|
|
|
|
if 'reblogged' in item:
|
|
|
|
left_column.append(item['reblogged'])
|
|
|
|
|
|
|
|
text = item['text']
|
|
|
|
|
|
|
|
right_column = wrap_text(text, 80)
|
|
|
|
|
|
|
|
return zip_longest(left_column, right_column, fillvalue="")
|
|
|
|
|
|
|
|
for left, right in timeline_rows(item):
|
2017-05-08 07:09:20 +00:00
|
|
|
print_out("{:30} │ {}".format(left, right))
|
2017-04-19 12:47:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _parse_timeline(item):
|
|
|
|
content = item['reblog']['content'] if item['reblog'] else item['content']
|
|
|
|
reblogged = item['reblog']['account']['username'] if item['reblog'] else ""
|
|
|
|
|
|
|
|
name = item['account']['display_name'] + " @" + item['account']['username']
|
|
|
|
soup = BeautifulSoup(content, "html.parser")
|
|
|
|
text = soup.get_text().replace(''', "'")
|
|
|
|
time = datetime.strptime(item['created_at'], "%Y-%m-%dT%H:%M:%S.%fZ")
|
|
|
|
|
|
|
|
return {
|
|
|
|
"name": name,
|
|
|
|
"text": text,
|
|
|
|
"time": time,
|
|
|
|
"reblogged": reblogged,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def timeline(app, user, args):
|
|
|
|
items = api.timeline_home(app, user)
|
|
|
|
parsed_items = [_parse_timeline(t) for t in items]
|
|
|
|
|
2017-05-08 07:09:20 +00:00
|
|
|
print_out("─" * 31 + "┬" + "─" * 88)
|
2017-04-19 12:47:30 +00:00
|
|
|
for item in parsed_items:
|
|
|
|
_print_timeline(item)
|
2017-05-08 07:09:20 +00:00
|
|
|
print_out("─" * 31 + "┼" + "─" * 88)
|
2017-04-19 12:47:30 +00:00
|
|
|
|
|
|
|
|
2017-04-21 18:23:48 +00:00
|
|
|
def curses(app, user, args):
|
2017-09-09 07:54:13 +00:00
|
|
|
from toot.app import TimelineApp
|
2017-04-21 18:23:48 +00:00
|
|
|
generator = api.timeline_generator(app, user)
|
|
|
|
TimelineApp(generator).run()
|
|
|
|
|
|
|
|
|
2017-04-19 12:47:30 +00:00
|
|
|
def post(app, user, args):
|
|
|
|
if args.media:
|
|
|
|
media = _do_upload(app, user, args.media)
|
|
|
|
media_ids = [media['id']]
|
|
|
|
else:
|
|
|
|
media_ids = None
|
|
|
|
|
|
|
|
response = api.post_status(app, user, args.text, media_ids=media_ids, visibility=args.visibility)
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
def auth(app, user, args):
|
|
|
|
if app and user:
|
2017-05-08 07:09:20 +00:00
|
|
|
print_out("You are logged in to <yellow>{}</yellow> as <yellow>{}</yellow>\n".format(
|
|
|
|
app.instance, user.username))
|
|
|
|
print_out("User data: <green>{}</green>".format(config.get_user_config_path()))
|
|
|
|
print_out("App data: <green>{}</green>".format(config.get_instance_config_path(app.instance)))
|
2017-04-19 12:47:30 +00:00
|
|
|
else:
|
2017-05-08 07:09:20 +00:00
|
|
|
print_out("You are not logged in")
|
2017-04-19 12:47:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
def login(app, user, args):
|
2017-08-26 09:33:36 +00:00
|
|
|
app = create_app_interactive(instance=args.instance)
|
|
|
|
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
|
|
|
|
|
|
|
|
2017-08-26 12:39:53 +00:00
|
|
|
def login_browser(app, user, args):
|
|
|
|
app = create_app_interactive(instance=args.instance)
|
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):
|
|
|
|
config.delete_user()
|
|
|
|
|
2017-05-08 07:09:20 +00:00
|
|
|
print_out("<green>✓ You are now logged out.</green>")
|
2017-04-19 12:47:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
def upload(app, user, args):
|
|
|
|
response = _do_upload(app, user, args.file)
|
|
|
|
|
2017-05-08 07:09:20 +00:00
|
|
|
print_out()
|
|
|
|
print_out("Successfully uploaded media ID <yellow>{}</yellow>, type '<yellow>{}</yellow>'".format(
|
|
|
|
response['id'], response['type']))
|
|
|
|
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.")
|
|
|
|
|
|
|
|
instance = api.get_instance(app, user, name)
|
|
|
|
print_instance(instance)
|