| 1 |
|
| 2 |
|
| 3 |
> |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
from argparse import ArgumentParser |
| 18 |
from getpass import getpass |
| 19 |
from os import getcwd |
| 20 |
from os import linesep |
| 21 |
from os import sep |
| 22 |
from mastodon import Mastodon |
| 23 |
from mastodon.Mastodon import MastodonIllegalArgumentError |
| 24 |
import sys |
| 25 |
|
| 26 |
__version__ = '0.2' |
| 27 |
|
| 28 |
epilog = 'For more information: https://feed2toot.readthedocs.io' |
| 29 |
description = 'Create a Mastodon app for Feed2toot' |
| 30 |
parser = ArgumentParser(prog='register_feed2toot_app', |
| 31 |
description=description, |
| 32 |
epilog=epilog) |
| 33 |
parser.add_argument('--version', action='version', version=__version__) |
| 34 |
parser.add_argument('--client-credentials-file', dest='clientcredfile', help='the name of the client credentials for the Mastodon app', default='feed2toot_clientcred.txt') |
| 35 |
parser.add_argument('--user-credentials-file', dest='usercredfile', help='the name of the user credentials for the Mastodon app', default='feed2toot_usercred.txt') |
| 36 |
parser.add_argument('--name', help='the name of the Mastodon app', default='feed2toot') |
| 37 |
opts = parser.parse_args() |
| 38 |
|
| 39 |
clientcredfile=opts.clientcredfile |
| 40 |
usercredfile=opts.usercredfile |
| 41 |
|
| 42 |
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()) |
| 43 |
print(headline) |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
instance = input('Mastodon instance URL (defaults to https://mastodon.social): ') |
| 48 |
if not instance: |
| 49 |
instance = 'https://mastodon.social' |
| 50 |
elif not instance.startswith('http'): |
| 51 |
instance = ''.join(['https://', instance]) |
| 52 |
|
| 53 |
|
| 54 |
userok = False |
| 55 |
while not userok: |
| 56 |
user = input('Mastodon login: ') |
| 57 |
if not user: |
| 58 |
print('Your Mastodon username can not be empty.') |
| 59 |
userok = False |
| 60 |
elif '@' not in user or '.' not in user: |
| 61 |
print('Your Mastodon username should be an email.') |
| 62 |
userok = False |
| 63 |
else: |
| 64 |
userok = True |
| 65 |
|
| 66 |
|
| 67 |
password = getpass(prompt='Mastodon password: ') |
| 68 |
Mastodon.create_app( |
| 69 |
opts.name, |
| 70 |
api_base_url=instance, |
| 71 |
to_file = '{cwd}{sep}{clientcredfile}'.format(cwd=getcwd(), sep=sep, clientcredfile=clientcredfile) |
| 72 |
) |
| 73 |
mastodon = Mastodon(client_id = '{cwd}{sep}{clientcredfile}'.format(cwd=getcwd(), sep=sep, clientcredfile=clientcredfile), |
| 74 |
api_base_url=instance) |
| 75 |
try: |
| 76 |
mastodon.log_in( |
| 77 |
user, |
| 78 |
password, |
| 79 |
to_file = '{cwd}{sep}{usercredfile}'.format(cwd=getcwd(), sep=sep, usercredfile=usercredfile) |
| 80 |
) |
| 81 |
except MastodonIllegalArgumentError as err: |
| 82 |
print(err) |
| 83 |
sys.exit('{linesep}I guess you entered a bad login or password.{linesep}'.format(linesep=linesep)) |
| 84 |
|
| 85 |
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, |
| 86 |
linesep=linesep, |
| 87 |
clientcredfile=clientcredfile, |
| 88 |
usercredfile=usercredfile) |
| 89 |
print(summary) |
| 90 |
sys.exit(0) |