import json import os import re from collections import defaultdict log_pattern = re.compile( r"[(?P<date>[^]]+)] Queue upload requested by (?P<username>.+?) for file (?P<filepath>.+)" )
Function to parse the log file and update statistics
def parse_log_file(file_path, folder_count, user_count, unique_logs): with open(file_path, 'r') as file: for line in file: if "Queue upload requested by" not in line: continue match = log_pattern.match(line.strip()) if not match: print(line) if match: date_str = match.group('date') username = match.group('username') filepath = match.group('filepath') # Combine date and file path to create a unique identifier unique_id = f"{date_str}_{filepath}" # Check if the log entry is unique if unique_id in unique_logs: continue # Skip duplicate logs # Add unique ID to unique logs unique_logs.add(unique_id) # Extract the directory path by stripping off the file name folder = '\'.join(filepath.split('\')[:-1]) # Increment folder and user counts folder_count[folder] += 1 user_count[username] += 1
Function to print statistics
def print_statistics(folder_count, user_count): # Print folder statistics sorted_folders = sorted(folder_count.items(), key=lambda x: x[1], reverse=True) print("\nMost Common Folders:") for folder, count in sorted_folders: print(f"{folder}: {count} requests") # Print user statistics sorted_users = sorted(user_count.items(), key=lambda x: x[1], reverse=True) print("\nUsers with Most Requests:") for user, count in sorted_users: print(f"{user}: {count} requests")
Function to save statistics and unique logs to files
def save_data(folder_count, user_count, unique_logs, folder_file, user_file, log_file): with open(folder_file, 'w') as f: json.dump(folder_count, f) with open(user_file, 'w') as f: json.dump(user_count, f) with open(log_file, 'w') as f: json.dump(list(unique_logs), f)
Function to load existing statistics and unique logs from files
def load_data(folder_file, user_file, log_file): if os.path.exists(folder_file): with open(folder_file, 'r') as f: folder_count = defaultdict(int, json.load(f)) else: folder_count = defaultdict(int) if os.path.exists(user_file): with open(user_file, 'r') as f: user_count = defaultdict(int, json.load(f)) else: user_count = defaultdict(int) if os.path.exists(log_file): with open(log_file, 'r') as f: unique_logs = set(json.load(f)) else: unique_logs = set() return folder_count, user_count, unique_logs
File paths for storing statistics and unique logs
folder_stats_file = 'soulseek_logs/folder_stats.json' user_stats_file = 'soulseek_logs/user_stats.json' unique_logs_file = 'soulseek_logs/unique_logs.json'
Load existing data
folder_count, user_count, unique_logs = load_data(folder_stats_file, user_stats_file, unique_logs_file)
File path to your new log file
log_file_path = 'soulseek_log.txt'
Parse the log file and update statistics
parse_log_file(log_file_path, folder_count, user_count, unique_logs)
Print updated statistics
print_statistics(folder_count, user_count)
Save updated data
save_data(folder_count, user_count, unique_logs, folder_stats_file, user_stats_file, unique_logs_file)
Comments
No comments yet