pdf script 2.2

This commit is contained in:
2026-02-11 14:14:22 +01:00
parent 23a60c6250
commit 5ae2747835
2 changed files with 79 additions and 3 deletions

View File

@@ -3,17 +3,91 @@ import requests
import msal import msal
import base64 import base64
import logging 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 # 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( logging.basicConfig(
level=logging.INFO, level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s', format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[ handlers=[
logging.FileHandler(LOG_FILE), logging.FileHandler(LOG_FILE, encoding='utf-8'),
logging.StreamHandler() 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__": if __name__ == "__main__":
logging.info("Skript gestartet.") logging.info("Skript gestartet.")
create_log_table_if_not_exists() # Call to create table
if not os.path.exists(DOWNLOAD_DIR): if not os.path.exists(DOWNLOAD_DIR):
logging.error(f"Fehler: Der Download-Ordner existiert nicht: {DOWNLOAD_DIR}") logging.error(f"Fehler: Der Download-Ordner existiert nicht: {DOWNLOAD_DIR}")
else: else:

View File

@@ -0,0 +1 @@
mysql-connector-python==8.2.0