Update all

This commit is contained in:
sebastianserfling
2024-09-24 21:04:41 +02:00
parent 25af35f723
commit 9bde6e380b
8 changed files with 72 additions and 35 deletions
+1 -1
View File
@@ -3,4 +3,4 @@ SMTP_PORT=587
SMTP_USER=serfling@itdata-gera.de
SMTP_PASSWORD="xPElLoyD,94,#"
FROM_EMAIL=serfling@itdata-gera.de
TO_EMAIL=serfling@itdata-gera.de
TO_EMAIL=b.goetze@trendsetzer.eu
+30 -13
View File
@@ -12,7 +12,7 @@ import time # Import time module for sleep functionality
load_dotenv()
# Define server details
host = "180.1.1.164"
host = "imap.strato.de"
port = 993
# Function to send an email notification
@@ -66,7 +66,8 @@ while True:
id INTEGER PRIMARY KEY AUTOINCREMENT,
email TEXT,
messages_transferred INTEGER,
date TEXT
date TEXT,
auth_failed INTEGER DEFAULT 0
)
''')
@@ -92,13 +93,21 @@ while True:
username, password, domain = user
local_part = username.split('@')[0]
print(f"User: {username} running.")
# Check if the user previously had an authentication failure
cursor.execute("SELECT auth_failed FROM sync_results WHERE email = ? ORDER BY date DESC LIMIT 1", (username,))
result = cursor.fetchone()
if result and result[0] == 1:
print(f"Skipping {username} due to previous authentication failure.")
# continue # Skip this user due to previous authentication failure
# Command for imapsync
command = [
"imapsync",
"--host1", domain,
"--user1", username,
"--password1", password,
"--host2", "180.1.1.164",
"--host2", "192.168.178.90",
"--user2", "archiv@trendsetzer.eu",
"--password2", "Ln0m2YQZd23H54L5tCiyjIBWLEn8mk36v7KauqS8QFGzu",
"--subfolder2", f"{local_part}",
@@ -112,29 +121,37 @@ while True:
result = subprocess.run(command, capture_output=True, text=True)
output = result.stdout
# Initialize variables
auth_failed = 0
transferred_count = 0
# Check for EXIT_AUTHENTICATION_FAILURE_USER error
if "EXIT_AUTHENTICATION_FAILURE_USER" in output:
subject = f"Authentication Failure for {username}"
body = f"An authentication failure occurred for user {username} during IMAP sync."
send_email(subject, body, os.getenv('TO_EMAIL'))
auth_failed = 1 # Mark authentication failure
# Find and extract the "Messages transferred" line
for line in output.splitlines():
if "Messages transferred" in line:
transferred_messages = line.split(":")[-1].strip()
transferred_count = int(transferred_messages)
current_date = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# Insert result into SQLite database
cursor.execute('''
INSERT INTO sync_results (email, messages_transferred, date)
VALUES (?, ?, ?)
''', (username, transferred_count, current_date))
conn.commit()
print(f"Data inserted for {username}: {transferred_count} messages on {current_date}")
break
# Insert into the database if messages were transferred or if there was an auth failure
if transferred_count > 0 or auth_failed == 1:
current_date = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# Insert result into SQLite database
cursor.execute('''
INSERT INTO sync_results (email, messages_transferred, date, auth_failed)
VALUES (?, ?, ?, ?)
''', (username, transferred_count, current_date, auth_failed))
conn.commit()
print(f"Data inserted for {username}: {transferred_count} messages on {current_date}")
else:
print(f"No 'Messages transferred' line found for {username}.")
print(f"No messages transferred for {username}.")
except Exception as e:
print(f"Error running imapsync for {username}: {e}")
+2 -2
View File
@@ -1,3 +1,3 @@
requests
streamlit
pandas
streamlit
+27 -10
View File
@@ -1,10 +1,14 @@
import streamlit as st
import sqlite3
import pandas as pd
from datetime import datetime, timedelta
def fetch_data_from_db(db_path, table_name):
def fetch_data_from_db(db_path, table_name, start_date=None, end_date=None):
conn = sqlite3.connect(db_path)
query = f"SELECT * FROM {table_name}"
if table_name == "sync_results" and start_date and end_date:
query = f"SELECT * FROM {table_name} WHERE date BETWEEN '{start_date}' AND '{end_date}'"
else:
query = f"SELECT * FROM {table_name}"
df = pd.read_sql_query(query, conn)
conn.close()
return df
@@ -25,6 +29,9 @@ def delete_user_from_db(db_path, email):
cursor.execute('''
DELETE FROM users WHERE email = ?
''', (email,))
cursor.execute('''
DELETE FROM sync_results WHERE email = ?
''', (email,))
conn.commit()
conn.close()
@@ -82,6 +89,11 @@ def main():
emails.insert(0, "All")
selected_email = st.selectbox("Filter by Email:", options=emails)
# Add date range picker for filtering results by date
st.subheader("Filter by Date Range:")
start_date = st.date_input("Start Date", value=datetime.now().date())
end_date = st.date_input("End Date + 1 Tag", value=start_date + timedelta(days=1))
if selected_email == "All":
filtered_users_df = users_df
else:
@@ -91,16 +103,21 @@ def main():
# Display email transfer results if available
if not results_df.empty:
# Filter by email and date
if selected_email == "All":
st.write("Email Transfer Results:")
st.dataframe(results_df)
filtered_results_df = fetch_data_from_db(db_path, "sync_results", start_date=start_date, end_date=end_date)
else:
filtered_results_df = results_df[results_df['email'] == selected_email]
if not filtered_results_df.empty:
st.write(f"Email Transfer Results for {selected_email}:")
st.dataframe(filtered_results_df)
else:
st.write(f"No transfer data found for {selected_email}.")
filtered_results_df = fetch_data_from_db(db_path, "sync_results", start_date=start_date, end_date=end_date)
filtered_results_df = filtered_results_df[filtered_results_df['email'] == selected_email]
if not filtered_results_df.empty:
# Replace 'auth_failed' values with icons
filtered_results_df['auth_failed'] = filtered_results_df['auth_failed'].apply(lambda x: '' if x == 0 else '')
st.write(f"Email Transfer Results for {selected_email}:")
st.dataframe(filtered_results_df)
else:
st.write(f"No transfer data found for {selected_email} in the selected date range.")
else:
st.write("No email transfer results available.")