From fdf4432bdd711fb8766b9f7ac84a97b63ef17430 Mon Sep 17 00:00:00 2001 From: spla Date: Mon, 28 Mar 2022 15:28:04 +0200 Subject: [PATCH] Added Activation/Deactivation of any account --- bots-setup.py | 122 ++++++++++++++++++++++++++++++++++++++++++++--- db-setup.py | 2 +- replicator.py | 3 +- requirements.txt | 2 +- 4 files changed, 120 insertions(+), 9 deletions(-) diff --git a/bots-setup.py b/bots-setup.py index 6dd660a..5f124fa 100644 --- a/bots-setup.py +++ b/bots-setup.py @@ -13,8 +13,10 @@ import pdb menu_options = { 1: 'New Bot', 2: 'Delete Bot', - 3: 'List Bots', - 4: 'Exit', + 3: 'Activate Bot', + 4: 'Deactivate Bot', + 5: 'List Bots', + 6: 'Exit', } def print_menu(): @@ -264,6 +266,94 @@ class GetBot: return (del_error) + def activate_account(self): + + twitter_sql = 'SELECT bot_id from taccounts where username=(%s)' + + activate_sql = 'update maccounts set active = True WHERE bot_id=(%s)' + + conn = None + + try: + + conn = psycopg2.connect(database = replicator_db, user = replicator_db_user, password = "", host = "/var/run/postgresql", port = "5432") + + cur = conn.cursor() + + cur.execute(twitter_sql, (self.twitter_username,)) + + row = cur.fetchone() + + if row != None: + + cur.execute(activate_sql, (row[0],)) + + conn.commit() + + print(f'{self.twitter_username} had been activated') + + time.sleep(2) + + else: + + print(f'{self.twitter_username} is not in the database') + + except (Exception, psycopg2.DatabaseError) as error: + + activate_error = error.pgcode + + sys.stdout.write(f'\n{str(activate_error)}\n') + + finally: + + if conn is not None: + + conn.close() + + def deactivate_account(self): + + twitter_sql = 'SELECT bot_id from taccounts where username=(%s)' + + deactivate_sql = 'update maccounts set active = False WHERE bot_id=(%s)' + + conn = None + + try: + + conn = psycopg2.connect(database = replicator_db, user = replicator_db_user, password = "", host = "/var/run/postgresql", port = "5432") + + cur = conn.cursor() + + cur.execute(twitter_sql, (self.twitter_username,)) + + row = cur.fetchone() + + if row != None: + + cur.execute(deactivate_sql, (row[0],)) + + conn.commit() + + print(f'{self.twitter_username} had been deactivated') + + time.sleep(2) + + else: + + print(f'{self.twitter_username} is not in the database') + + except (Exception, psycopg2.DatabaseError) as error: + + activate_error = error.pgcode + + sys.stdout.write(f'\n{str(activate_error)}\n') + + finally: + + if conn is not None: + + conn.close() + def print(self): print(f'\nBot found: {self.twitter_username}\n') @@ -296,7 +386,7 @@ def list_bots(): t_usernames_lst = [] - mastodon_sql = 'SELECT bot_id, hostname, username, hostname_soft FROM maccounts' + mastodon_sql = 'SELECT bot_id, hostname, username, hostname_soft, active FROM maccounts' twitter_sql = 'SELECT username from taccounts where bot_id=(%s)' @@ -430,17 +520,37 @@ if __name__ == '__main__': check_db_conn() + getbot = GetBot() + + getbot.activate_account() + + elif option == 4: + + config_filepath, replicator_db, replicator_db_user = get_config() + + check_db_conn() + + getbot = GetBot() + + getbot.deactivate_account() + + elif option == 5: + + config_filepath, replicator_db, replicator_db_user = get_config() + + check_db_conn() + bots_list, t_usernames_lst = list_bots() print_table = PrettyTable() - print_table.field_names = ['No.','Twitter username', 'bot account', 'host', 'host software'] + print_table.field_names = ['No.','Twitter username', 'bot account', 'host', 'host software', 'active'] i = 0 for bot in bots_list: - print_table.add_row([str(i+1), t_usernames_lst[i], bot[2], bot[1], bot[3]]) + print_table.add_row([str(i+1), t_usernames_lst[i], bot[2], bot[1], bot[3], bot[4]]) i += 1 @@ -448,7 +558,7 @@ if __name__ == '__main__': print(f'Total: {len(bots_list)}') - elif option == 4: + elif option == 6: print('Bye!') exit() diff --git a/db-setup.py b/db-setup.py index f39f38b..91afd6a 100644 --- a/db-setup.py +++ b/db-setup.py @@ -201,7 +201,7 @@ if __name__ == '__main__': create_table(db, db_user, table, sql) table = "maccounts" - sql = f'create table {table} (bot_id uuid, hostname varchar(20), username varchar(45), client_id varchar(45), client_secret varchar(45), client_token varchar(45), hostname_soft varchar(10), PRIMARY KEY (bot_id))' + sql = f'create table {table} (bot_id uuid, hostname varchar(20), username varchar(45), client_id varchar(45), client_secret varchar(45), client_token varchar(45), hostname_soft varchar(10), active boolean default True, PRIMARY KEY (bot_id))' create_table(db, db_user, table, sql) diff --git a/replicator.py b/replicator.py index a22f189..bcf103f 100644 --- a/replicator.py +++ b/replicator.py @@ -33,7 +33,7 @@ def load_bots(): hostname_soft_lst = [] - sql = 'select bot_id, hostname, username, client_id, client_secret, client_token, hostname_soft from maccounts' + sql = 'select bot_id, hostname, username, client_id, client_secret, client_token, hostname_soft from maccounts where active' try: @@ -331,6 +331,7 @@ def twitter_config(): conn.close() def get_parameter( parameter, file_path ): + # Check if secrets file exists if not os.path.isfile(file_path): print("File %s not found, exiting."%file_path) diff --git a/requirements.txt b/requirements.txt index 81d883a..87a6dc6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ psycopg2>=2.9.3 Mastodon.py>=1.5.1 -tweepy>=4.5.0 +tweepy>=4.7.0 PrettyTable>=3.1.1