mastofeeds/db-setup.py

151 líneas
4,1 KiB
Python
Original Vista normal Històric

2020-03-21 18:49:45 +01:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
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
# Returns the parameter from the specified file
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 ):
2020-03-21 20:11:45 +01:00
print("Setting up newsfeed parameters...")
2020-03-21 18:49:45 +01:00
print("\n")
feeds_db = input("feeds db name: ")
feeds_db_user = input("feeds db user: ")
feeds_url = input("enter feeds url: ")
with open(file_path, "w") as text_file:
print("feeds_db: {}".format(feeds_db), file=text_file)
print("feeds_db_user: {}".format(feeds_db_user), file=text_file)
print("feeds_url: {}".format(feeds_url), file=text_file)
def create_table(db, db_user, table, sql):
conn = None
try:
conn = psycopg2.connect(database = db, user = db_user, password = "", host = "/var/run/postgresql", port = "5432")
cur = conn.cursor()
print("Creating table.. "+table)
# Create the table in PostgreSQL database
cur.execute(sql)
conn.commit()
print("Table "+table+" created!")
print("\n")
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
2020-10-20 12:54:34 +02:00
###############################################################################
# main
2020-03-21 18:49:45 +01:00
2020-10-20 12:54:34 +02:00
if __name__ == '__main__':
2020-03-21 18:49:45 +01:00
2020-10-20 12:54:34 +02:00
# Load configuration from config file
config_filepath = "db_config.txt"
feeds_db = get_parameter("feeds_db", config_filepath)
feeds_db_user = get_parameter("feeds_db_user", config_filepath)
feeds_url = get_parameter("feeds_url", config_filepath)
2020-03-21 18:49:45 +01:00
2020-10-20 12:54:34 +02:00
############################################################
# create database
############################################################
2020-03-21 18:49:45 +01:00
2020-10-20 12:54:34 +02:00
conn = None
2020-03-21 18:49:45 +01:00
2020-10-20 12:54:34 +02:00
try:
2020-03-21 18:49:45 +01:00
2020-10-20 12:54:34 +02:00
conn = psycopg2.connect(dbname='postgres', user=feeds_db_user, host='', password='')
2020-03-21 18:49:45 +01:00
2020-10-20 12:54:34 +02:00
conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
2020-03-21 18:49:45 +01:00
2020-10-20 12:54:34 +02:00
cur = conn.cursor()
2020-03-21 18:49:45 +01:00
2020-10-20 12:54:34 +02:00
print("Creating database " + feeds_db + ". Please wait...")
2020-03-21 18:49:45 +01:00
2020-10-20 12:54:34 +02:00
cur.execute(sql.SQL("CREATE DATABASE {}").format(sql.Identifier(feeds_db)))
2020-03-21 18:49:45 +01:00
2020-10-20 12:54:34 +02:00
print("Database " + feeds_db + " created!")
2020-03-21 18:49:45 +01:00
2020-10-20 12:54:34 +02:00
except (Exception, psycopg2.DatabaseError) as error:
2020-03-21 18:49:45 +01:00
2020-10-20 12:54:34 +02:00
print(error)
2020-03-21 18:49:45 +01:00
2020-10-20 12:54:34 +02:00
finally:
2020-03-21 18:49:45 +01:00
2020-10-20 12:54:34 +02:00
if conn is not None:
2020-03-21 18:49:45 +01:00
2020-10-20 12:54:34 +02:00
conn.close()
2020-03-21 18:49:45 +01:00
2020-10-20 12:54:34 +02:00
#############################################################################################
2020-03-21 18:49:45 +01:00
2020-10-20 12:54:34 +02:00
try:
2020-03-21 18:49:45 +01:00
2020-10-20 12:54:34 +02:00
conn = None
conn = psycopg2.connect(database = feeds_db, user = feeds_db_user, password = "", host = "/var/run/postgresql", port = "5432")
2020-03-21 18:49:45 +01:00
2020-10-20 12:54:34 +02:00
except (Exception, psycopg2.DatabaseError) as error:
2020-03-21 18:49:45 +01:00
2020-10-20 12:54:34 +02:00
print(error)
# Load configuration from config file
os.remove("db_config.txt")
print("Exiting. Run setup again with right parameters")
sys.exit(0)
2020-03-21 18:49:45 +01:00
2020-10-20 12:54:34 +02:00
if conn is not None:
print("\n")
print("Host parameters saved to config.txt!")
print("\n")
2020-03-21 18:49:45 +01:00
2020-10-20 12:54:34 +02:00
############################################################
# Create needed tables
############################################################
2020-03-21 18:49:45 +01:00
2020-10-20 12:54:34 +02:00
print("Creating table...")
2020-03-21 18:49:45 +01:00
2020-10-20 12:54:34 +02:00
########################################
2020-03-21 18:49:45 +01:00
2020-10-20 12:54:34 +02:00
db = feeds_db
db_user = feeds_db_user
table = "feeds"
sql = "create table "+table+" (id varchar(200) PRIMARY KEY, link varchar(250))"
create_table(db, db_user, table, sql)
2020-03-21 18:49:45 +01:00
2020-10-20 12:54:34 +02:00
#####################################
print("Done!")
print("Now you can run setup.py!")
print("\n")