fediverse/setup.py

158 líneas
4,6 KiB
Python
Original Vista normal Històric

2020-05-17 14:14:55 +02:00
import os
import sys
2023-01-05 00:01:36 +01:00
from datetime import datetime
import pytz
from mastodon import Mastodon
from mastodon.Mastodon import MastodonMalformedEventError, MastodonNetworkError, MastodonReadTimeout, MastodonAPIError, MastodonIllegalArgumentError
import pdb
2020-05-17 14:14:55 +02:00
2023-01-05 00:01:36 +01:00
class Setup():
2023-01-05 00:01:36 +01:00
name = 'fediverse setup'
2020-05-17 14:14:55 +02:00
2023-01-05 00:01:36 +01:00
def __init__(self, config_file=None, mastodon_hostname=None, peers_api=None, user_agent=None, secrets_filepath=None, mastodon_app_token=None):
2023-01-05 00:01:36 +01:00
self.config_file = "config/config.txt"
self.mastodon_hostname = self.__get_parameter("mastodon_hostname", self.config_file)
self.peers_api = '/api/v1/instance/peers?'
self.user_agent = {'User-agent': "fediverse's stats (fediverse@mastodont.cat)"}
2020-05-17 14:14:55 +02:00
2023-01-05 00:01:36 +01:00
self.secrets_filepath = 'secrets/secrets.txt'
2023-01-05 00:01:36 +01:00
is_setup = self.__check_mastodon_setup(self)
2020-05-17 14:14:55 +02:00
2023-01-05 00:01:36 +01:00
if is_setup:
2023-01-05 00:01:36 +01:00
self.mastodon_app_token = self.__get_mastodon_parameter("mastodon_app_token", self.secrets_filepath)
2020-05-17 14:14:55 +02:00
2023-01-05 00:01:36 +01:00
else:
2023-01-05 00:01:36 +01:00
self.mastodon_app_token = self.mastodon_setup(self)
2020-05-17 14:14:55 +02:00
2023-01-05 00:01:36 +01:00
@staticmethod
def __check_mastodon_setup(self):
2023-01-05 00:01:36 +01:00
is_setup = False
2023-01-05 00:01:36 +01:00
if not os.path.isfile(self.secrets_filepath):
print(f"File {self.secrets_filepath} not found, running setup.")
else:
is_setup = True
2023-01-05 00:01:36 +01:00
return is_setup
2020-05-17 14:14:55 +02:00
2023-01-05 00:01:36 +01:00
@staticmethod
def mastodon_setup(self):
2023-01-05 00:01:36 +01:00
if not os.path.exists('secrets'):
os.makedirs('secrets')
2023-01-05 00:01:36 +01:00
self.mastodon_user = input("Mastodon user login? ")
self.mastodon_password = input("Mastodon user password? ")
self.app_name = 'fediverse'
2023-01-05 00:01:36 +01:00
self.mastodon_app_token = self.mastodon_log_in()
2020-05-17 14:14:55 +02:00
2023-01-05 00:01:36 +01:00
if not os.path.exists(self.secrets_filepath):
with open(self.secrets_filepath, 'w'): pass
print(f"{self.secrets_filepath} created!")
2023-01-05 00:01:36 +01:00
with open(self.secrets_filepath, 'a') as the_file:
print("Writing Mastodon parameters to " + self.secrets_filepath)
the_file.write(f'mastodon_app_token: {self.mastodon_app_token}')
2023-01-05 00:01:36 +01:00
return self.mastodon_app_token
2020-05-17 14:14:55 +02:00
2023-01-05 00:01:36 +01:00
def mastodon_log_in(self):
2023-01-05 00:01:36 +01:00
token = ''
2023-01-05 00:01:36 +01:00
try:
2023-01-05 00:01:36 +01:00
response = Mastodon.create_app(
self.app_name,
scopes=["read","write"],
to_file=None,
api_base_url=self.mastodon_hostname
)
client_id = response[0]
client_secret = response[1]
mastodon = Mastodon(client_id = client_id, client_secret = client_secret, api_base_url = self.mastodon_hostname)
token = mastodon.log_in(
self.mastodon_user,
self.mastodon_password,
scopes = ["read", "write"],
to_file = None
)
2023-01-05 00:01:36 +01:00
print('Log in succesful!')
except MastodonIllegalArgumentError as i_error:
sys.stdout.write(f'\n{str(i_error)}\n')
except MastodonNetworkError as n_error:
sys.stdout.write(f'\n{str(n_error)}\n')
except MastodonReadTimeout as r_error:
sys.stdout.write(f'\n{str(r_error)}\n')
except MastodonAPIError as a_error:
sys.stdout.write(f'\n{str(a_error)}\n')
finally:
return token
def __get_parameter(self, parameter, config_file):
if not os.path.isfile(config_file):
print(f"File {config_file} not found..")
self.mastodon_hostname = input("\nMastodon hostname: ")
self.__create_config(self)
self.__write_config(self)
with open( self.config_file ) as f:
for line in f:
if line.startswith( parameter ):
return line.replace(parameter + ":", "").strip()
def __get_mastodon_parameter(self, parameter, secrets_filepath):
if not os.path.isfile(secrets_filepath):
print(f"File {secrets_filepath} not found..")
self.sign_in()
with open( self.secrets_filepath ) as f:
for line in f:
if line.startswith( parameter ):
return line.replace(parameter + ":", "").strip()
@staticmethod
def __create_config(self):
if not os.path.exists('config'):
os.makedirs('config')
if not os.path.exists(self.config_file):
print(self.config_file + " created!")
with open(self.config_file, 'w'): pass
@staticmethod
def __write_config(self):
with open(self.config_file, 'a') as the_file:
the_file.write(f'mastodon_hostname: {self.mastodon_hostname}')
print(f"adding parameters to {self.config_file}\n")