Fixed indents

This commit is contained in:
spla 2020-10-20 12:54:34 +02:00
pare 70b8575fe5
commit c65a4c1fc8
S'han modificat 2 arxius amb 116 adicions i 112 eliminacions

Veure arxiu

@ -66,84 +66,85 @@ def create_table(db, db_user, table, sql):
conn.close() conn.close()
############################################################################################# ###############################################################################
# main
# Load configuration from config file if __name__ == '__main__':
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)
############################################################ # Load configuration from config file
# create database 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)
conn = None ############################################################
# create database
############################################################
try: conn = None
conn = psycopg2.connect(dbname='postgres', try:
user=feeds_db_user, host='',
password='')
conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT) conn = psycopg2.connect(dbname='postgres', user=feeds_db_user, host='', password='')
cur = conn.cursor() conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
print("Creating database " + feeds_db + ". Please wait...") cur = conn.cursor()
cur.execute(sql.SQL("CREATE DATABASE {}").format( print("Creating database " + feeds_db + ". Please wait...")
sql.Identifier(feeds_db))
)
print("Database " + feeds_db + " created!")
except (Exception, psycopg2.DatabaseError) as error: cur.execute(sql.SQL("CREATE DATABASE {}").format(sql.Identifier(feeds_db)))
print(error) print("Database " + feeds_db + " created!")
finally: except (Exception, psycopg2.DatabaseError) as error:
if conn is not None: print(error)
conn.close() finally:
############################################################################################# if conn is not None:
try: conn.close()
conn = None #############################################################################################
conn = psycopg2.connect(database = feeds_db, user = feeds_db_user, password = "", host = "/var/run/postgresql", port = "5432")
except (Exception, psycopg2.DatabaseError) as error: try:
print(error) conn = None
# Load configuration from config file conn = psycopg2.connect(database = feeds_db, user = feeds_db_user, password = "", host = "/var/run/postgresql", port = "5432")
os.remove("db_config.txt")
print("Exiting. Run setup again with right parameters")
sys.exit(0)
if conn is not None: except (Exception, psycopg2.DatabaseError) as error:
print("\n") print(error)
print("Host parameters saved to config.txt!")
print("\n")
############################################################ # Load configuration from config file
# Create needed tables os.remove("db_config.txt")
############################################################ print("Exiting. Run setup again with right parameters")
sys.exit(0)
print("Creating table...") if conn is not None:
######################################## print("\n")
print("Host parameters saved to config.txt!")
print("\n")
db = feeds_db ############################################################
db_user = feeds_db_user # Create needed tables
table = "feeds" ############################################################
sql = "create table "+table+" (id varchar(200) PRIMARY KEY, link varchar(250))"
create_table(db, db_user, table, sql)
##################################### print("Creating table...")
print("Done!") ########################################
print("Now you can run setup.py!")
print("\n") 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)
#####################################
print("Done!")
print("Now you can run setup.py!")
print("\n")

Veure arxiu

@ -52,76 +52,39 @@ mastodon = Mastodon(
# Initialise access headers # Initialise access headers
headers={ 'Authorization': 'Bearer %s'%uc_access_token } headers={ 'Authorization': 'Bearer %s'%uc_access_token }
######################################################## ###############################################################################
# main
publish = 0 if __name__ == '__main__':
newsfeeds = feedparser.parse(feeds_url) publish = 0
for entry in newsfeeds.entries: newsfeeds = feedparser.parse(feeds_url)
title = entry['title'] for entry in newsfeeds.entries:
id = entry['id']
link = entry['link']
################################################################### title = entry['title']
# check database if feed is already published id = entry['id']
################################################################### link = entry['link']
try: ###################################################################
# check database if feed is already published
conn = None ###################################################################
conn = psycopg2.connect(database = feeds_db, user = feeds_db_user, password = "", host = "/var/run/postgresql", port = "5432")
cur = conn.cursor()
cur.execute('select id from feeds where id=(%s)', (id,))
row = cur.fetchone()
if row == None:
publish = 1
else:
publish = 0
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
###########################################################
if publish == 1:
toot_text = str(title)+'\n'
toot_text += str(link)
print("Tooting...")
print(toot_text)
mastodon.status_post(toot_text, in_reply_to_id=None,)
#########################################################
insert_line = 'INSERT INTO feeds(id, link) VALUES (%s, %s)'
conn = None
try: try:
conn = None
conn = psycopg2.connect(database = feeds_db, user = feeds_db_user, password = "", host = "/var/run/postgresql", port = "5432") conn = psycopg2.connect(database = feeds_db, user = feeds_db_user, password = "", host = "/var/run/postgresql", port = "5432")
cur = conn.cursor() cur = conn.cursor()
cur.execute(insert_line, (id, link,)) cur.execute('select id from feeds where id=(%s)', (id,))
conn.commit() row = cur.fetchone()
if row == None:
publish = 1
else:
publish = 0
cur.close() cur.close()
@ -134,6 +97,46 @@ for entry in newsfeeds.entries:
if conn is not None: if conn is not None:
conn.close() conn.close()
else:
print("Any new feeds") ###########################################################
if publish == 1:
toot_text = str(title)+'\n'
toot_text += str(link)
print("Tooting...")
print(toot_text)
mastodon.status_post(toot_text, in_reply_to_id=None,)
#########################################################
insert_line = 'INSERT INTO feeds(id, link) VALUES (%s, %s)'
conn = None
try:
conn = psycopg2.connect(database = feeds_db, user = feeds_db_user, password = "", host = "/var/run/postgresql", port = "5432")
cur = conn.cursor()
cur.execute(insert_line, (id, link,))
conn.commit()
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
else:
print("Any new feeds")