File size: 8,516 Bytes
88fb279
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0b65ce
 
 
 
88fb279
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
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")