From f7c39c9585646139ac2baaf9430776d051e05cfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Gruszczy=C5=84ski?= Date: Sun, 18 May 2025 23:19:58 +0200 Subject: [PATCH] nowy mail i funkcja --- app.py | 45 ++++++++++++++++++--------------------------- 1 file changed, 18 insertions(+), 27 deletions(-) diff --git a/app.py b/app.py index f8228c8..084f7f2 100644 --- a/app.py +++ b/app.py @@ -3,7 +3,7 @@ import argparse import mysql.connector import dns.resolver -import datetime +from datetime import datetime, timedelta import csv import socket from dotenv import load_dotenv @@ -21,8 +21,6 @@ import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText import time -from datetime import datetime, timedelta - # Redis - baza 5 redis_client = redis.Redis(host='localhost', port=6379, db=5, decode_responses=True) @@ -81,7 +79,7 @@ def is_temp_email(email): return False def export_to_csv(users): - now = datetime.datetime.now().strftime("%Y-%m-%d_%H%M") + now = datetime.now().strftime("%Y-%m-%d_%H%M") filename = f"user_cleanup_results_{now}.csv" with open(filename, mode='w', newline='', encoding='utf-8') as f: writer = csv.writer(f) @@ -89,8 +87,8 @@ def export_to_csv(users): for u in users: writer.writerow([ u['uid'], u['name'], u['mail'], - datetime.datetime.fromtimestamp(u['access']).strftime('%Y-%m-%d') if u['access'] else 'nigdy', - datetime.datetime.fromtimestamp(u['created']).strftime('%Y-%m-%d') if u.get('created') else 'brak', + datetime.fromtimestamp(u['access']).strftime('%Y-%m-%d') if u['access'] else 'nigdy', + datetime.fromtimestamp(u['created']).strftime('%Y-%m-%d') if u.get('created') else 'brak', u.get('points', 0), 'TAK' if u['inactive'] else 'NIE', 'TAK' if u['email_valid'] else 'NIE', @@ -99,7 +97,7 @@ def export_to_csv(users): print(f"📁 CSV zapisany: {filename}") def export_to_excel(users): - now = datetime.datetime.now().strftime("%Y-%m-%d_%H%M") + now = datetime.now().strftime("%Y-%m-%d_%H%M") filename = f"user_cleanup_results_{now}.xlsx" workbook = xlsxwriter.Workbook(filename) sheet = workbook.add_worksheet("Wyniki") @@ -110,8 +108,8 @@ def export_to_excel(users): sheet.write(row_idx, 0, u['uid']) sheet.write(row_idx, 1, u['name']) sheet.write(row_idx, 2, u['mail']) - sheet.write(row_idx, 3, datetime.datetime.fromtimestamp(u['access']).strftime('%Y-%m-%d') if u['access'] else 'nigdy') - sheet.write(row_idx, 4, datetime.datetime.fromtimestamp(u['created']).strftime('%Y-%m-%d') if u.get('created') else 'brak') + sheet.write(row_idx, 3, datetime.fromtimestamp(u['access']).strftime('%Y-%m-%d') if u['access'] else 'nigdy') + sheet.write(row_idx, 4, datetime.fromtimestamp(u['created']).strftime('%Y-%m-%d') if u.get('created') else 'brak') sheet.write(row_idx, 5, u.get('points', 0)) sheet.write(row_idx, 6, 'TAK' if u['inactive'] else 'NIE') sheet.write(row_idx, 7, 'TAK' if u['email_valid'] else 'NIE') @@ -182,10 +180,9 @@ def send_email_batch(users, smtp_config, mails_per_pack=100, time_per_pack=60, d print(f"❌ Błąd podczas odczytu szablonu HTML: {e}") return - # Filtrowanie użytkowników według zakresu nieaktywności if inactive_range: min_days, max_days = inactive_range - now_ts = int(datetime.datetime.now().timestamp()) + now_ts = int(datetime.now().timestamp()) filtered_users = [] for user in users: last_access = user['access'] or 0 @@ -226,15 +223,14 @@ def send_email_batch(users, smtp_config, mails_per_pack=100, time_per_pack=60, d msg['To'] = user['mail'] msg['Subject'] = "Twoje konto w unitraklub.pl" - # Generowanie treści maila z uwzględnieniem ostatniego logowania body = template.replace("@user", user['name']) \ - .replace("@rejestracja", datetime.datetime.fromtimestamp(user['created']).strftime('%Y-%m-%d')) \ - .replace("@ostatnie_logowanie", datetime.datetime.fromtimestamp(user['access']).strftime('%Y-%m-%d') if user['access'] else 'nigdy') + .replace("@rejestracja", datetime.fromtimestamp(user['created']).strftime('%Y-%m-%d')) \ + .replace("@ostatnie_logowanie", datetime.fromtimestamp(user['access']).strftime('%Y-%m-%d') if user['access'] else 'nigdy') msg.attach(MIMEText(body, 'html')) smtp.send_message(msg) - time.sleep(0.5) # opcjonalne mikroopóźnienie między mailami + time.sleep(0.5) except Exception as e: print(f"⚠️ Błąd wysyłki do {user['mail']}: {e}") @@ -339,7 +335,6 @@ def main(): parser.add_argument('--only-invalid-emails', action='store_true', help='Usuń tylko użytkowników z nieprawidłowymi lub tymczasowymi adresami e-mail (bez sprawdzania aktywności)') - # NEW: dodano argument --inactive-since parser.add_argument( "--inactive-since", type=str, @@ -348,7 +343,6 @@ def main(): args = parser.parse_args() - # NEW: przetwarzanie parametru --inactive-since inactive_range = None if args.inactive_since: try: @@ -362,7 +356,7 @@ def main(): test_user = { 'name': 'Testowy Użytkownik', 'mail': args.send_test, - 'created': int(datetime.datetime.now().timestamp()) - (86400 * 365 * 2) # 2 lata temu + 'created': int(datetime.now().timestamp()) - (86400 * 365 * 2) # 2 lata temu } print(f"📬 Wysyłka testowego maila na: {test_user['mail']}") @@ -387,7 +381,7 @@ def main(): } users = get_users(db_config) - now_ts = int(dt.now().timestamp()) + now_ts = int(datetime.now().timestamp()) final_candidates = [] inactive_count = 0 invalid_email_count = 0 @@ -401,7 +395,6 @@ def main(): continue if (user.get('post_count') or 0) > 0: - # Pomijamy użytkownika, który dodał treści continue user['temp_email'] = is_temp_email(user['mail']) @@ -413,8 +406,7 @@ def main(): final_candidates.append(user) continue - # Pomijanie aktywnych "weteranów" - created_year = datetime.datetime.fromtimestamp(user['created']).year if user.get('created') else None + created_year = datetime.fromtimestamp(user['created']).year if user.get('created') else None recent_login_threshold = now_ts - (args.recent_login_days * 86400) if created_year and created_year <= args.veteran_year: @@ -437,7 +429,6 @@ def main(): if args.validate or user['inactive'] or not user['email_valid']: final_candidates.append(user) - # Redundant safety filter to exclude any with points final_candidates = [u for u in final_candidates if (u.get('points') or 0) == 0] if args.report_domains: @@ -446,8 +437,8 @@ def main(): if args.show_table: print(tabulate([ [u['uid'], u['name'], u['mail'], - datetime.datetime.fromtimestamp(u['access']).strftime('%Y-%m-%d') if u['access'] else 'nigdy', - datetime.datetime.fromtimestamp(u['created']).strftime('%Y-%m-%d') if u.get('created') else 'brak', + datetime.fromtimestamp(u['access']).strftime('%Y-%m-%d') if u['access'] else 'nigdy', + datetime.fromtimestamp(u['created']).strftime('%Y-%m-%d') if u.get('created') else 'brak', u.get('points', 0), 'TAK' if u['inactive'] else 'NIE', 'TAK' if u['email_valid'] else 'NIE', @@ -466,7 +457,7 @@ def main(): smtp_config, args.mails_per_pack, args.time_per_pack, - inactive_range=inactive_range # NEW: przekazanie zakresu nieaktywności + inactive_range=inactive_range ) print("\n📋 Parametry filtrowania:") @@ -474,7 +465,7 @@ def main(): print(f"- Nieaktywni: brak logowania przez ≥ {args.days_inactive} dni (~{days_to_years(args.days_inactive)} lat)") print(f"- Weterani: konta zarejestrowane w roku ≤ {args.veteran_year}") print(f"- Pominięci weterani: logowanie w ciągu ostatnich ≤ {args.recent_login_days} dni (~{days_to_years(args.recent_login_days)} lat)") - if args.inactive_since: # NEW: wyświetlanie zakresu nieaktywności + if args.inactive_since: min_days, max_days = map(int, args.inactive_since.split('-')) print(f"- Wysyłka maili tylko dla nieaktywnych od {min_days} do {max_days} dni (~{days_to_years(min_days)}-{days_to_years(max_days)} lat)")