replicator/bots-setup.py

226 líneas
5,8 KiB
Python
Original Vista normal Històric

2022-02-16 18:43:30 +01:00
import getpass
import os
import sys
import psycopg2
from mastodon import Mastodon
from mastodon.Mastodon import MastodonMalformedEventError, MastodonNetworkError, MastodonReadTimeout, MastodonAPIError, MastodonIllegalArgumentError
import uuid
import pdb
2022-02-20 23:23:28 +01:00
def get_config():
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
# 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)
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
return (config_filepath, replicator_db, replicator_db_user)
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
def check_db_conn():
2022-02-16 18:43:30 +01:00
try:
2022-02-20 23:23:28 +01:00
conn = None
2022-02-16 18:43:30 +01:00
conn = psycopg2.connect(database = replicator_db, user = replicator_db_user, password = "", host = "/var/run/postgresql", port = "5432")
2022-02-20 23:23:28 +01:00
except (Exception, psycopg2.DatabaseError) as error:
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
sys.stdout.write(f'\n{str(error)}\n')
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
sys.exit("Exiting. Run 'python db-setup' again")
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
class Bot:
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
name = "Bot"
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
def __init__(self, hostname, software, username, password, app_name, twitter_username):
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
self.hostname = hostname
self.software = software
self.username = username
self.password = password
self.app_name = app_name
self.twitter_username = twitter_username
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
def log_in(self):
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
token = ''
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
try:
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
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]
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
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
)
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
print('Log in succesful!')
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
except MastodonIllegalArgumentError as i_error:
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
sys.stdout.write(f'\n{str(i_error)}\n')
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
except MastodonNetworkError as n_error:
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
sys.stdout.write(f'\n{str(n_error)}\n')
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
except MastodonReadTimeout as r_error:
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
sys.stdout.write(f'\n{str(r_error)}\n')
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
except MastodonAPIError as a_error:
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
sys.stdout.write(f'\n{str(a_error)}\n')
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
finally:
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
return (client_id, client_secret, token)
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
def check_account(self):
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
is_duplicate = False
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
sql = 'select username from maccounts where username=(%s)'
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
conn = None
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
try:
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
conn = psycopg2.connect(database = replicator_db, user = replicator_db_user, password = "", host = "/var/run/postgresql", port = "5432")
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
cur = conn.cursor()
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
cur.execute(sql, (self.username,))
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
row = cur.fetchone()
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
if row != None:
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
is_duplicate = True
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
except (Exception, psycopg2.DatabaseError) as error:
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
check_error = error.pgcode
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
sys.stdout.write(f'\n{str(error)}\n')
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
finally:
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
if conn is not None:
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
conn.close()
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
return is_duplicate
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
def save_account(self, client_id, client_secret, token):
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
bot_uuid = str(uuid.uuid4())
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
save_error = None
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
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)'
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
twitter_sql = f'INSERT INTO taccounts (bot_id, username) VALUES (%s,%s)'
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
conn = None
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
try:
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
conn = psycopg2.connect(database = replicator_db, user = replicator_db_user, password = "", host = "/var/run/postgresql", port = "5432")
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
cur = conn.cursor()
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
cur.execute(mastodon_sql, (bot_uuid, self.hostname, self.username, client_id, client_secret, token, self.software))
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
cur.execute(twitter_sql, (bot_uuid, self.twitter_username))
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
conn.commit()
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
except (Exception, psycopg2.DatabaseError) as error:
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
save_error = error.pgcode
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
sys.stdout.write(f'\n{str(error)}\n')
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
finally:
if conn is not None:
conn.close()
return save_error
2022-02-16 18:43:30 +01:00
def get_db_params( parameter, file_path ):
if not os.path.isfile(file_path):
print("File %s not found, exiting."%file_path)
sys.exit(0)
# Find parameter in file
with open( file_path ) as f:
for line in f:
if line.startswith( parameter ):
return line.replace(parameter + ":", "").strip()
# Cannot find parameter, exit
print(file_path + " Missing parameter %s "%parameter)
sys.exit(0)
###############################################################################
# main
if __name__ == '__main__':
config_filepath, replicator_db, replicator_db_user = get_config()
check_db_conn()
2022-02-20 23:23:28 +01:00
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)
2022-02-16 18:43:30 +01:00
2022-02-20 23:23:28 +01:00
is_duplicate = myBot.check_account()
2022-02-16 18:43:30 +01:00
if is_duplicate:
2022-02-18 18:25:29 +01:00
print(f'Bot account already exist!')
2022-02-16 18:43:30 +01:00
sys.exit()
else:
2022-02-20 23:23:28 +01:00
client_id, client_secret, token = myBot.log_in()
2022-02-16 18:43:30 +01:00
if len(token) > 0:
2022-02-20 23:23:28 +01:00
save_error = myBot.save_account(client_id, client_secret, token)
2022-02-16 18:43:30 +01:00
if save_error == None:
print(f'Done.')
else:
if save_error == '423505':
sys.exit()