feed2toot-docker/scripts/register_feed2toot_app

91 lines
4 KiB
Plaintext
Raw Permalink Normal View History

2017-04-09 08:30:48 +00:00
#!/usr/bin/env python3
# vim:ts=4:sw=4:ft=python:fileencoding=utf-8
2020-12-07 19:33:30 +00:00
# Copyright © 2015-2020 Carl Chenet <carl.chenet@ohmytux.com>
2017-04-09 08:30:48 +00:00
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
from argparse import ArgumentParser
2017-04-09 08:30:48 +00:00
from getpass import getpass
from os import getcwd
from os import linesep
from os import sep
2017-04-09 08:30:48 +00:00
from mastodon import Mastodon
from mastodon.Mastodon import MastodonIllegalArgumentError
import sys
__version__ = '0.2'
epilog = 'For more information: https://feed2toot.readthedocs.io'
description = 'Create a Mastodon app for Feed2toot'
parser = ArgumentParser(prog='register_feed2toot_app',
description=description,
epilog=epilog)
parser.add_argument('--version', action='version', version=__version__)
parser.add_argument('--client-credentials-file', dest='clientcredfile', help='the name of the client credentials for the Mastodon app', default='feed2toot_clientcred.txt')
parser.add_argument('--user-credentials-file', dest='usercredfile', help='the name of the user credentials for the Mastodon app', default='feed2toot_usercred.txt')
parser.add_argument('--name', help='the name of the Mastodon app', default='feed2toot')
opts = parser.parse_args()
clientcredfile=opts.clientcredfile
usercredfile=opts.usercredfile
headline = '{linesep}This script generates the Mastodon application credentials for Feed2toot.{linesep}{clientcredfile} and {usercredfile} will be written{linesep}in the current directory: {cwd}.{linesep}WARNING: previous files with the same names will be overwritten.{linesep}{linesep}A connection is also initiated to create the application.{linesep}Your password is *not* stored.{linesep}'.format(linesep=linesep, clientcredfile=clientcredfile, usercredfile=usercredfile, cwd=getcwd())
print(headline)
2017-04-09 08:30:48 +00:00
# get the instance
instance = input('Mastodon instance URL (defaults to https://mastodon.social): ')
2017-04-09 08:30:48 +00:00
if not instance:
instance = 'https://mastodon.social'
elif not instance.startswith('http'):
instance = ''.join(['https://', instance])
# get the username
userok = False
while not userok:
user = input('Mastodon login: ')
2017-04-09 08:30:48 +00:00
if not user:
print('Your Mastodon username can not be empty.')
2017-04-09 08:30:48 +00:00
userok = False
elif '@' not in user or '.' not in user:
print('Your Mastodon username should be an email.')
2017-04-09 08:30:48 +00:00
userok = False
else:
userok = True
# get the password
password = getpass(prompt='Mastodon password: ')
2017-04-09 08:30:48 +00:00
Mastodon.create_app(
opts.name,
api_base_url=instance,
to_file = '{cwd}{sep}{clientcredfile}'.format(cwd=getcwd(), sep=sep, clientcredfile=clientcredfile)
2017-04-09 08:30:48 +00:00
)
mastodon = Mastodon(client_id = '{cwd}{sep}{clientcredfile}'.format(cwd=getcwd(), sep=sep, clientcredfile=clientcredfile),
2017-04-09 08:30:48 +00:00
api_base_url=instance)
try:
mastodon.log_in(
user,
password,
to_file = '{cwd}{sep}{usercredfile}'.format(cwd=getcwd(), sep=sep, usercredfile=usercredfile)
2017-04-09 08:30:48 +00:00
)
except MastodonIllegalArgumentError as err:
print(err)
sys.exit('{linesep}I guess you entered a bad login or password.{linesep}'.format(linesep=linesep))
summary = '{linesep}The app {appname} was added to your Preferences=>Accounts=>Authorized apps page.{linesep}The file {clientcredfile} and {usercredfile} were created in the current directory.{linesep}'.format(appname=opts.name,
linesep=linesep,
clientcredfile=clientcredfile,
usercredfile=usercredfile)
print(summary)
2017-04-09 08:30:48 +00:00
sys.exit(0)