Refactored to use Bot Class

master
spla 12 months ago
parent 637a93337c
commit 0420bebbee

@ -7,159 +7,162 @@ from mastodon.Mastodon import MastodonMalformedEventError, MastodonNetworkError,
import uuid
import pdb
def check_account(username):
def get_config():
is_duplicate = False
# Load configuration from config file
config_filepath = "config/db_config.txt"
replicator_db = get_db_params("replicator_db", config_filepath)
replicator_db_user = get_db_params("replicator_db_user", config_filepath)
sql = 'select username from maccounts where username=(%s)'
return (config_filepath, replicator_db, replicator_db_user)
conn = None
def check_db_conn():
try:
conn = None
conn = psycopg2.connect(database = replicator_db, user = replicator_db_user, password = "", host = "/var/run/postgresql", port = "5432")
cur = conn.cursor()
except (Exception, psycopg2.DatabaseError) as error:
cur.execute(sql, (username,))
sys.stdout.write(f'\n{str(error)}\n')
row = cur.fetchone()
sys.exit("Exiting. Run 'python db-setup' again")
if row != None:
class Bot:
is_duplicate = True
name = "Bot"
except (Exception, psycopg2.DatabaseError) as error:
def __init__(self, hostname, software, username, password, app_name, twitter_username):
check_error = error.pgcode
self.hostname = hostname
self.software = software
self.username = username
self.password = password
self.app_name = app_name
self.twitter_username = twitter_username
sys.stdout.write(f'\n{str(error)}\n')
def log_in(self):
finally:
token = ''
if conn is not None:
try:
conn.close()
response = Mastodon.create_app(
self.app_name,
scopes=["read","write"],
to_file=None,
api_base_url=self.hostname
)
client_id = response[0]
client_secret = response[1]
return is_duplicate
mastodon = Mastodon(client_id = client_id, client_secret = client_secret, api_base_url = self.hostname)
token = mastodon.log_in(
self.username,
self.password,
scopes = ["read", "write"],
to_file = None
)
def save_bot_account(hostname, username, client_id, client_secret, token, twitter_username):
print('Log in succesful!')
bot_uuid = str(uuid.uuid4())
except MastodonIllegalArgumentError as i_error:
save_error = None
sys.stdout.write(f'\n{str(i_error)}\n')
mastodon_sql = f'INSERT INTO maccounts (bot_id, hostname, username, client_id, client_secret, client_token, hostname_soft) VALUES (%s,%s,%s,%s,%s,%s,%s)'
except MastodonNetworkError as n_error:
twitter_sql = f'INSERT INTO taccounts (bot_id, username) VALUES (%s,%s)'
sys.stdout.write(f'\n{str(n_error)}\n')
conn = None
except MastodonReadTimeout as r_error:
try:
sys.stdout.write(f'\n{str(r_error)}\n')
conn = psycopg2.connect(database = replicator_db, user = replicator_db_user, password = "", host = "/var/run/postgresql", port = "5432")
except MastodonAPIError as a_error:
cur = conn.cursor()
sys.stdout.write(f'\n{str(a_error)}\n')
cur.execute(mastodon_sql, (bot_uuid, hostname, username, client_id, client_secret, token, hostname_soft))
finally:
cur.execute(twitter_sql, (bot_uuid, twitter_username))
return (client_id, client_secret, token)
conn.commit()
def check_account(self):
except (Exception, psycopg2.DatabaseError) as error:
is_duplicate = False
save_error = error.pgcode
sql = 'select username from maccounts where username=(%s)'
sys.stdout.write(f'\n{str(error)}\n')
conn = None
finally:
try:
if conn is not None:
conn = psycopg2.connect(database = replicator_db, user = replicator_db_user, password = "", host = "/var/run/postgresql", port = "5432")
conn.close()
cur = conn.cursor()
return save_error
cur.execute(sql, (self.username,))
def get_config():
row = cur.fetchone()
# Load configuration from config file
config_filepath = "config/db_config.txt"
replicator_db = get_db_params("replicator_db", config_filepath)
replicator_db_user = get_db_params("replicator_db_user", config_filepath)
if row != None:
return (config_filepath, replicator_db, replicator_db_user)
is_duplicate = True
def check_db_conn():
except (Exception, psycopg2.DatabaseError) as error:
try:
check_error = error.pgcode
conn = None
sys.stdout.write(f'\n{str(error)}\n')
conn = psycopg2.connect(database = replicator_db, user = replicator_db_user, password = "", host = "/var/run/postgresql", port = "5432")
finally:
except (Exception, psycopg2.DatabaseError) as error:
if conn is not None:
sys.stdout.write(f'\n{str(error)}\n')
conn.close()
sys.exit("Exiting. Run 'python db-setup' again")
return is_duplicate
def ask_account():
def save_account(self, client_id, client_secret, token):
print(f'Setting up Mastodon/Pleroma bot account...\n')
hostname = input("Enter Mastodon/Pleroma hostname: ")
hostname_soft = input("Server software (mastodon or pleroma)? ")
user_name = input(f'User name, ex. user@{hostname}? ')
user_password = getpass.getpass("User password? ")
app_name = 'replicator'
twitter_username = input(f'Twitter username (ex. jack)? ')
return (hostname, hostname_soft, user_name, user_password, app_name, twitter_username)
bot_uuid = str(uuid.uuid4())
def log_in(hostname, user_name, user_password, app_name):
save_error = None
token = ''
mastodon_sql = f'INSERT INTO maccounts (bot_id, hostname, username, client_id, client_secret, client_token, hostname_soft) VALUES (%s,%s,%s,%s,%s,%s,%s)'
try:
twitter_sql = f'INSERT INTO taccounts (bot_id, username) VALUES (%s,%s)'
response = Mastodon.create_app(
app_name,
scopes=["read","write"],
to_file=None,
api_base_url=hostname
)
client_id = response[0]
client_secret = response[1]
conn = None
mastodon = Mastodon(client_id = client_id, client_secret = client_secret, api_base_url = hostname)
token = mastodon.log_in(
user_name,
user_password,
scopes = ["read", "write"],
to_file = None
)
try:
except MastodonIllegalArgumentError as i_error:
conn = psycopg2.connect(database = replicator_db, user = replicator_db_user, password = "", host = "/var/run/postgresql", port = "5432")
sys.stdout.write(f'\n{str(i_error)}\n')
cur = conn.cursor()
except MastodonNetworkError as n_error:
cur.execute(mastodon_sql, (bot_uuid, self.hostname, self.username, client_id, client_secret, token, self.software))
sys.stdout.write(f'\n{str(n_error)}\n')
cur.execute(twitter_sql, (bot_uuid, self.twitter_username))
except MastodonReadTimeout as r_error:
conn.commit()
sys.stdout.write(f'\n{str(r_error)}\n')
except (Exception, psycopg2.DatabaseError) as error:
except MastodonAPIError as a_error:
save_error = error.pgcode
sys.stdout.write(f'\n{str(a_error)}\n')
sys.stdout.write(f'\n{str(error)}\n')
finally:
finally:
if conn is not None:
conn.close()
return save_error
return (client_id, client_secret, token)
def get_db_params( parameter, file_path ):
if not os.path.isfile(file_path):
@ -186,9 +189,16 @@ if __name__ == '__main__':
check_db_conn()
hostname, hostname_soft, user_name, user_password, app_name, twitter_username = ask_account()
bot_hostname = input("Enter Mastodon/Pleroma hostname: ")
bot_software = input("Server software (mastodon or pleroma)? ")
bot_username = input(f'User name, ex. user@{bot_hostname}? ')
bot_password = getpass.getpass("User password? ")
bot_app_name = 'replicator'
bot_twitter_username = input(f'Twitter username (ex. jack)? ')
myBot = Bot(bot_hostname, bot_software, bot_username, bot_password, bot_app_name, bot_twitter_username)
is_duplicate = check_account(user_name)
is_duplicate = myBot.check_account()
if is_duplicate:
@ -198,11 +208,11 @@ if __name__ == '__main__':
else:
client_id, client_secret, token = log_in(hostname, user_name, user_password, app_name)
client_id, client_secret, token = myBot.log_in()
if len(token) > 0:
save_error = save_bot_account(hostname, hostname_soft, user_name, client_id, client_secret, token, twitter_username)
save_error = myBot.save_account(client_id, client_secret, token)
if save_error == None:

Loading…
Cancel
Save