import os import sys import json from pprint import pprint from typing import Optional, List from pathlib import Path def read_color_data(color_id: str) -> Optional[str]: """ Read BrickLink color ID from a JSON file for a given color ID. Args: color_id: The color ID to look up Returns: The BrickLink ID if found, None otherwise """ try: with open(f"colors/{color_id}.json", 'r') as file: data = json.load(file) bricklink_data = data['external_ids'].get('BrickLink', {}) return bricklink_data.get('ext_ids', [None])[0] if bricklink_data else None except (FileNotFoundError, KeyError, json.JSONDecodeError): return None def process_colors_file(input_file: str, output_file: str) -> None: """ Process the colors.csv file and create a new combined file with BrickLink IDs. Args: input_file: Path to the input colors.csv file output_file: Path to write the output file """ new_file: List[str] = [] try: with open(input_file) as file: lines = file.readlines() # Process header new_file.append('id,name,rgb,is_trans,BrickLink\n') # Process data lines for line in lines[1:]: id, name, rgb, is_trans = line.strip().split(',') bricklink_id = read_color_data(id) new_file.append(f"{id},{name},{rgb},{is_trans},{bricklink_id}\n") # Write output file with open(output_file, "w") as f: f.writelines(new_file) except Exception as e: print(f"Error processing file: {e}") if __name__ == "__main__": input_file = "colors.csv" output_file = "colors_combined.csv" process_colors_file(input_file, output_file)