From 43b40070305bd8fde7c51f814702afedc9d578ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Gruszczy=C5=84ski?= Date: Mon, 26 May 2025 19:39:33 +0200 Subject: [PATCH] search user --- app.py | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/app.py b/app.py index 3bd1cbe..8c54c87 100644 --- a/app.py +++ b/app.py @@ -108,6 +108,71 @@ def get_users(db_config): connection.close() return users +def search_user(db_config, username): + """Wyszukuje użytkownika po nazwie i wyświetla jego statystyki.""" + connection = mysql.connector.connect(**db_config) + cursor = connection.cursor(dictionary=True) + + query = """ + SELECT + u.uid, + u.name, + u.mail, + u.access, + u.created, + u.login, + u.status, + p.points, + COUNT(n.nid) AS post_count + FROM users u + LEFT JOIN node n ON u.uid = n.uid + LEFT JOIN userpoints p ON u.uid = p.uid + WHERE u.name = %s + GROUP BY u.uid + """ + + cursor.execute(query, (username,)) + user = cursor.fetchone() + cursor.close() + connection.close() + + if not user: + print(f"❌ Użytkownik '{username}' nie został znaleziony.") + return None + + # Dodatkowe informacje o e-mailu + temp_domains_cache = load_temp_domains() + email_valid = not is_fake_email(user['mail']) and not is_temp_email(user['mail'], temp_domains_cache) + bad_name = is_bad_name(user['name']) + + # Oblicz dni nieaktywności + now_ts = int(datetime.now().timestamp()) + last_access = user['access'] or 0 + days_inactive = (now_ts - last_access) / 86400 if last_access else float('inf') + + # Przygotuj dane do wyświetlenia + user_data = { + 'UID': user['uid'], + 'Nazwa użytkownika': user['name'], + 'E-mail': user['mail'], + 'Status konta': 'Aktywny' if user['status'] == 1 else 'Zablokowany', + 'Data rejestracji': datetime.fromtimestamp(user['created']).strftime('%Y-%m-%d %H:%M:%S') if user.get('created') else 'Nieznana', + 'Ostatnie logowanie': datetime.fromtimestamp(user['access']).strftime('%Y-%m-%d %H:%M:%S') if user.get('access') else 'Nigdy', + 'Ostatnia aktywność': datetime.fromtimestamp(user['login']).strftime('%Y-%m-%d %H:%M:%S') if user.get('login') else 'Nigdy', + 'Dni nieaktywności': f"{days_inactive:.1f} dni", + 'Punkty': user.get('points', 0), + 'Liczba postów': user.get('post_count', 0), + 'E-mail poprawny': 'Tak' if email_valid else 'Nie', + 'Tymczasowy e-mail': 'Tak' if is_temp_email(user['mail'], temp_domains_cache) else 'Nie', + 'Podejrzana nazwa': 'Tak' if bad_name else 'Nie' + } + + # Wyświetl wyniki w tabeli + print("\n🔍 Szczegóły użytkownika:") + print(tabulate([(k, v) for k, v in user_data.items()], tablefmt="fancy_grid")) + + return user + def is_fake_email(email): try: domain = email.split('@')[1] @@ -434,6 +499,8 @@ def main(): parser.add_argument('--mail-template', type=str, help='Ścieżka do alternatywnego pliku HTML z szablonem maila (domyślnie: mail_template.html)') + parser.add_argument('--search-user', metavar="USERNAME", + help='Wyszukaj użytkownika po nazwie i wyświetl jego statystyki') args = parser.parse_args() @@ -473,6 +540,16 @@ def main(): flush_redis_cache() return + if args.search_user: + db_config = { + 'host': args.host or os.getenv('DB_HOST'), + 'user': args.user or os.getenv('DB_USER'), + 'password': args.password or os.getenv('DB_PASSWORD'), + 'database': args.database or os.getenv('DB_NAME') + } + search_user(db_config, args.search_user) + return + db_config = { 'host': args.host or os.getenv('DB_HOST'), 'user': args.user or os.getenv('DB_USER'),