Added Activation/Deactivation of any account

This commit is contained in:
spla 2022-03-28 15:28:04 +02:00
parent 69bc2d0623
commit fdf4432bdd
4 changed files with 120 additions and 9 deletions

View file

@ -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()

View file

@ -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)

View file

@ -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)

View file

@ -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