diff --git a/get-progress.py b/get-progress.py index 0506390..cb5801e 100755 --- a/get-progress.py +++ b/get-progress.py @@ -85,31 +85,34 @@ db = pymysql.connect("localhost", dbuser, dbpw, "worldbuild") cursor = db.cursor() -while True: - sql = "SELECT topLevel.name AS topLevelName, status.name AS statusName, COUNT(*) AS sCount from secondLevel JOIN topLevel ON secondLevel.parent_id = topLevel.id JOIN status ON secondLevel.status_id = status.id GROUP BY status.id, topLevelName" - cursor.execute(sql) - rows = cursor.fetchall() - - status = {} - for row in rows: - if not row[0] in status: - status[row[0]] = {} - status[row[0]][row[1]] = row[2] - - sql = "SELECT topLevel.name, status.name FROM topLevel JOIN status ON topLevel.status_id = status.id" - cursor.execute(sql) - rows = cursor.fetchall() - - for row in rows: - status[row[0]]["status"] = row[1] - - try: - with open(fname, "w") as f: - json.dump(status, f, indent=4) - except IOError: - print("ERROR: Failed to write to file") +try: + while True: + sql = "SELECT topLevel.name AS topLevelName, status.name AS statusName, COUNT(*) AS sCount from secondLevel JOIN topLevel ON secondLevel.parent_id = topLevel.id JOIN status ON secondLevel.status_id = status.id GROUP BY status.id, topLevelName" + cursor.execute(sql) + rows = cursor.fetchall() - if repeat == 0: - sys.exit(0) - else: - sleep(repeat * 60) + status = {} + for row in rows: + if not row[0] in status: + status[row[0]] = {} + status[row[0]][row[1]] = row[2] + + sql = "SELECT topLevel.name, status.name FROM topLevel JOIN status ON topLevel.status_id = status.id" + cursor.execute(sql) + rows = cursor.fetchall() + + for row in rows: + status[row[0]]["status"] = row[1] + + try: + with open(fname, "w") as f: + json.dump(status, f, indent=4) + except IOError: + print("ERROR: Failed to write to file") + + if repeat == 0: + sys.exit(0) + else: + sleep(repeat * 60) +except KeyboardInterrupt: + print("Exiting...")