Пример #1
0
def upload(app, user, args):
    response = _do_upload(app, user, args.file)

    print("\nSuccessfully uploaded media ID {}, type '{}'".format(
        yellow(response['id']), yellow(response['type'])))
    print("Original URL: " + green(response['url']))
    print("Preview URL:  " + green(response['preview_url']))
    print("Text URL:     " + green(response['text_url']))
Пример #2
0
def auth(app, user, args):
    if app and user:
        print("You are logged in to {} as {}\n".format(yellow(app.instance),
                                                       yellow(user.username)))
        print("User data: " + green(config.get_user_config_path()))
        print("App data:  " +
              green(config.get_instance_config_path(app.instance)))
    else:
        print("You are not logged in")
Пример #3
0
def two_factor_login_interactive(app):
    """Hacky implementation of two factor authentication"""

    print("Log in to " + green(app.instance))
    email = input('Email: ')
    password = getpass('Password: '******'/auth/sign_in'

    session = requests.Session()

    # Fetch sign in form
    response = session.get(sign_in_url)
    response.raise_for_status()

    soup = BeautifulSoup(response.content, "html.parser")
    form = soup.find('form')
    inputs = form.find_all('input')

    data = {i.attrs.get('name'): i.attrs.get('value') for i in inputs}
    data['user[email]'] = email
    data['user[password]'] = password

    # Submit form, get 2FA entry form
    response = session.post(sign_in_url, data)
    response.raise_for_status()

    soup = BeautifulSoup(response.content, "html.parser")
    form = soup.find('form')
    inputs = form.find_all('input')

    data = {i.attrs.get('name'): i.attrs.get('value') for i in inputs}
    data['user[otp_attempt]'] = input("2FA Token: ")

    # Submit token
    response = session.post(sign_in_url, data)
    response.raise_for_status()

    # Extract access token from response
    soup = BeautifulSoup(response.content, "html.parser")
    initial_state = soup.find('script', id='initial-state')

    if not initial_state:
        raise ConsoleError("Login failed: Invalid 2FA token?")

    data = json.loads(initial_state.get_text())
    access_token = data['meta']['access_token']

    user = User(app.instance, email, access_token)
    path = config.save_user(user)
    print("Access token saved to: " + green(path))
Пример #4
0
def register_app(instance):
    print("Registering application with %s" % green(instance))

    try:
        response = api.create_app(instance)
    except:
        raise ConsoleError(
            "Registration failed. Did you enter a valid instance?")

    base_url = 'https://' + instance

    app = App(instance, base_url, response['client_id'],
              response['client_secret'])
    path = config.save_app(app)
    print("Application tokens saved to: {}\n".format(green(path)))

    return app
Пример #5
0
def _print_accounts(accounts):
    if not accounts:
        return

    print("\nAccounts:")
    for account in accounts:
        acct = green("@{}".format(account['acct']))
        display_name = account['display_name']
        print("* {} {}".format(acct, display_name))
Пример #6
0
def _print_account(account):
    print("{} {}".format(green("@" + account['acct']),
                         account['display_name']))

    if account['note']:
        print("")
        note = BeautifulSoup(account['note'], "html.parser")
        print("\n".join(wrap(note.get_text())))

    print("")
    print("ID: " + green(account['id']))
    print("Since: " + green(account['created_at'][:19].replace('T', ' @ ')))
    print("")
    print("Followers: " + yellow(account['followers_count']))
    print("Following: " + yellow(account['following_count']))
    print("Statuses: " + yellow(account['statuses_count']))
    print("")
    print(account['url'])
Пример #7
0
def unfollow(app, user, args):
    account = _find_account(app, user, args.account)

    if not account:
        print_error("Account not found")
        return

    api.unfollow(app, user, account['id'])

    print(green("✓ You are no longer following %s" % args.account))
Пример #8
0
def login_interactive(app):
    print("\nLog in to " + green(app.instance))
    email = input('Email: ')
    password = getpass('Password: '******'access_token'])
    path = config.save_user(user)
    print("Access token saved to: " + green(path))

    return user
Пример #9
0
def login_2fa(app, user, args):
    print()
    print(yellow("Two factor authentication is experimental."))
    print(yellow("If you have problems logging in, please open an issue:"))
    print(yellow("https://github.com/ihabunek/toot/issues"))
    print()

    app = create_app_interactive()
    two_factor_login_interactive(app)

    print()
    print(green("✓ Successfully logged in."))
Пример #10
0
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)

    print("Toot posted: " + green(response.get('url')))
Пример #11
0
def _print_hashtags(hashtags):
    if not hashtags:
        return

    print("\nHashtags:")
    print(", ".join([green("#" + t) for t in hashtags]))
Пример #12
0
def create_app_interactive():
    instance = input("Choose an instance [%s]: " % green(DEFAULT_INSTANCE))
    if not instance:
        instance = DEFAULT_INSTANCE

    return config.load_app(instance) or register_app(instance)
Пример #13
0
def unblock(app, user, args):
    account = _find_account(app, user, args.account)
    api.unblock(app, user, account['id'])
    print(green("✓ %s is no longer blocked" % args.account))
Пример #14
0
def block(app, user, args):
    account = _find_account(app, user, args.account)
    api.block(app, user, account['id'])
    print(green("✓ You are now blocking %s" % args.account))
Пример #15
0
def mute(app, user, args):
    account = _find_account(app, user, args.account)
    api.mute(app, user, account['id'])
    print(green("✓ You have muted %s" % args.account))
Пример #16
0
def unfollow(app, user, args):
    account = _find_account(app, user, args.account)
    api.unfollow(app, user, account['id'])
    print(green("✓ You are no longer following %s" % args.account))
Пример #17
0
def login(app, user, args):
    app = create_app_interactive()
    login_interactive(app)

    print()
    print(green("✓ Successfully logged in."))
Пример #18
0
def _do_upload(app, user, file):
    print("Uploading media: {}".format(green(file.name)))
    return api.upload_media(app, user, file)
Пример #19
0
def logout(app, user, args):
    config.delete_user()

    print(green("✓ You are now logged out"))