Debian Codegraph @

scripts/register_feed2toot_app

raw symbols SCIP
1 #!/usr/bin/env python3
2 # vim:ts=4:sw=4:ft=python:fileencoding=utf-8
3 # Copyright © 2015-2021 Carl Chenet <[email protected]>
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>
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 # get the instance
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 # get the username
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 # get the password
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)