new lists

This commit is contained in:
gauthiier 2019-08-24 13:06:59 +02:00
parent 6138fcf640
commit 849e57f450
5 changed files with 65 additions and 27 deletions

2
.gitignore vendored
View File

@ -2,7 +2,7 @@
archives/ archives/
config/ config/
config.py config.py
test.py test*.py
#macos #macos
.DS_Store .DS_Store

View File

@ -53,38 +53,59 @@ def connect_db(database, host, user, password):
except mariadb.Error as error: except mariadb.Error as error:
print("Error: {}".format(error)) print("Error: {}".format(error))
if error.errno == 1049: if error.errno == 1049:
if util.y_n_question("Table " + archive_name + " does not exist. Create it?"): print("Database " + database + " does not exist.")
print("creating")
else:
print("not creating")
return None return None
finally: finally:
return con return con
def list_tables_db(database, host, user, password):
con = connect_db(database, host, user, password)
if con is not None:
try:
cursor = con.cursor()
cursor.execute(archive.sql.SHOW_TABLE)
results = []
for t in cursor:
results.append(t[0])
return results
except mariadb.Error as error:
print("Error: {}".format(error))
finally:
cursor.close()
con.close()
class Archive: class Archive:
data = None # "raw" json data data = None # "raw" json data
db_con = None db_con = None
def __init__(self, archive_name, archive_dir): def __init__(self, archive_name, archive_dir_or_db_config):
if isinstance(archive_name, str): if isinstance(archive_dir_or_db_config, str):
# need a filename or a dir name.... # need a filename or a dir name....
print("reading archive " + archive_name, end='') print("reading archive " + archive_name, end='')
archive_dir = archive_dir_or_db_config
(self.data, self.archive_name) = load_from_file(archive_name, archive_name, archive_dir) (self.data, self.archive_name) = load_from_file(archive_name, archive_name, archive_dir)
print(" - done.") print(" - done.")
elif isinstance(archive_dir_or_db_config, dict):
def __init__(self, archive_name, database, host, user, password):
self.archive_name = archive_name
self.db_con = connect_db(database, host, user, password)
def __init__(self, archive_name, config):
self.archive_name = archive_name self.archive_name = archive_name
self.db_con = connect_db(config['database'], config['host'], config['user'], config['password']) self.db_con = connect_db(config['database'], config['host'], config['user'], config['password'])
# def __init__(self, archive_name, database, host, user, password):
# self.archive_name = archive_name
# self.db_con = connect_db(database, host, user, password)
def __enter__(self): def __enter__(self):
return self return self
@ -92,12 +113,15 @@ class Archive:
if self.db_con is not None: if self.db_con is not None:
self.db_con.close() self.db_con.close()
def create_db(self, config=None):
def create_db(self, host, database, user, password):
print("creating table: " + self.archive_name, end='') print("creating table: " + self.archive_name, end='')
self.db_con = connect_db(database, host, user, password)
if self.db_con is None: if self.db_con is None:
if config is not None:
self.db_con = connect_db(config['database'], config['host'], config['user'], config['password'])
if self.db_con is None:
print(" - no connection... Aborting.")
return return
try: try:
@ -110,9 +134,12 @@ class Archive:
print(" - done.") print(" - done.")
def insert_db(self, host, database, user, password):
self.db_con = connect_db(database, host, user, password) def insert_db(self, config=None):
if self.db_con is None:
if config is not None:
self.db_con = connect_db(config['database'], config['host'], config['user'], config['password'])
if self.db_con is None: if self.db_con is None:
return return
@ -136,8 +163,8 @@ class Archive:
self.db_con.commit() self.db_con.commit()
except mariadb.Error as error: except mariadb.Error as error:
print("Error: {}".format(error))
pass pass
# print("Error: {}".format(error))
finally: finally:
cursor.close() cursor.close()
@ -153,11 +180,14 @@ class Archive:
date_ = archive.util.format_date(m, self.archive_name) date_ = archive.util.format_date(m, self.archive_name)
if date_ is None or from_ is None: if date_ is None or from_ is None:
# print("\nerrorororororo") print("\nerrorororororo")
# print(m['from'] + " -- " + m['date']) print(m['from'] + " -- " + m['date'])
continue continue
cursor.execute(archive.sql.INSERT, (from_,author_name_,to_,m["subject"],date_,m["content-type"],m["content"],m["url"]))
str_insert = archive.sql.INSERT.format(self.archive_name)
cursor.execute(str_insert, (from_, author_name_,to_, m["subject"], date_, m["content-type"], m["content"], m["url"]))
# cursor.execute(archive.sql.INSERT.format(self.archive_name, from_, author_name_,to_, m["subject"], date_, m["content-type"], m["content"], m["url"]))
n_inserted += 1 n_inserted += 1
if "follow-up" in m: if "follow-up" in m:
@ -168,6 +198,10 @@ class Archive:
#duplication continue <------------------------- look this up... #duplication continue <------------------------- look this up...
# print("\nError: {}".format(error)) # print("\nError: {}".format(error))
continue continue
else:
print("\nError: {}".format(error))
print(str_insert)
continue
return n_inserted return n_inserted

View File

@ -12,7 +12,7 @@ CREATE = "CREATE TABLE `{}` (" \
"FULLTEXT (`from_`, `author_name_`)" \ "FULLTEXT (`from_`, `author_name_`)" \
") ENGINE = InnoDB;" ") ENGINE = InnoDB;"
INSERT = ("INSERT INTO nettime_l" INSERT = ("INSERT INTO {}"
"(from_, author_name_, to_, subject_, date_, content_type_, content_, url_) " "(from_, author_name_, to_, subject_, date_, content_type_, content_, url_) "
"VALUES (%s, %s, %s, %s, %s, %s, %s, %s)") "VALUES (%s, %s, %s, %s, %s, %s, %s, %s)")
@ -28,4 +28,6 @@ FROM_QUERY_BOOLEAN = ("SELECT from_, author_name_, subject_, date_, url_ FROM {}
FROM_QUERY_NL = ("SELECT from_, author_name_, subject_, date_, url_ FROM {} " FROM_QUERY_NL = ("SELECT from_, author_name_, subject_, date_, url_ FROM {} "
"WHERE MATCH(from_, author_name_) AGAINST('{}') ORDER BY date_") "WHERE MATCH(from_, author_name_) AGAINST('{}') ORDER BY date_")
SHOW_TABLE = "show tables"
# SELECT from_, author_name_, subject_, date_, url_ FROM nettime_l WHERE MATCH(content_) AGAINST('%s' IN BOOLEAN MODE) # SELECT from_, author_name_, subject_, date_, url_ FROM nettime_l WHERE MATCH(content_) AGAINST('%s' IN BOOLEAN MODE)

View File

@ -79,7 +79,7 @@ def format_from(msg):
def format_to(msg): def format_to(msg):
if "to" not in msg or msg["to"] is None: if "to" not in msg or msg["to"] is None:
return None return "n/a"
to_str = msg["to"] to_str = msg["to"]
toks = email.utils.parseaddr(to_str) toks = email.utils.parseaddr(to_str)

View File

@ -20,6 +20,8 @@ def favicon():
def searh(): def searh():
if len(request.args) < 1: if len(request.args) < 1:
# q: list all table or keep predefined wconfig.lists_to_serve?
wconfig.lists_to_serve = archive.show_tables_db(config.db['database'], config.db['host'], config.db['user'], config.db['password'])
return render_template("search.html", archives=wconfig.lists_to_serve, fields=['content', 'from']) return render_template("search.html", archives=wconfig.lists_to_serve, fields=['content', 'from'])
k_arg = request.args.get('keyword') k_arg = request.args.get('keyword')