|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Script to remove '%' characters from screenshot filenames in game.json files. |
| 4 | +Processes all subfolders in the 'entries' folder, updating both the JSON and renaming files on disk. |
| 5 | +""" |
| 6 | + |
| 7 | +import json |
| 8 | +import os |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | +def process_entries_folder(entries_path): |
| 12 | + """Process all subfolders in the entries folder.""" |
| 13 | + entries_dir = Path(entries_path) |
| 14 | + |
| 15 | + if not entries_dir.exists(): |
| 16 | + print(f"Error: Entries folder '{entries_path}' does not exist") |
| 17 | + return |
| 18 | + |
| 19 | + processed_count = 0 |
| 20 | + renamed_count = 0 |
| 21 | + |
| 22 | + # Iterate through all subfolders |
| 23 | + for subfolder in entries_dir.iterdir(): |
| 24 | + if not subfolder.is_dir(): |
| 25 | + continue |
| 26 | + |
| 27 | + game_json_path = subfolder / "game.json" |
| 28 | + |
| 29 | + if not game_json_path.exists(): |
| 30 | + continue |
| 31 | + |
| 32 | + # Load game.json |
| 33 | + try: |
| 34 | + with open(game_json_path, 'r', encoding='utf-8') as f: |
| 35 | + game_data = json.load(f) |
| 36 | + except Exception as e: |
| 37 | + print(f"Error reading {game_json_path}: {e}") |
| 38 | + continue |
| 39 | + |
| 40 | + # Check if screenshots property exists |
| 41 | + if 'screenshots' not in game_data or not isinstance(game_data['screenshots'], list): |
| 42 | + continue |
| 43 | + |
| 44 | + screenshots = game_data['screenshots'] |
| 45 | + updated = False |
| 46 | + |
| 47 | + # Process each screenshot entry |
| 48 | + for i, screenshot in enumerate(screenshots): |
| 49 | + if '%' in screenshot: |
| 50 | + # Remove '%' from the filename |
| 51 | + new_screenshot = screenshot.replace('%', '') |
| 52 | + |
| 53 | + # Get full paths for renaming the actual file |
| 54 | + old_file_path = subfolder / screenshot |
| 55 | + new_file_path = subfolder / new_screenshot |
| 56 | + |
| 57 | + # Rename the file on disk if it exists |
| 58 | + if old_file_path.exists(): |
| 59 | + try: |
| 60 | + old_file_path.rename(new_file_path) |
| 61 | + print(f"Renamed: {old_file_path} -> {new_file_path}") |
| 62 | + renamed_count += 1 |
| 63 | + except Exception as e: |
| 64 | + print(f"Error renaming {old_file_path}: {e}") |
| 65 | + else: |
| 66 | + print(f"Warning: File not found: {old_file_path}") |
| 67 | + |
| 68 | + # Update the JSON entry |
| 69 | + screenshots[i] = new_screenshot |
| 70 | + updated = True |
| 71 | + |
| 72 | + # Save the updated game.json if changes were made |
| 73 | + if updated: |
| 74 | + try: |
| 75 | + with open(game_json_path, 'w', encoding='utf-8') as f: |
| 76 | + json.dump(game_data, f, indent=4, ensure_ascii=False) |
| 77 | + f.write('\n') # Add newline at end of file |
| 78 | + print(f"Updated: {game_json_path}") |
| 79 | + processed_count += 1 |
| 80 | + except Exception as e: |
| 81 | + print(f"Error writing {game_json_path}: {e}") |
| 82 | + |
| 83 | + print(f"\nSummary:") |
| 84 | + print(f" - Updated {processed_count} game.json file(s)") |
| 85 | + print(f" - Renamed {renamed_count} file(s) on disk") |
| 86 | + |
| 87 | +if __name__ == "__main__": |
| 88 | + entries_folder = "../entries" |
| 89 | + process_entries_folder(entries_folder) |
0 commit comments