#!/usr/bin/env python3
import re
from datetime import datetime, timedelta

# File path
log_file_path = '/var/log/pacman.log'

# Time of analysis
now = datetime.now()
time_threshold = now - timedelta(hours=24)

# Regex pattern to extract timestamp and package info
log_pattern = re.compile(r'\[(.*?)\] \[ALPM\] (installed|upgraded) (.+)')

# List to store installed/updated packages
installed_or_updated = []

# Open the log file and parse each line
with open(log_file_path, 'r') as log_file:
    for line in log_file:
        match = log_pattern.search(line)
        if match:
            timestamp_str, action, package = match.groups()
            timestamp = datetime.strptime(timestamp_str.split('+')[0], '%Y-%m-%dT%H:%M:%S')
            if timestamp > time_threshold:
                formatted_timestamp = timestamp.strftime('%Y-%m-%d %H:%M:%S')
                installed_or_updated.append(f"{formatted_timestamp} - {action} {package}")

# Output the result
if installed_or_updated:
    print("Packages installed or updated in the last 24 hours:")
    for pkg in installed_or_updated:
        print(f"- {pkg}")
else:
    print("No packages were installed or updated in the last 24 hours.")


