PSHomeCacheDepot / OBJECTS /scripts /gethumbnails.py
pebxcvi's picture
mini update
e0b65ce
import os
import shutil
import re
from pathlib import Path
def get_png_files(directory):
"""Find all relevant PNG files in the given directory."""
files = []
for root, _, filenames in os.walk(directory):
for filename in filenames:
if filename.endswith('.png'):
files.append(os.path.join(root, filename))
return files
def extract_t_number(file):
"""Extract the TXXX number from the file name if present."""
match = re.search(r'_T(\d{3})', file)
return int(match.group(1)) if match else 0
def select_best_thumbnail(files, search_string):
"""Select the best thumbnail based on the given rules and search string."""
matching_files = [f for f in files if search_string in f]
if not matching_files:
return None
# Sort by TXXX number, if it exists
matching_files.sort(key=extract_t_number, reverse=True)
return matching_files[0]
def copy_thumbnail(src, dest):
"""Copy the selected thumbnail to the destination."""
os.makedirs(os.path.dirname(dest), exist_ok=True)
shutil.copy2(src, dest)
def log_to_file(logfile, uuid, png_name, file_size):
"""Log to a tab-delimited file with UUID, PNG name, and file size."""
with open(logfile, 'a') as log:
log.write(f"{uuid}\t{png_name}\t{file_size}\n")
def log_processing(logfile, message):
"""Log detailed processing messages to the processing log."""
with open(logfile, 'a') as log:
log.write(f"{message}\n")
print(message) # Print to console as well for debugging
def clear_logs(*logfiles):
"""Clear the content of log files."""
for logfile in logfiles:
with open(logfile, 'w'):
pass
def process_directories(copy_files=False, search_string="small"):
# Correct base directory paths
base_dir = Path(r"Q:/PSHOME-INT/PSHomeCacheDepot/OBJECTS")
output_dir = Path.cwd() / 'THUMBNAILS' / 'Objects' # Output directory in current folder
# Log files should include the search_string in their names
log_file = Path.cwd() / f'processing_log_{search_string}.txt'
thumbnail_log_file = Path.cwd() / f'thumbnails_log_{search_string}.txt'
# Clear logs at the start
clear_logs(log_file, thumbnail_log_file)
if copy_files:
# Use absolute paths for prod2/live2 and prod/live directories
prod2_dir = base_dir / 'scee-home.playstation.net' / 'c.home' / 'prod2' / 'live2' / 'Objects'
prod_dir = base_dir / 'scee-home.playstation.net' / 'c.home' / 'prod' / 'live' / 'Objects'
counter = 0
processed_uuids = set()
# Check if directories exist
if not prod2_dir.exists():
log_processing(log_file, f"Directory {prod2_dir} not found!")
return
if not prod_dir.exists():
log_processing(log_file, f"Directory {prod_dir} not found!")
return
# Process prod2/live2/Objects first
log_processing(log_file, f"Started processing prod2/live2/Objects for {search_string}")
for uuid_dir in prod2_dir.iterdir():
if uuid_dir.is_dir():
# Initialize the log message
log_msg = f"processing /live2/{uuid_dir.name}/ ->"
png_files = get_png_files(uuid_dir)
if not png_files:
log_msg += f" {search_string} not found"
log_processing(log_file, log_msg)
continue
best_thumbnail = select_best_thumbnail(png_files, search_string)
if best_thumbnail:
t_version = extract_t_number(best_thumbnail)
dest_path = output_dir / uuid_dir.name / f"{search_string}.png" # Keep the original naming scheme
# Skip if already copied
if not dest_path.exists():
# New check: if ref.txt exists and already has the entry, skip copying
ref_path = Path.cwd() / "ref.txt"
if ref_path.exists():
with open(ref_path, 'r') as ref_file:
ref_entries = [line.strip() for line in ref_file.readlines()]
# Build entry in the format "UUID\SMALL.PNG"
ref_entry = f"{uuid_dir.name.upper()}\\{search_string.upper()}.PNG"
if ref_entry in ref_entries:
log_msg += f" {search_string} found (_T{t_version:03d}) -> skipped (already in ref.txt)"
log_processing(log_file, log_msg)
continue
copy_thumbnail(best_thumbnail, dest_path)
file_size = os.path.getsize(best_thumbnail)
log_to_file(thumbnail_log_file, uuid_dir.name, f"{search_string}.png", file_size)
log_msg += f" {search_string} found (_T{t_version:03d}) -> copied"
processed_uuids.add(uuid_dir.name)
counter += 1
else:
log_msg += f" {search_string} found (_T{t_version:03d}) -> skipped (already exists)"
else:
log_msg += f" {search_string} not found"
# Log the entire message at once
log_processing(log_file, log_msg)
# Process prod/live/Objects and copy to current directory if not already existing
log_processing(log_file, f"Started processing prod/live/Objects for {search_string}")
for uuid_dir in prod_dir.iterdir():
if uuid_dir.is_dir():
# Initialize the log message
log_msg = f"processing /live/{uuid_dir.name}/ ->"
if uuid_dir.name in processed_uuids:
log_msg += f" skipped (already processed in prod2/live2)"
log_processing(log_file, log_msg)
continue # Skip if already processed from prod2
png_files = get_png_files(uuid_dir)
if not png_files:
log_msg += f" {search_string} not found"
log_processing(log_file, log_msg)
continue
best_thumbnail = select_best_thumbnail(png_files, search_string)
if best_thumbnail:
t_version = extract_t_number(best_thumbnail)
dest_path = output_dir / uuid_dir.name / f"{search_string}.png" # Keep the original naming scheme
if not dest_path.exists():
# New check: if ref.txt exists and already has the entry, skip copying
ref_path = Path.cwd() / "ref.txt"
if ref_path.exists():
with open(ref_path, 'r') as ref_file:
ref_entries = [line.strip() for line in ref_file.readlines()]
ref_entry = f"{uuid_dir.name.upper()}\\{search_string.upper()}.PNG"
if ref_entry in ref_entries:
log_msg += f" {search_string} found (_T{t_version:03d}) -> skipped (already in ref.txt)"
log_processing(log_file, log_msg)
continue
copy_thumbnail(best_thumbnail, dest_path)
file_size = os.path.getsize(best_thumbnail)
log_to_file(thumbnail_log_file, uuid_dir.name, f"{search_string}.png", file_size)
log_msg += f" {search_string} found (_T{t_version:03d}) -> copied"
counter += 1
else:
log_msg += f" {search_string} found (_T{t_version:03d}) -> skipped (already exists)"
else:
log_msg += f" {search_string} not found"
# Log the entire message at once
log_processing(log_file, log_msg)
log_processing(log_file, f"Processing completed. {counter} files copied.")
else:
log_processing(log_file, "Copying was disabled. Skipping file processing.")
# Log completion
log_processing(log_file, "Logging of thumbnails completed.")
if __name__ == "__main__":
# First pass: process "large"
process_directories(copy_files=True, search_string="large")
# Second pass: process "small"
process_directories(copy_files=True, search_string="small")