2017-04-09 08:30:48 +00:00
#!/usr/bin/env python3
# vim:ts=4:sw=4:ft=python:fileencoding=utf-8
2021-03-26 17:18:46 +00:00
# Copyright © 2015-2021 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/>
2020-12-08 18:35:56 +00:00
from argparse import ArgumentParser
2017-04-09 08:30:48 +00:00
from getpass import getpass
from os import getcwd
2020-12-08 18:35:56 +00:00
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
2020-12-08 18:35:56 +00:00
__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')
2022-10-23 20:09:20 +00:00
parser.add_argument('--instance', help='the URL of the Mastodon instance')
parser.add_argument('--username', help='the username of your Mastodon account')
parser.add_argument('--password', help='the password of your Mastodon account')
2020-12-08 18:35:56 +00:00
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
2022-10-23 20:09:20 +00:00
instance = opts.instance
if not 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
2022-10-23 20:09:20 +00:00
quit_on_error = True
2017-04-09 08:30:48 +00:00
while not userok:
2022-10-23 20:09:20 +00:00
user = opts.username
if not user:
user = input('Mastodon login: ')
quit_on_error = False
2017-04-09 08:30:48 +00:00
if not user:
2017-05-13 05:55:47 +00:00
print('Your Mastodon username can not be empty.')
2017-04-09 08:30:48 +00:00
elif '@' not in user or '.' not in user:
2017-05-13 05:55:47 +00:00
print('Your Mastodon username should be an email.')
2017-04-09 08:30:48 +00:00
else:
userok = True
2022-10-23 20:09:20 +00:00
if not userok and quit_on_error:
exit()
2017-04-09 08:30:48 +00:00
# get the password
2022-10-23 20:09:20 +00:00
password = opts.password
if not password:
password = getpass(prompt='Mastodon password: ')
2017-04-09 08:30:48 +00:00
Mastodon.create_app(
2020-12-08 18:35:56 +00:00
opts.name,
2017-04-11 13:24:54 +00:00
api_base_url=instance,
2020-12-08 18:35:56 +00:00
to_file = '{cwd}{sep}{clientcredfile}'.format(cwd=getcwd(), sep=sep, clientcredfile=clientcredfile)
2017-04-09 08:30:48 +00:00
)
2020-12-08 18:35:56 +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,
2020-12-08 18:35:56 +00:00
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)
2020-12-08 18:35:56 +00:00
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)