From 64e7452fffe15248633b8b719b91a7fea9231c64 Mon Sep 17 00:00:00 2001 From: Lukas Metzger Date: Fri, 24 Jul 2020 17:20:01 +0200 Subject: Ignore testservers on import --- db_structure.sql | 3 ++- import/import.py | 27 ++++++++++++++++++++------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/db_structure.sql b/db_structure.sql index a47b5c1..e655fc8 100644 --- a/db_structure.sql +++ b/db_structure.sql @@ -51,7 +51,8 @@ CREATE TABLE reports ( CREATE TABLE names ( ip varchar(20) COLLATE utf8_bin NOT NULL, - name varchar(200) COLLATE utf8_bin NOT NULL + name varchar(200) COLLATE utf8_bin NOT NULL, + testserver tinyint NOT NULL, ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin; CREATE TABLE server ( diff --git a/import/import.py b/import/import.py index 68324c5..24a871c 100644 --- a/import/import.py +++ b/import/import.py @@ -12,22 +12,34 @@ import datetime def default(x, default): return default if x is None else x -with open(sys.argv[1], 'r') as f: - data = json.load(f) - c = configparser.ConfigParser() c.read('config.ini') filename = os.path.basename(sys.argv[1]) -date = re.search('^[0-9]{4}-[0-9]{2}-[0-9]{2}', filename).group(0) -date = datetime.datetime.fromtimestamp(int(data['days7']['tsTo'])).date() - ip = re.search('_([0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3})[_\.]', filename).group(1) db = pymysql.connect(c.get('db', 'host'), c.get('db', 'username'), c.get('db', 'password'), c.get('db', 'name'), cursorclass=pymysql.cursors.DictCursor) cursor = db.cursor() +# Check if ip is already in names table +cursor.execute('SELECT testserver FROM names WHERE ip = %s', (ip)) +entry = cursor.fetchall() +if len(entry) > 0: # Already in table + if entry[0]['testserver'] == 1: + print('This is a Testserver. Ignoring!') + db.close() + exit(0) +else: # Add to database + cursor.execute('INSERT INTO names(ip, name, testserver) VALUES (%s, %s, 0)', (ip, ip)) + +# Load data and timestamps +with open(sys.argv[1], 'r') as f: + data = json.load(f) + +date = re.search('^[0-9]{4}-[0-9]{2}-[0-9]{2}', filename).group(0) +date = datetime.datetime.fromtimestamp(int(data['days7']['tsTo'])).date() + # Add report to database try: cursor.execute('INSERT INTO reports(date,ip,version) VALUES(%s, %s, %s)',( @@ -37,7 +49,8 @@ try: )) except pymysql.err.IntegrityError: print('Report is already in database!') - exit(1) + db.close() + exit(0) cursor.execute('SELECT LAST_INSERT_ID() as id') reportId = cursor.fetchone()['id'] -- cgit v1.2.3-55-g7522