diff --git a/Scripts/python/pdf_import/import_pdf_from_o365_de.v.2.1.py b/Scripts/python/pdf_import/import_pdf_from_o365_de.v2.2.py similarity index 76% rename from Scripts/python/pdf_import/import_pdf_from_o365_de.v.2.1.py rename to Scripts/python/pdf_import/import_pdf_from_o365_de.v2.2.py index e69eb29..397bb94 100644 --- a/Scripts/python/pdf_import/import_pdf_from_o365_de.v.2.1.py +++ b/Scripts/python/pdf_import/import_pdf_from_o365_de.v2.2.py @@ -3,17 +3,91 @@ import requests import msal import base64 import logging +import mysql.connector +import sys + +sys.stdout.reconfigure(encoding='utf-8') + +# ============================================================================== +# MySQL KONFIGURATION +# ============================================================================== +MYSQL_HOST = "10.102.1.65" +MYSQL_USER = "svc.emailtopdf" +MYSQL_PASSWORD = "zZUHrps62skLKfr9yQwQ" +MYSQL_DATABASE = "emailtopdf" +MYSQL_TABLE = "emailtopdf" +# ============================================================================== + +def create_log_table_if_not_exists(): + """Létrehozza a naplótáblát, ha még nem létezik.""" + try: + cnx = mysql.connector.connect( + host=MYSQL_HOST, + user=MYSQL_USER, + password=MYSQL_PASSWORD, + database=MYSQL_DATABASE + ) + cursor = cnx.cursor() + + # A tábla struktúrája + create_table_query = f""" + CREATE TABLE IF NOT EXISTS `{MYSQL_TABLE}` ( + `id` INT AUTO_INCREMENT PRIMARY KEY, + `timestamp` DATETIME DEFAULT CURRENT_TIMESTAMP, + `level` VARCHAR(10), + `message` TEXT + ) + """ + cursor.execute(create_table_query) + cnx.commit() + logging.info(f"MySQL naplótábla '{MYSQL_TABLE}' ellenőrizve/létrehozva.") + except mysql.connector.Error as err: + logging.error(f"Hiba a MySQL naplótábla létrehozásakor/ellenőrzésekor: {err}") + finally: + if 'cnx' in locals() and cnx.is_connected(): + cursor.close() + cnx.close() + +class MySQLHandler(logging.Handler): + """Egyéni naplózó kezelő, amely MySQL adatbázisba ír.""" + def emit(self, record): + # Szűrjük ki a mysql.connector naplókat, hogy elkerüljük a rekurziót + if record.name.startswith('mysql.connector'): + return + + try: + cnx = mysql.connector.connect( + host=MYSQL_HOST, + user=MYSQL_USER, + password=MYSQL_PASSWORD, + database=MYSQL_DATABASE + ) + cursor = cnx.cursor() + + sql = f"INSERT INTO `{MYSQL_TABLE}` (level, message) VALUES (%s, %s)" + # A rekord szintjének és üzenetének használata + cursor.execute(sql, (record.levelname, self.format(record))) + cnx.commit() + except mysql.connector.Error as err: + # Ne naplózzunk a MySQLHandlerben, hogy elkerüljük a végtelen ciklust + # Helyette printeljük ki a hibát, hogy debuggolható legyen. + print(f"Hiba a MySQL naplóbejegyzés beszúrásakor: {err}") + finally: + if 'cnx' in locals() and cnx.is_connected(): + cursor.close() + cnx.close() # ============================================================================== # LOGGING KONFIGURATION # ============================================================================== -LOG_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'import_pdf_from_o365_de.v.2.1.log') +LOG_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'import_pdf_from_o365_de.v2.1.log') logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', handlers=[ - logging.FileHandler(LOG_FILE), - logging.StreamHandler() + logging.FileHandler(LOG_FILE, encoding='utf-8'), + logging.StreamHandler(), # Revert to default StreamHandler, encoding is handled by sys.stdout.reconfigure + MySQLHandler() # Re-enabled MySQLHandler ] ) # ============================================================================== @@ -215,6 +289,7 @@ def main(): if __name__ == "__main__": logging.info("Skript gestartet.") + create_log_table_if_not_exists() # Call to create table if not os.path.exists(DOWNLOAD_DIR): logging.error(f"Fehler: Der Download-Ordner existiert nicht: {DOWNLOAD_DIR}") else: diff --git a/Scripts/python/pdf_import/requirements.txt b/Scripts/python/pdf_import/requirements.txt new file mode 100644 index 0000000..2b4bdc7 --- /dev/null +++ b/Scripts/python/pdf_import/requirements.txt @@ -0,0 +1 @@ +mysql-connector-python==8.2.0 \ No newline at end of file