2020-05-17 14:14:55 +02:00
|
|
|
import getpass
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
from mastodon import Mastodon
|
|
|
|
from mastodon.Mastodon import MastodonMalformedEventError, MastodonNetworkError, MastodonReadTimeout, MastodonAPIError
|
|
|
|
import psycopg2
|
|
|
|
from psycopg2 import sql
|
|
|
|
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
|
|
|
|
|
|
|
|
def get_parameter( parameter, file_path ):
|
|
|
|
# Check if secrets file exists
|
|
|
|
if not os.path.isfile(file_path):
|
|
|
|
print("File %s not found, asking."%file_path)
|
|
|
|
write_parameter( parameter, 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)
|
|
|
|
|
|
|
|
def write_parameter( parameter, file_path ):
|
2021-10-26 13:38:12 +02:00
|
|
|
|
|
|
|
if not os.path.exists('config'):
|
|
|
|
os.makedirs('config')
|
|
|
|
print("Setting up fediverse parameters...")
|
|
|
|
print("\n")
|
|
|
|
fediverse_db = input("fediverse db name: ")
|
|
|
|
fediverse_db_user = input("fediverse db user: ")
|
|
|
|
mastodon_db = input("Mastodon db name: ")
|
|
|
|
mastodon_db_user = input("Mastodon db user: ")
|
|
|
|
|
|
|
|
with open(file_path, "w") as text_file:
|
|
|
|
|
|
|
|
print("fediverse_db: {}".format(fediverse_db), file=text_file)
|
|
|
|
print("fediverse_db_user: {}".format(fediverse_db_user), file=text_file)
|
|
|
|
print("mastodon_db: {}".format(mastodon_db), file=text_file)
|
|
|
|
print("mastodon_db_user: {}".format(mastodon_db_user), file=text_file)
|
2020-05-17 14:14:55 +02:00
|
|
|
|
|
|
|
def create_table(db, db_user, table, sql):
|
|
|
|
|
2021-10-26 13:38:12 +02:00
|
|
|
conn = None
|
2020-05-17 14:14:55 +02:00
|
|
|
|
2021-10-26 13:38:12 +02:00
|
|
|
try:
|
2020-05-17 14:14:55 +02:00
|
|
|
|
2021-10-26 13:38:12 +02:00
|
|
|
conn = psycopg2.connect(database = db, user = db_user, password = "", host = "/var/run/postgresql", port = "5432")
|
|
|
|
cur = conn.cursor()
|
2020-05-17 14:14:55 +02:00
|
|
|
|
2021-10-26 13:38:12 +02:00
|
|
|
print("Creating table.. "+table)
|
|
|
|
# Create the table in PostgreSQL database
|
|
|
|
cur.execute(sql)
|
2020-05-17 14:14:55 +02:00
|
|
|
|
2021-10-26 13:38:12 +02:00
|
|
|
conn.commit()
|
2020-05-17 14:14:55 +02:00
|
|
|
|
2021-10-26 13:38:12 +02:00
|
|
|
print("Table "+table+" created!")
|
|
|
|
print("\n")
|
2020-05-17 14:14:55 +02:00
|
|
|
|
2021-10-26 13:38:12 +02:00
|
|
|
except (Exception, psycopg2.DatabaseError) as error:
|
2020-05-17 14:14:55 +02:00
|
|
|
|
2021-10-26 13:38:12 +02:00
|
|
|
print(error)
|
|
|
|
|
|
|
|
finally:
|
2020-05-17 14:14:55 +02:00
|
|
|
|
2021-10-26 13:38:12 +02:00
|
|
|
if conn is not None:
|
2020-05-17 14:14:55 +02:00
|
|
|
|
2021-10-26 13:38:12 +02:00
|
|
|
conn.close()
|
2020-05-17 14:14:55 +02:00
|
|
|
|
2021-10-26 13:38:12 +02:00
|
|
|
###############################################################################
|
|
|
|
# main
|
2020-05-17 14:14:55 +02:00
|
|
|
|
2021-10-26 13:38:12 +02:00
|
|
|
if __name__ == '__main__':
|
2020-05-17 14:14:55 +02:00
|
|
|
|
2021-10-26 13:38:12 +02:00
|
|
|
# Load configuration from config file
|
|
|
|
config_filepath = "config/db_config.txt"
|
|
|
|
fediverse_db = get_parameter("fediverse_db", config_filepath)
|
|
|
|
fediverse_db_user = get_parameter("fediverse_db_user", config_filepath)
|
|
|
|
mastodon_db = get_parameter("mastodon_db", config_filepath)
|
|
|
|
mastodon_db_user = get_parameter("mastodon_db_user", config_filepath)
|
2020-05-17 14:14:55 +02:00
|
|
|
|
2021-10-26 13:38:12 +02:00
|
|
|
############################################################
|
|
|
|
# create database
|
|
|
|
############################################################
|
2020-05-17 14:14:55 +02:00
|
|
|
|
2021-10-26 13:38:12 +02:00
|
|
|
conn = None
|
2020-05-17 14:14:55 +02:00
|
|
|
|
2021-10-26 13:38:12 +02:00
|
|
|
try:
|
2020-05-17 14:14:55 +02:00
|
|
|
|
2021-10-26 13:38:12 +02:00
|
|
|
conn = psycopg2.connect(dbname='postgres',
|
|
|
|
user=fediverse_db_user, host='',
|
|
|
|
password='')
|
2020-05-17 14:14:55 +02:00
|
|
|
|
2021-10-26 13:38:12 +02:00
|
|
|
conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
|
2020-05-17 14:14:55 +02:00
|
|
|
|
2021-10-26 13:38:12 +02:00
|
|
|
cur = conn.cursor()
|
2020-05-17 14:14:55 +02:00
|
|
|
|
2021-10-26 13:38:12 +02:00
|
|
|
print("Creating database " + fediverse_db + ". Please wait...")
|
2020-05-17 14:14:55 +02:00
|
|
|
|
2021-10-26 13:38:12 +02:00
|
|
|
cur.execute(sql.SQL("CREATE DATABASE {}").format(
|
|
|
|
sql.Identifier(fediverse_db))
|
|
|
|
)
|
|
|
|
print("Database " + fediverse_db + " created!")
|
2020-05-17 14:14:55 +02:00
|
|
|
|
2021-10-26 13:38:12 +02:00
|
|
|
except (Exception, psycopg2.DatabaseError) as error:
|
2020-05-17 14:14:55 +02:00
|
|
|
|
2021-10-26 13:38:12 +02:00
|
|
|
print(error)
|
2020-05-17 14:14:55 +02:00
|
|
|
|
2021-10-26 13:38:12 +02:00
|
|
|
finally:
|
2020-05-17 14:14:55 +02:00
|
|
|
|
2021-10-26 13:38:12 +02:00
|
|
|
if conn is not None:
|
2020-05-17 14:14:55 +02:00
|
|
|
|
2021-10-26 13:38:12 +02:00
|
|
|
conn.close()
|
2020-05-17 14:14:55 +02:00
|
|
|
|
2021-10-26 13:38:12 +02:00
|
|
|
#############################################################################################
|
2020-05-17 14:14:55 +02:00
|
|
|
|
2021-10-26 13:38:12 +02:00
|
|
|
try:
|
2020-05-17 14:14:55 +02:00
|
|
|
|
2021-10-26 13:38:12 +02:00
|
|
|
conn = None
|
|
|
|
conn = psycopg2.connect(database = fediverse_db, user = fediverse_db_user, password = "", host = "/var/run/postgresql", port = "5432")
|
2020-05-17 14:14:55 +02:00
|
|
|
|
2021-10-26 13:38:12 +02:00
|
|
|
except (Exception, psycopg2.DatabaseError) as error:
|
2020-05-17 14:14:55 +02:00
|
|
|
|
2021-10-26 13:38:12 +02:00
|
|
|
print(error)
|
|
|
|
# Load configuration from config file
|
|
|
|
os.remove("db_config.txt")
|
|
|
|
print("Exiting. Run db-setup again with right parameters")
|
|
|
|
sys.exit(0)
|
2020-05-17 14:14:55 +02:00
|
|
|
|
2021-10-26 13:38:12 +02:00
|
|
|
if conn is not None:
|
|
|
|
|
|
|
|
print("\n")
|
|
|
|
print("fediverse parameters saved to db-config.txt!")
|
|
|
|
print("\n")
|
|
|
|
|
|
|
|
############################################################
|
|
|
|
# Create needed tables
|
|
|
|
############################################################
|
2020-05-17 14:14:55 +02:00
|
|
|
|
2021-10-26 13:38:12 +02:00
|
|
|
print("Creating table...")
|
2020-05-17 14:14:55 +02:00
|
|
|
|
2021-10-26 13:38:12 +02:00
|
|
|
########################################
|
2020-05-17 14:14:55 +02:00
|
|
|
|
2021-10-26 13:38:12 +02:00
|
|
|
db = fediverse_db
|
|
|
|
db_user = fediverse_db_user
|
|
|
|
table = "world"
|
|
|
|
sql = "create table "+table+" (server varchar(200) PRIMARY KEY, federated_with varchar(200), updated_at timestamptz, saved_at timestamptz, checked boolean)"
|
|
|
|
create_table(db, db_user, table, sql)
|
2020-05-17 14:14:55 +02:00
|
|
|
|
2021-10-26 13:38:12 +02:00
|
|
|
table = "fediverse"
|
|
|
|
sql = "create table "+table+" (server varchar(200) PRIMARY KEY, users INT, updated_at timestamptz, software varchar(50), version varchar(100), first_checked_at timestamptz, last_checked_at timestamptz, downs int)"
|
|
|
|
create_table(db, db_user, table, sql)
|
2020-05-17 14:14:55 +02:00
|
|
|
|
2021-10-26 13:38:12 +02:00
|
|
|
table = "totals"
|
|
|
|
sql = "create table "+table+" (datetime timestamptz PRIMARY KEY, total_servers INT, total_users INT)"
|
|
|
|
create_table(db, db_user, table, sql)
|
2020-05-17 14:14:55 +02:00
|
|
|
|
2021-10-26 13:38:12 +02:00
|
|
|
table = "evo"
|
|
|
|
sql = "create table "+table+" (datetime timestamptz PRIMARY KEY, servers INT, users INT)"
|
|
|
|
create_table(db, db_user, table, sql)
|
2020-05-17 14:14:55 +02:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
table = "execution_time"
|
|
|
|
sql = "create table "+table+" (program varchar(20) PRIMARY KEY, start timestamptz, finish timestamptz)"
|
2021-10-26 13:38:12 +02:00
|
|
|
create_table(db, db_user, table, sql)
|
2020-05-17 14:14:55 +02:00
|
|
|
|
2021-10-26 13:38:12 +02:00
|
|
|
#####################################
|
|
|
|
|
|
|
|
print("Done!")
|
|
|
|
print("Now you can run setup.py!")
|
|
|
|
print("\n")
|