126 lines
3.1 KiB
Python
126 lines
3.1 KiB
Python
import mysql.connector as mariadb
|
|
import db.sql
|
|
import dateparser
|
|
|
|
def connect_db(database, host, user, password):
|
|
try:
|
|
con = mariadb.connect(host=host, user=user, password=password, database=database)
|
|
except mariadb.Error as error:
|
|
logging.error("Error: {}".format(error))
|
|
if error.errno == 1049:
|
|
logging.error("Database " + database + " does not exist.")
|
|
return None
|
|
finally:
|
|
return con
|
|
|
|
def list_all_tables(db_con):
|
|
try:
|
|
cursor = db_con.cursor()
|
|
cursor.execute(db.sql.SHOW_TABLE)
|
|
results = []
|
|
for t in cursor:
|
|
results.append(t[0])
|
|
return results
|
|
except mariadb.Error as error:
|
|
logging.error("Error: {}".format(error))
|
|
return None
|
|
finally:
|
|
cursor.close()
|
|
|
|
def format_date(date_str):
|
|
|
|
date_time = dateparser.parse(date_str)
|
|
if date_time is not None:
|
|
return date_time
|
|
|
|
if '(' in date_str:
|
|
date_str = date_str.split('(')[0].rstrip()
|
|
|
|
|
|
date_time = dateparser.parse(date_str)
|
|
if date_time is not None:
|
|
return date_time
|
|
|
|
else:
|
|
# random stuff...
|
|
fix = False
|
|
toks = date_str.split()
|
|
|
|
if len(toks[-1]) == 5 or len(toks[-1]) == 4:
|
|
# ex. Thu, 24 Jan 2002 15:21:31 -0000
|
|
if toks[-1] in ['+0000', '-0000', '0000']:
|
|
date_str = date_str[:-5]
|
|
fix = True
|
|
# ex. Fri, 25 Jan 2002 13:21:49 +1050
|
|
elif toks[-1][-2] == '5':
|
|
d = list(date_str)
|
|
d[-2] = '3'
|
|
date_str = "".join(d)
|
|
fix = True
|
|
|
|
if toks[-1][-1] != '0':
|
|
#ex. 'Fri,', '20', 'Jun', '1997', '02:58:59', '-0005'
|
|
date_str = date_str[:-5]
|
|
fix = True
|
|
|
|
if 'Fru' in toks[0]:
|
|
date_str = date_str.replace('Fru', 'Fri')
|
|
fix = True
|
|
elif 'Thur' in toks[0]:
|
|
date_str = date_str.replace('Thur', 'Thu')
|
|
fix = True
|
|
|
|
date_time = dateparser.parse(date_str)
|
|
if date_time is not None:
|
|
return date_time
|
|
|
|
else:
|
|
|
|
if 'GMT' in date_str:
|
|
# ex. 'Mon,', '15', 'Jan', '96', '02:55', 'GMT+0100'
|
|
date_str = date_str.split('GMT')[0].rstrip()
|
|
fix = True
|
|
|
|
if 'METDST' in toks[-1]:
|
|
# ex. 'Sat,', '3', 'May', '97', '21:07', 'METDST'
|
|
date_str = date_str.replace('METDST', 'MET')
|
|
fix = True
|
|
|
|
date_time = dateparser.parse(date_str)
|
|
if date_time is not None:
|
|
return date_time
|
|
else:
|
|
return None
|
|
|
|
def format_author(author_str):
|
|
|
|
# author_str = author_str.replace('"', '')
|
|
|
|
if "by way of" in author_str:
|
|
toks = author_str.split("by way of")
|
|
if toks[0] == "":
|
|
author_str = format_from(msg)
|
|
elif toks[0][-1] == "(":
|
|
author_str = toks[0][:-1].strip()
|
|
else:
|
|
author_str = toks[0]
|
|
|
|
if ("(" in author_str) or ("<" in author_str):
|
|
# ex. zx {AT} xyz.net (Michel Foucault) OR Michel Foucault (c'estcommeca.com) OR Michel Foucault <zx {AT} xyz.net>
|
|
# print("±±±±±±")
|
|
# print("name: " + author_str)
|
|
# print("from: " + msg['from'])
|
|
if not '@' in author_str.lower().replace('{at}', '@').replace(' at ', '@'):
|
|
author_str = author_str.split('(')[0].strip()
|
|
else:
|
|
author_str = email.utils.parseaddr(author_str)[0]
|
|
# print(" Name:" + author_str.replace('"', ''))
|
|
# print(" From:" + format_from(msg))
|
|
|
|
if " ," in author_str:
|
|
# nettime's_roving_reporter , thing.net {AT} bbs.thing.net
|
|
author_str = author_str.split(' ,')[0]
|
|
|
|
|
|
return author_str
|